这是个在dos下运行的加密解密程序。
运行方式
加密:
程序名 被加密文件名 密码 1
解密:
程序名 被解密文件名 密码 -1
注意:为确保安全密码不得小于10个字节。
/////////////////////////////////////程序开始
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <io.h>
#include <string.h>
void HanShu(char *filename,char *mima,int biaozhi);
static int s1=1;
static int s2=1;
#define MODMULT(a,b,m,s) s= b*s; if(s<0) s+=m;
unsigned int combinedLOG(void);
void initLOG(int InitS1,int InitS2);
unsigned int x_ = 1,
y_ = 1,
z_ = 1,
c_ = 1;
unsigned int Rand(); //随机函数
int main(void)
{
char filename[240],mima[240];
int biaozhi;
printf("请输入被加密文件名 密码 标志 : ");
scanf("%s %s %d" ,&filename,&mima,&biaozhi);
//HanShu是加密解密函数 biaozhi=1 加密 biaozhi=-1 解密
HanShu(filename,mima,biaozhi);
getch(); //暂停
return 0;
}
void HanShu(char *filename,char *mima,int biaozhi)
{
unsigned int K[100]={0}; //密码相关参数
FILE *f;
unsigned char *ss; //文件容器
unsigned char *ss1; //密钥容器
unsigned char ch;
unsigned char ch1='\0';
int b256=256;
int Length=0;
int i,j=0;
if((f=fopen(filename,"ab+"))==NULL)
{printf("无法打开1");exit(0);}
Length = filelength(_fileno(f)); //得到文件长度
fclose(f);
ss=new unsigned char[Length+256+1]; //文件容器
ss1=new unsigned char[Length+256+1]; //密钥容器
ss = (unsigned char*)calloc(Length+256, sizeof(char));
ss1 = (unsigned char*)calloc(Length+256, sizeof(char));
unsigned int mm[1000];
for(i=0;i<(int)strlen(mima);i++) // 密码复杂化
{
mm[i]=mima[i]*19773161;
mm[i]=mm[i]%(i+1373);
mm[i]=mm[i]>>(mima[5]%7);
mm[i]=mm[i]*mm[i]*mm[i]+101;
}
for(i=0;i<(int)strlen(mima);i++)
{
srand(mm[i]);
srand(rand()+mm[i]);
}
for(i=0;i<100;i++)
{
K[i] = K[i] + mm[i%(int)strlen(mima)] + rand()*rand()*rand()*rand() + 1;
}
x_=K[11];
y_=K[22];
z_=K[33];
c_=K[44];
if((f=fopen(filename,"ab+"))==NULL)
{printf("无法打开2");exit(0);}
fseek(f,0,0);
fread(ss,Length,1,f); //文件内容读入 ss
fclose(f);
//=================数据处理函数Q
int n1=101;
int ka=K[0];
for(i=0;i<Length;i++) //密钥串建造 及加密解密
{
ss1[i] = Rand()*ka+ K[i%41];
if(i%11==0) {ka=ka+K[i%100];}
if(i%7==3) {x_++;}
if(i%17==0) {y_++;Rand();}
if(i%27==10) {z_++;Rand();}
if(i%37==13) {n1=x_;x_=c_;c_=n1;}
ss[i] = ss[i] + biaozhi*ss1[i]; //加密解密
}//for(i=0;i<Length;i++)
ss1[Length]='\0';
ss[Length]='\0';
if((f=fopen(filename,"w"))==NULL)
{printf("无法打开3");exit(0);}
fclose(f);
if((f=fopen(filename,"ab+"))==NULL)
{printf("无法打开4");exit(0);}
fseek(f,0,SEEK_SET);
fwrite(ss,Length,1,f);
fclose(f);
delete [] ss;
delete [] ss1;
}
unsigned int combinedLOG(void)
{
unsigned int z;
MODMULT(40014,12211,2147483563,s1)
MODMULT(40692,3791,2147483399,s2)
z = s1 -s2;
return z;
}
void initLOG(int InitS1,int InitS2)
{
s1=InitS1;
s2=InitS2;
}
unsigned int Rand()
{
unsigned __int64 t,A = 698769069L;
x_ = 69069*x_+12345;
y_ ^= (y_<<13); y_ ^= (y_>>17); y_ ^= (y_<<5);
t = A*z_ + c_; c_ = ((unsigned int)t >> 32);
return x_+y_+(z_=(unsigned int)t);
}
/////////////////////////////////////程序结束
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课