RC4流密码算法之C语言实现.docx
《RC4流密码算法之C语言实现.docx》由会员分享,可在线阅读,更多相关《RC4流密码算法之C语言实现.docx(10页珍藏版)》请在冰豆网上搜索。
RC4流密码算法之C语言实现
RC4加密算法
RC4算法的原理很简单,包括初始化算法(KSA)和伪随机子密码生成算法(PRGA)两大部分。
假设S-box长度和密钥长度均为为n。
先来看看算法的初始化部分(用类C伪代码表示):
for(i=0;i s=i;在初始化的过程中,密钥的主要功能是将S-box搅乱,i确保S-box的每个元素都得到处理,j保证S-box的搅乱是随机的。而不同的S-box在经过伪随机子密码生成算法的处理后可以得到不同的子密钥序列,并且,该序列是随机的: j=0; for(i=0;i { j=(j+s+k)%256; swap(s,s[j]); } i=j=0;得到的子密码sub_k用以和明文进行xor运算,得到密文,解密过程也完全相同。根据目前的分析结果,没有任何的分析对于密钥长度达到128位的RC4有效,所以,RC4是目前最安全的加密算法之一,大家可以放心使用! while(明文未结束) { ++i%=n; j=(j+s)%n; swap(s,s[j]); sub_k=s((s+s[j])%n); } 位长可以自己随意设置,将256设置为你希望的即可我的C语言源码:平台:windowsXP,VC++6.0有什么大家可以讨论的地方请留言或发邮件至我邮箱:#include#include#includevoidswap(unsignedchar*s1,unsignedchar*s2){chartemp;temp=*s1;*s1=*s2;*s2=temp;}voidre_S(unsignedchar*S){unsignedinti;for(i=0;i<256;i++) S[i]=i;}voidre_T(unsignedchar*T,char*key){inti;intkeylen;keylen=strlen(key);for(i=0;i<256;i++) T[i]=key[i%keylen];}voidre_Sbox(unsignedchar*S,unsignedchar*T){inti;intj=0;for(i=0;i<256;i++){ j=(j+S[i]+T[i])%256; swap(&S[i],&S[j]);}}voidre_RC4(unsignedchar*S,char*key){charT[255]={0};re_S(S);re_T(T,key);re_Sbox(S,T);}voidRC4(FILE*readfile,FILE*writefile,char*key){unsignedcharS[255]={0};unsignedcharreadbuf[1];inti,j,t;re_RC4(S,key);i=j=0;while(fread(readbuf,1,1,readfile)){ i=(i+1)%256; j=(j+S[i])%256; swap(&S[i],&S[j]); t=(S[i]+(S[j]%256))%256; readbuf[0]=readbuf[0]^S[t]; fwrite(readbuf,1,1,writefile); memset(readbuf,0,1);}printf("加密|解密成功!!!\n");}intmain(intargc,char*argv[]){char*Filename1,*Filename2;char*key;FILE*file1,*file2;Filename1=argv[1];Filename2=argv[2];key=argv[3];if(argc==4){ printf("**********************正在加密中*******************\n");}else{ printf("输入格式:\n"); printf("文件名|加密或解密文件名|存放文件名|密钥\n"); return-1;}if((file1=fopen(Filename1,"rb"))==NULL||(file2=fopen(Filename2,"wb"))==NULL){ printf("Can'topenfile\n"); return-1;} RC4(file1,file2,key);fclose(file1);fclose(file2);return0;} 加密解密算法RC5--#include#include"math.h"#includestaticconstw=32;staticconstr=12;staticconstb=16;staticconstt=26;//2*r+2=12*2+2staticconstc=4;//r*8/b=16*8/32typedefunsignedlongintFOURBYTEINT;typedefunsignedcharBYTE;/***循环左移和右移函数*x:被循环的数*y:将要循环的位数*/#defineROTL(x,y)(((x)<<(y&(w-1)))|((x)>>(w-(y&(w-1)))))#defineROTR(x,y)(((x)>>(y&(w-1)))|((x)<<(w-(y&(w-1)))))/**产生子密钥的函数*/voidgenerateChildKey(unsignedchar*,FOURBYTEINT*);/**产生初试密钥的函数*/voidInitialKey(unsignedchar*KeyK,intb){for(inti=0;iKeyK[i]=0;}intintiSeed=3;KeyK[0]=intiSeed;for(i=1;iKeyK[i]=(BYTE)(((int)(pow(3,i)))%(255-i));//KeyK[i]=KeyK[i-1]*intiSeed%256;}}/**取最近奇数的函数*/intOdd(doublefl){intfloorOffl=(int)floor(fl);if(floorOffl%2==0)returnfloorOffl+1;returnfloorOffl;}/**加密函数*/voidEncipher(FOURBYTEINT*In,FOURBYTEINT*Out,FOURBYTEINT*S){FOURBYTEINTX,Y;X=In[0]+S[0];Y=In[1]+S[1];for(inti=1;i<=r;i++){X=ROTL((X^Y),Y)+S[2*i];Y=ROTL((Y^X),X)+S[2*i+1];}Out[0]=X;Out[1]=Y;}/**解密函数*/voidDecipher(FOURBYTEINT*In,FOURBYTEINT*Out,FOURBYTEINT*S){FOURBYTEINTX,Y;X=In[0];Y=In[1];for(inti=r;i>0;i--){Y=ROTR(Y-S[2*i+1],X)^X;X=ROTR(X-S[2*i],Y)^Y;}Out[0]=X-S[0];Out[1]=Y-S[1];}/**主函数*/voidmain(){FOURBYTEINTChildKeyS[2*r+2];//64bitunsignedcharKeyK[b];//8bit=byteInitialKey(KeyK,b);intk;generateChildKey(KeyK,ChildKeyS);FOURBYTEINTSource[2]={8888,6666};//cout<printf("加密以前的明文:");for(k=0;k<2;k++){printf("%.8lX",Source[k]);}FOURBYTEINTDest[2];Encipher(Source,Dest,ChildKeyS);//cout<printf("\n");printf("加密以后的密文:");for(k=0;k<2;k++){printf("%.8lX",Dest[k]);}printf("\n");Decipher(Dest,Source,ChildKeyS);//cout<printf("解密以后的明文:");for(k=0;k<2;k++){printf("%.8lX",Source[k]);}printf("\n");/*printf("加密以前的明文:");for(k=0;k<6;k++){printf("%.8lX",mingwen_org[k]);}printf("\n");printf("加密以后的密文:");for(k=0;k<6;k++){printf("%.8lX",miwen[k]);}printf("\n");printf("解密以后的明文:");for(k=0;k<6;k++){printf("%.8lX",mingwen_final[k]);}printf("\n");*/}voidgenerateChildKey(unsignedchar*KeyK,FOURBYTEINT*ChildKeyS){constdoublee=2.718281828459;constdoublePhia=1.618033988749;//intPW=Odd((e-1)*pow(2,w));intPW=0xb7e15163;//intQW=Odd((Phia-1)*pow(2,w));intQW=0x9e3779b9;inti;FOURBYTEINTL[c];//初试化数组SChildKeyS[0]=PW;for(i=1;iChildKeyS[i]=(ChildKeyS[i-1]+QW);//%(2<}//transformfromKtoL//initializationLfor(i=0;iL[i]=0;}intu=w/8;for(i=b-1;i!=-1;i--)L[i/u]=(L[i/u]<<8)+KeyK[i];for(i=0;i<4;i++)printf("%.8X\n",L[i]);/generatechildkeysFOURBYTEINTA,B,X,Y;A=B=X=Y=0;for(i=0;i<3*t;i++){X=ChildKeyS[A]=ROTL(ChildKeyS[A]+X+Y,3);A=(A+1)%t;Y=L[B]=ROTL(L[B]+X+Y,(X+Y));B=(B+1)%c;}/*for(i=0;iprintf("%.8X",ChildKeyS[i]);printf("\n");}*/}
s=i;
在初始化的过程中,密钥的主要功能是将S-box搅乱,i确保S-box的每个元素都得到处理,j保证S-box的搅乱是随机的。
而不同的S-box在经过伪随机子密码生成算法的处理后可以得到不同的子密钥序列,并且,该序列是随机的:
j=0;
for(i=0;i { j=(j+s+k)%256; swap(s,s[j]); } i=j=0;得到的子密码sub_k用以和明文进行xor运算,得到密文,解密过程也完全相同。根据目前的分析结果,没有任何的分析对于密钥长度达到128位的RC4有效,所以,RC4是目前最安全的加密算法之一,大家可以放心使用! while(明文未结束) { ++i%=n; j=(j+s)%n; swap(s,s[j]); sub_k=s((s+s[j])%n); } 位长可以自己随意设置,将256设置为你希望的即可我的C语言源码:平台:windowsXP,VC++6.0有什么大家可以讨论的地方请留言或发邮件至我邮箱:#include#include#includevoidswap(unsignedchar*s1,unsignedchar*s2){chartemp;temp=*s1;*s1=*s2;*s2=temp;}voidre_S(unsignedchar*S){unsignedinti;for(i=0;i<256;i++) S[i]=i;}voidre_T(unsignedchar*T,char*key){inti;intkeylen;keylen=strlen(key);for(i=0;i<256;i++) T[i]=key[i%keylen];}voidre_Sbox(unsignedchar*S,unsignedchar*T){inti;intj=0;for(i=0;i<256;i++){ j=(j+S[i]+T[i])%256; swap(&S[i],&S[j]);}}voidre_RC4(unsignedchar*S,char*key){charT[255]={0};re_S(S);re_T(T,key);re_Sbox(S,T);}voidRC4(FILE*readfile,FILE*writefile,char*key){unsignedcharS[255]={0};unsignedcharreadbuf[1];inti,j,t;re_RC4(S,key);i=j=0;while(fread(readbuf,1,1,readfile)){ i=(i+1)%256; j=(j+S[i])%256; swap(&S[i],&S[j]); t=(S[i]+(S[j]%256))%256; readbuf[0]=readbuf[0]^S[t]; fwrite(readbuf,1,1,writefile); memset(readbuf,0,1);}printf("加密|解密成功!!!\n");}intmain(intargc,char*argv[]){char*Filename1,*Filename2;char*key;FILE*file1,*file2;Filename1=argv[1];Filename2=argv[2];key=argv[3];if(argc==4){ printf("**********************正在加密中*******************\n");}else{ printf("输入格式:\n"); printf("文件名|加密或解密文件名|存放文件名|密钥\n"); return-1;}if((file1=fopen(Filename1,"rb"))==NULL||(file2=fopen(Filename2,"wb"))==NULL){ printf("Can'topenfile\n"); return-1;} RC4(file1,file2,key);fclose(file1);fclose(file2);return0;} 加密解密算法RC5--#include#include"math.h"#includestaticconstw=32;staticconstr=12;staticconstb=16;staticconstt=26;//2*r+2=12*2+2staticconstc=4;//r*8/b=16*8/32typedefunsignedlongintFOURBYTEINT;typedefunsignedcharBYTE;/***循环左移和右移函数*x:被循环的数*y:将要循环的位数*/#defineROTL(x,y)(((x)<<(y&(w-1)))|((x)>>(w-(y&(w-1)))))#defineROTR(x,y)(((x)>>(y&(w-1)))|((x)<<(w-(y&(w-1)))))/**产生子密钥的函数*/voidgenerateChildKey(unsignedchar*,FOURBYTEINT*);/**产生初试密钥的函数*/voidInitialKey(unsignedchar*KeyK,intb){for(inti=0;iKeyK[i]=0;}intintiSeed=3;KeyK[0]=intiSeed;for(i=1;iKeyK[i]=(BYTE)(((int)(pow(3,i)))%(255-i));//KeyK[i]=KeyK[i-1]*intiSeed%256;}}/**取最近奇数的函数*/intOdd(doublefl){intfloorOffl=(int)floor(fl);if(floorOffl%2==0)returnfloorOffl+1;returnfloorOffl;}/**加密函数*/voidEncipher(FOURBYTEINT*In,FOURBYTEINT*Out,FOURBYTEINT*S){FOURBYTEINTX,Y;X=In[0]+S[0];Y=In[1]+S[1];for(inti=1;i<=r;i++){X=ROTL((X^Y),Y)+S[2*i];Y=ROTL((Y^X),X)+S[2*i+1];}Out[0]=X;Out[1]=Y;}/**解密函数*/voidDecipher(FOURBYTEINT*In,FOURBYTEINT*Out,FOURBYTEINT*S){FOURBYTEINTX,Y;X=In[0];Y=In[1];for(inti=r;i>0;i--){Y=ROTR(Y-S[2*i+1],X)^X;X=ROTR(X-S[2*i],Y)^Y;}Out[0]=X-S[0];Out[1]=Y-S[1];}/**主函数*/voidmain(){FOURBYTEINTChildKeyS[2*r+2];//64bitunsignedcharKeyK[b];//8bit=byteInitialKey(KeyK,b);intk;generateChildKey(KeyK,ChildKeyS);FOURBYTEINTSource[2]={8888,6666};//cout<printf("加密以前的明文:");for(k=0;k<2;k++){printf("%.8lX",Source[k]);}FOURBYTEINTDest[2];Encipher(Source,Dest,ChildKeyS);//cout<printf("\n");printf("加密以后的密文:");for(k=0;k<2;k++){printf("%.8lX",Dest[k]);}printf("\n");Decipher(Dest,Source,ChildKeyS);//cout<printf("解密以后的明文:");for(k=0;k<2;k++){printf("%.8lX",Source[k]);}printf("\n");/*printf("加密以前的明文:");for(k=0;k<6;k++){printf("%.8lX",mingwen_org[k]);}printf("\n");printf("加密以后的密文:");for(k=0;k<6;k++){printf("%.8lX",miwen[k]);}printf("\n");printf("解密以后的明文:");for(k=0;k<6;k++){printf("%.8lX",mingwen_final[k]);}printf("\n");*/}voidgenerateChildKey(unsignedchar*KeyK,FOURBYTEINT*ChildKeyS){constdoublee=2.718281828459;constdoublePhia=1.618033988749;//intPW=Odd((e-1)*pow(2,w));intPW=0xb7e15163;//intQW=Odd((Phia-1)*pow(2,w));intQW=0x9e3779b9;inti;FOURBYTEINTL[c];//初试化数组SChildKeyS[0]=PW;for(i=1;iChildKeyS[i]=(ChildKeyS[i-1]+QW);//%(2<}//transformfromKtoL//initializationLfor(i=0;iL[i]=0;}intu=w/8;for(i=b-1;i!=-1;i--)L[i/u]=(L[i/u]<<8)+KeyK[i];for(i=0;i<4;i++)printf("%.8X\n",L[i]);/generatechildkeysFOURBYTEINTA,B,X,Y;A=B=X=Y=0;for(i=0;i<3*t;i++){X=ChildKeyS[A]=ROTL(ChildKeyS[A]+X+Y,3);A=(A+1)%t;Y=L[B]=ROTL(L[B]+X+Y,(X+Y));B=(B+1)%c;}/*for(i=0;iprintf("%.8X",ChildKeyS[i]);printf("\n");}*/}
{
j=(j+s+k)%256;
swap(s,s[j]);
}
i=j=0;
得到的子密码sub_k用以和明文进行xor运算,得到密文,解密过程也完全相同。
根据目前的分析结果,没有任何的分析对于密钥长度达到128位的RC4有效,所以,RC4是目前最安全的加密算法之一,大家可以放心使用!
while(明文未结束)
++i%=n;
j=(j+s)%n;
sub_k=s((s+s[j])%n);
位长可以自己随意设置,将256设置为你希望的即可
我的C语言源码:
平台:
windowsXP,VC++6.0
有什么大家可以讨论的地方请留言或发邮件至我邮箱:
#include
voidswap(unsignedchar*s1,unsignedchar*s2)
chartemp;
temp=*s1;
*s1=*s2;
*s2=temp;
voidre_S(unsignedchar*S)
unsignedinti;
for(i=0;i<256;i++)
S[i]=i;
voidre_T(unsignedchar*T,char*key)
inti;
intkeylen;
keylen=strlen(key);
T[i]=key[i%keylen];
voidre_Sbox(unsignedchar*S,unsignedchar*T)
intj=0;
j=(j+S[i]+T[i])%256;
swap(&S[i],&S[j]);
voidre_RC4(unsignedchar*S,char*key)
charT[255]={0};
re_S(S);
re_T(T,key);
re_Sbox(S,T);
voidRC4(FILE*readfile,FILE*writefile,char*key)
unsignedcharS[255]={0};
unsignedcharreadbuf[1];
inti,j,t;
re_RC4(S,key);
while(fread(readbuf,1,1,readfile))
i=(i+1)%256;
j=(j+S[i])%256;
t=(S[i]+(S[j]%256))%256;
readbuf[0]=readbuf[0]^S[t];
fwrite(readbuf,1,1,writefile);
memset(readbuf,0,1);
printf("加密|解密成功!
!
\n");
intmain(intargc,char*argv[])
char*Filename1,*Filename2;
char*key;
FILE*file1,*file2;
Filename1=argv[1];
Filename2=argv[2];
key=argv[3];
if(argc==4)
printf("**********************正在加密中*******************\n");
else
printf("输入格式:
printf("文件名|加密或解密文件名|存放文件名|密钥\n");
return-1;
if((file1=fopen(Filename1,"rb"))==NULL||(file2=fopen(Filename2,"wb"))==NULL)
printf("Can'topenfile\n");
RC4(file1,file2,key);
fclose(file1);
fclose(file2);
return0;
加密解密算法RC5--
#include"math.h"
staticconstw=32;
staticconstr=12;
staticconstb=16;
staticconstt=26;//2*r+2=12*2+2
staticconstc=4;//r*8/b=16*8/32
typedefunsignedlongintFOURBYTEINT;
typedefunsignedcharBYTE;
/**
*循环左移和右移函数
*x:
被循环的数
*y:
将要循环的位数
*/
#defineROTL(x,y)(((x)<<(y&(w-1)))|((x)>>(w-(y&(w-1)))))
#defineROTR(x,y)(((x)>>(y&(w-1)))|((x)<<(w-(y&(w-1)))))
/**产生子密钥的函数
voidgenerateChildKey(unsignedchar*,FOURBYTEINT*);
/**产生初试密钥的函数
voidInitialKey(unsignedchar*KeyK,intb){
for(inti=0;i
KeyK[i]=0;
intintiSeed=3;
KeyK[0]=intiSeed;
for(i=1;i
KeyK[i]=(BYTE)(((int)(pow(3,i)))%(255-i));
//KeyK[i]=KeyK[i-1]*intiSeed%256;
/**取最近奇数的函数
intOdd(doublefl){
intfloorOffl=(int)floor(fl);
if(floorOffl%2==0)returnfloorOffl+1;
returnfloorOffl;
/**加密函数
voidEncipher(FOURBYTEINT*In,FOURBYTEINT*Out,FOURBYTEINT*S){
FOURBYTEINTX,Y;
X=In[0]+S[0];
Y=In[1]+S[1];
for(inti=1;i<=r;i++){
X=ROTL((X^Y),Y)+S[2*i];
Y=ROTL((Y^X),X)+S[2*i+1];
Out[0]=X;
Out[1]=Y;
/**解密函数
voidDecipher(FOURBYTEINT*In,FOURBYTEINT*Out,FOURBYTEINT*S){
X=In[0];
Y=In[1];
for(inti=r;i>0;i--){
Y=ROTR(Y-S[2*i+1],X)^X;
X=ROTR(X-S[2*i],Y)^Y;
Out[0]=X-S[0];
Out[1]=Y-S[1];
/**主函数
voidmain()
FOURBYTEINTChildKeyS[2*r+2];//64bit
unsignedcharKeyK[b];//8bit=byte
InitialKey(KeyK,b);
intk;
generateChildKey(KeyK,ChildKeyS);
FOURBYTEINTSource[2]={8888,6666};
//cout<printf("加密以前的明文:");for(k=0;k<2;k++){printf("%.8lX",Source[k]);}FOURBYTEINTDest[2];Encipher(Source,Dest,ChildKeyS);//cout<printf("\n");printf("加密以后的密文:");for(k=0;k<2;k++){printf("%.8lX",Dest[k]);}printf("\n");Decipher(Dest,Source,ChildKeyS);//cout<printf("解密以后的明文:");for(k=0;k<2;k++){printf("%.8lX",Source[k]);}printf("\n");/*printf("加密以前的明文:");for(k=0;k<6;k++){printf("%.8lX",mingwen_org[k]);}printf("\n");printf("加密以后的密文:");for(k=0;k<6;k++){printf("%.8lX",miwen[k]);}printf("\n");printf("解密以后的明文:");for(k=0;k<6;k++){printf("%.8lX",mingwen_final[k]);}printf("\n");*/}voidgenerateChildKey(unsignedchar*KeyK,FOURBYTEINT*ChildKeyS){constdoublee=2.718281828459;constdoublePhia=1.618033988749;//intPW=Odd((e-1)*pow(2,w));intPW=0xb7e15163;//intQW=Odd((Phia-1)*pow(2,w));intQW=0x9e3779b9;inti;FOURBYTEINTL[c];//初试化数组SChildKeyS[0]=PW;for(i=1;iChildKeyS[i]=(ChildKeyS[i-1]+QW);//%(2<}//transformfromKtoL//initializationLfor(i=0;iL[i]=0;}intu=w/8;for(i=b-1;i!=-1;i--)L[i/u]=(L[i/u]<<8)+KeyK[i];for(i=0;i<4;i++)printf("%.8X\n",L[i]);/generatechildkeysFOURBYTEINTA,B,X,Y;A=B=X=Y=0;for(i=0;i<3*t;i++){X=ChildKeyS[A]=ROTL(ChildKeyS[A]+X+Y,3);A=(A+1)%t;Y=L[B]=ROTL(L[B]+X+Y,(X+Y));B=(B+1)%c;}/*for(i=0;iprintf("%.8X",ChildKeyS[i]);printf("\n");}*/}
printf("加密以前的明文:
");
for(k=0;k<2;k++){
printf("%.8lX",Source[k]);
FOURBYTEINTDest[2];
Encipher(Source,Dest,ChildKeyS);
//cout<printf("\n");printf("加密以后的密文:");for(k=0;k<2;k++){printf("%.8lX",Dest[k]);}printf("\n");Decipher(Dest,Source,ChildKeyS);//cout<printf("解密以后的明文:");for(k=0;k<2;k++){printf("%.8lX",Source[k]);}printf("\n");/*printf("加密以前的明文:");for(k=0;k<6;k++){printf("%.8lX",mingwen_org[k]);}printf("\n");printf("加密以后的密文:");for(k=0;k<6;k++){printf("%.8lX",miwen[k]);}printf("\n");printf("解密以后的明文:");for(k=0;k<6;k++){printf("%.8lX",mingwen_final[k]);}printf("\n");*/}voidgenerateChildKey(unsignedchar*KeyK,FOURBYTEINT*ChildKeyS){constdoublee=2.718281828459;constdoublePhia=1.618033988749;//intPW=Odd((e-1)*pow(2,w));intPW=0xb7e15163;//intQW=Odd((Phia-1)*pow(2,w));intQW=0x9e3779b9;inti;FOURBYTEINTL[c];//初试化数组SChildKeyS[0]=PW;for(i=1;iChildKeyS[i]=(ChildKeyS[i-1]+QW);//%(2<}//transformfromKtoL//initializationLfor(i=0;iL[i]=0;}intu=w/8;for(i=b-1;i!=-1;i--)L[i/u]=(L[i/u]<<8)+KeyK[i];for(i=0;i<4;i++)printf("%.8X\n",L[i]);/generatechildkeysFOURBYTEINTA,B,X,Y;A=B=X=Y=0;for(i=0;i<3*t;i++){X=ChildKeyS[A]=ROTL(ChildKeyS[A]+X+Y,3);A=(A+1)%t;Y=L[B]=ROTL(L[B]+X+Y,(X+Y));B=(B+1)%c;}/*for(i=0;iprintf("%.8X",ChildKeyS[i]);printf("\n");}*/}
printf("\n");
printf("加密以后的密文:
printf("%.8lX",Dest[k]);
Decipher(Dest,Source,ChildKeyS);
//cout<printf("解密以后的明文:");for(k=0;k<2;k++){printf("%.8lX",Source[k]);}printf("\n");/*printf("加密以前的明文:");for(k=0;k<6;k++){printf("%.8lX",mingwen_org[k]);}printf("\n");printf("加密以后的密文:");for(k=0;k<6;k++){printf("%.8lX",miwen[k]);}printf("\n");printf("解密以后的明文:");for(k=0;k<6;k++){printf("%.8lX",mingwen_final[k]);}printf("\n");*/}voidgenerateChildKey(unsignedchar*KeyK,FOURBYTEINT*ChildKeyS){constdoublee=2.718281828459;constdoublePhia=1.618033988749;//intPW=Odd((e-1)*pow(2,w));intPW=0xb7e15163;//intQW=Odd((Phia-1)*pow(2,w));intQW=0x9e3779b9;inti;FOURBYTEINTL[c];//初试化数组SChildKeyS[0]=PW;for(i=1;iChildKeyS[i]=(ChildKeyS[i-1]+QW);//%(2<}//transformfromKtoL//initializationLfor(i=0;iL[i]=0;}intu=w/8;for(i=b-1;i!=-1;i--)L[i/u]=(L[i/u]<<8)+KeyK[i];for(i=0;i<4;i++)printf("%.8X\n",L[i]);/generatechildkeysFOURBYTEINTA,B,X,Y;A=B=X=Y=0;for(i=0;i<3*t;i++){X=ChildKeyS[A]=ROTL(ChildKeyS[A]+X+Y,3);A=(A+1)%t;Y=L[B]=ROTL(L[B]+X+Y,(X+Y));B=(B+1)%c;}/*for(i=0;iprintf("%.8X",ChildKeyS[i]);printf("\n");}*/}
printf("解密以后的明文:
/*
for(k=0;k<6;k++){
printf("%.8lX",mingwen_org[k]);
printf("%.8lX",miwen[k]);
printf("%.8lX",mingwen_final[k]);
voidgenerateChildKey(unsignedchar*KeyK,FOURBYTEINT*ChildKeyS)
constdoublee=2.718281828459;
constdoublePhia=1.618033988749;
//intPW=Odd((e-1)*pow(2,w));
intPW=0xb7e15163;
//intQW=Odd((Phia-1)*pow(2,w));
intQW=0x9e3779b9;
FOURBYTEINTL[c];
//初试化数组S
ChildKeyS[0]=PW;
for(i=1;iChildKeyS[i]=(ChildKeyS[i-1]+QW);//%(2<}//transformfromKtoL//initializationLfor(i=0;iL[i]=0;}intu=w/8;for(i=b-1;i!=-1;i--)L[i/u]=(L[i/u]<<8)+KeyK[i];for(i=0;i<4;i++)printf("%.8X\n",L[i]);/generatechildkeysFOURBYTEINTA,B,X,Y;A=B=X=Y=0;for(i=0;i<3*t;i++){X=ChildKeyS[A]=ROTL(ChildKeyS[A]+X+Y,3);A=(A+1)%t;Y=L[B]=ROTL(L[B]+X+Y,(X+Y));B=(B+1)%c;}/*for(i=0;iprintf("%.8X",ChildKeyS[i]);printf("\n");}*/}
ChildKeyS[i]=(ChildKeyS[i-1]+QW);//%(2<}//transformfromKtoL//initializationLfor(i=0;iL[i]=0;}intu=w/8;for(i=b-1;i!=-1;i--)L[i/u]=(L[i/u]<<8)+KeyK[i];for(i=0;i<4;i++)printf("%.8X\n",L[i]);/generatechildkeysFOURBYTEINTA,B,X,Y;A=B=X=Y=0;for(i=0;i<3*t;i++){X=ChildKeyS[A]=ROTL(ChildKeyS[A]+X+Y,3);A=(A+1)%t;Y=L[B]=ROTL(L[B]+X+Y,(X+Y));B=(B+1)%c;}/*for(i=0;iprintf("%.8X",ChildKeyS[i]);printf("\n");}*/}
//transformfromKtoL
//initializationL
for(i=0;iL[i]=0;}intu=w/8;for(i=b-1;i!=-1;i--)L[i/u]=(L[i/u]<<8)+KeyK[i];for(i=0;i<4;i++)printf("%.8X\n",L[i]);/generatechildkeysFOURBYTEINTA,B,X,Y;A=B=X=Y=0;for(i=0;i<3*t;i++){X=ChildKeyS[A]=ROTL(ChildKeyS[A]+X+Y,3);A=(A+1)%t;Y=L[B]=ROTL(L[B]+X+Y,(X+Y));B=(B+1)%c;}/*for(i=0;iprintf("%.8X",ChildKeyS[i]);printf("\n");}*/}
L[i]=0;
intu=w/8;
for(i=b-1;i!
=-1;i--)
L[i/u]=(L[i/u]<<8)+KeyK[i];
for(i=0;i<4;i++)
printf("%.8X\n",L[i]);
/generatechildkeys
FOURBYTEINTA,B,X,Y;
A=B=X=Y=0;
for(i=0;i<3*t;i++){
X=ChildKeyS[A]=ROTL(ChildKeyS[A]+X+Y,3);
A=(A+1)%t;
Y=L[B]=ROTL(L[B]+X+Y,(X+Y));
B=(B+1)%c;
for(i=0;iprintf("%.8X",ChildKeyS[i]);printf("\n");}*/}
printf("%.8X",ChildKeyS[i]);
}*/
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1