《RC4流密码算法之C语言实现(共8页).doc》由会员分享,可在线阅读,更多相关《RC4流密码算法之C语言实现(共8页).doc(8页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上RC4流密码算法之C语言实现RC4加密算法RC4算法的原理很简单,包括初始化算法(KSA)和伪随机子密码生成算法(PRGA)两大部分。假设S-box长度和密钥长度均为为n。先来看看算法的初始化部分(用类C伪代码表示):for (i=0; in; i+)s=i;在初始化的过程中,密钥的主要功能是将S-box搅乱,i确保S-box的每个元素都得到处理,j保证S-box的搅乱是随机的。而不同的S-box在经过伪随机子密码生成算法的处理后可以得到不同的子密钥序列,并且,该序列是随机的:j=0;for (i=0; in; i+)j=(j+s+k)%256;swap(s, sj)
2、;i=j=0;得到的子密码sub_k用以和明文进行xor运算,得到密文,解密过程也完全相同。根据目前的分析结果,没有任何的分析对于密钥长度达到128位的RC4有效,所以,RC4是目前最安全的加密算法之一,大家可以放心使用!while (明文未结束)+i%=n;j=(j+s)%n;swap(s, sj);sub_k=s(s+sj)%n);位长可以自己随意设置,将256设置为你希望的即可我的C语言源码:平台:windowsXP,VC+6.0有什么大家可以讨论的地方请留言或发邮件至我邮箱:#include#include#includevoid swap(unsigned char *s1,unsi
3、gned char *s2)char temp;temp=*s1;*s1=*s2;*s2=temp;void re_S(unsigned char *S)unsigned int i;for(i=0;i256;i+) Si=i;void re_T(unsigned char *T,char *key)int i;int keylen;keylen=strlen(key);for(i=0;i256;i+) Ti=keyi%keylen;void re_Sbox(unsigned char *S,unsigned char *T)int i;int j=0;for(i=0;i256;i+) j=(
4、j+Si+Ti)%256; swap(&Si,&Sj);void re_RC4(unsigned char *S,char *key)char T255=0;re_S(S);re_T(T,key);re_Sbox(S,T);void RC4(FILE *readfile,FILE *writefile,char *key) unsigned char S255=0;unsigned char readbuf1;int i,j,t; re_RC4(S,key);i=j=0;while(fread(readbuf,1,1,readfile) i = (i + 1) % 256; j = (j +
5、Si) % 256; swap(&Si,&Sj); t = (Si + (Sj % 256) % 256; readbuf0=readbuf0St; fwrite(readbuf,1,1,writefile); memset(readbuf,0,1);printf(加密|解密成功!n);int main(int argc,char *argv)char *Filename1,*Filename2;char *key;FILE *file1,*file2;Filename1 = argv1;Filename2 = argv2;key=argv3;if ( argc = 4) printf(*正在
6、加密中*n);else printf(输入格式: n); printf(文件名|加密或解密文件名|存放文件名|密钥 n); return -1; if (file1= fopen(Filename1,rb) = NULL| (file2 = fopen(Filename2,wb)=NULL) printf(Cant open filen); return -1; RC4(file1,file2,key);fclose(file1);fclose(file2);return 0;加密解密算法 RC5- -#include #include math.h#include static const
7、w=32;static const r=12;static const b=16;static const t=26;/2*r+2=12*2+2static const c=4; /r*8/b = 16*8/32 typedef unsigned long int FOURBYTEINT;typedef unsigned char BYTE;/* 循环左移和右移函数* x : 被循环的数* y : 将要循环的位数*/#define ROTL(x,y) (x)(w-(y&(w-1)#define ROTR(x,y) (x)(y&(w-1) | (x)(w-(y&(w-1)/* 产生子密钥的函数*
8、/void generateChildKey(unsigned char*,FOURBYTEINT*);/* 产生初试密钥的函数*/void InitialKey(unsigned char* KeyK,int b)for(int i=0;ib;i+) KeyKi=0;int intiSeed=3;KeyK0=intiSeed;for(i=1;ib;i+) KeyKi = (BYTE) ( (int)(pow(3,i)%(255-i) ); /KeyKi = KeyKi-1*intiSeed % 256;/* 取最近奇数的函数*/int Odd(double fl)int floorOffl
9、= (int)floor(fl);if (floorOffl% 2=0) return floorOffl+1;return floorOffl;/* 加密函数*/void Encipher(FOURBYTEINT* In,FOURBYTEINT* Out,FOURBYTEINT* S)FOURBYTEINT X,Y;X = In0+S0;Y = In1+S1;for(int i=1;i0;i-) Y = ROTR(Y-S2*i+1,X)X; X = ROTR(X-S2*i,Y)Y; Out0=X - S0;Out1=Y - S1;/* 主函数*/void main() FOURBYTEINT
10、 ChildKeyS2*r+2; /64bitunsigned char KeyKb;/8bit=byteInitialKey(KeyK,b);int k;generateChildKey(KeyK,ChildKeyS);FOURBYTEINT Source2=8888,6666;/coutSource0 Source1endl;printf(加密以前的明文:);for (k=0;k2;k+) printf(%.8lX ,Sourcek);FOURBYTEINT Dest2;Encipher(Source,Dest,ChildKeyS);/coutDest0 Dest1endl;printf(
11、n);printf(加密以后的密文:);for (k=0;k2;k+) printf(%.8lX ,Destk);printf(n);Decipher(Dest,Source,ChildKeyS);/coutSource0 Source1endl;printf(解密以后的明文:);for (k=0;k2;k+) printf(%.8lX ,Sourcek);printf(n);/*printf(加密以前的明文:);for (k=0;k6;k+) printf(%.8lX ,mingwen_orgk);printf(n);printf(加密以后的密文:);for (k=0;k6;k+) prin
12、tf(%.8lX ,miwenk);printf(n);printf(解密以后的明文:);for (k=0;k6;k+) printf(%.8lX ,mingwen_finalk);printf(n);*/void generateChildKey(unsigned char* KeyK,FOURBYTEINT* ChildKeyS)const double e = 2.9;const double Phia = 1.9;/int PW = Odd(e-1)*pow(2,w);int PW = 0xb7e15163;/int QW = Odd(Phia -1)*pow(2,w);int QW
13、= 0x9e3779b9;int i;FOURBYTEINT Lc;/初试化数组 SChildKeyS0=PW;for ( i=1;it;i+) ChildKeySi=(ChildKeySi-1+ QW);/% (2w)不需要/transform from K to L/initialization Lfor(i=0;ic;i+) Li=0;int u = w/8;for (i=b-1;i!=-1; i-) Li/u = (Li/u8)+KeyKi;for (i=0;i4;i+)printf(%.8Xn,Li);/generate child keysFOURBYTEINT A,B,X,Y;A=B=X=Y=0;for(i=0;i3*t;i+) X = ChildKeySA = ROTL(ChildKeySA+X+Y,3); A = (A+1) % t; Y = LB = ROTL(LB+X+Y,(X+Y); B = (B+1) % c;/*for (i=0;it;i+) printf(%.8X,ChildKeySi); printf(n);*/专心-专注-专业