RC5_C语言代码

来源:互联网 发布:mac搜狗 工具箱 编辑:程序博客网 时间:2024/04/19 12:47

RC5基本算法讲解:

将明文分成两组,A和B,32位秘钥组S0,S1、、、、S2r+1
其中r为加密轮数。
A+=S0;
B+=S1;
A=((A异或B)<<<B)+S2i;    //<<<此符号为循环左移。
B=((B异或A)<<<A)+S2i+1;
解密,相反即可。
B=((B-S2i+1)>>>A)异或A;
A=((A-S2i)>>>B)异或B;
A-=S0;
B-=S1 。

其中RC5秘钥产生是:

S[i]=(s[i-1]+P),s[i-1]=Q其中PQ32位,A=B=0,

3nn=c>2*(nr+1)?c:2*(nr+1);

A=S[i]=(S[i]+A+B<<<3);

B=L[j]=(L[j]+A+B<<<A+B);

i=(i+1)mod 2*(nr+1);

j=(j+1)mod c;


下面直接贴源代码:

#include <stdio.h>#include <malloc.h>typedef struct{unsigned int *xk;  //子秘钥;unsigned short nr;        //加密轮数;}rc5_ctx;#define ROTL32(x,c) (((x)<<(c)) | ((x)>>(32-c)))#define ROTR32(x,c) (((x)>>(c)) | ((x)<<(32-c)))/*void rc5_init(rc5_ctx *,unsigned short);void rc5_destroy(rc5_ctx *);void rc5_key(rc5_ctx *,unsigned char *,unsigned short);void rc5_encrypt(rc5_ctx *,unsigned int *,unsigned short);void rc5_decrypt(rc5_ctx *,unsigned int *,unsigned short);*/void rc5_destroy(rc5_ctx *c){int i;for(i=0;i<(c->nr)*2+2;i++){c->xk[i]=0;}free(c->xk);}void rc5_init(rc5_ctx * c,unsigned short rounds){c->nr=rounds;c->xk =(unsigned int *)malloc(4*(rounds*2+2));}void rc5_encrypt(rc5_ctx * c,unsigned int * data,unsigned short blocks){unsigned int *d,*sk;unsigned short h,i,rc;d=data;sk=(c->xk)+2;for(h=0;h<blocks;h++){d[0]+=c->xk[0];d[1]+=c->xk[1];for(i=0;i<c->nr;i+=2){d[0]^=d[1];rc=d[1] & 31;d[0]=ROTL32(d[0],rc);d[0]+=sk[i];d[1]^=d[0];rc=d[0] & 31;d[1]=ROTL32(d[1],rc);d[1]+=sk[i+1];}d+=2;}}void rc5_decrypt(rc5_ctx * c,unsigned int * data,unsigned short blocks){unsigned int *d,*sk;int h,i,rc;d=data;sk=(c->xk)+2;for(h=0;h<blocks;h++){for(i=0;i<c->nr;i+=2){d[1]-=sk[i+1];rc=d[0] & 31;d[1]=ROTR32(d[1],rc);d[1]^=d[0];d[0]-=sk[i];rc=d[1] & 31;d[0]=ROTR32(d[0],rc);d[0]^=d[1];}d[0]-=c->xk[0];d[1]-=c->xk[1];d+=2;}}void rc5_key(rc5_ctx * c,unsigned char * key,unsigned short keylen){unsigned int *pk,A,B;unsigned short xk_len,pk_len,i,num_steps,rc;unsigned char * cp;xk_len = c->nr*2+2;pk_len=keylen/4;if(keylen%4 != 0){pk_len+=1;}pk=(unsigned int *)malloc(4*pk_len);if(pk==NULL){printf("An error occurred!\n");return 0;}for(i=0;i<pk_len;i++)pk[i]=0;cp=(unsigned char *)pk;for(i=0;i<keylen;i++){cp[i]=key[i];}c->xk[0]=0xb7e15163;for(i=1;i<xk_len;i++){c->xk[i]=c->xk[i-1]+0x9e377969;}A=0;B=0;for(i=0;i<xk_len;i++){A=A+c->xk[i];B^=c->xk[i];}if(pk_len>xk_len){num_steps=3*pk_len;}else{num_steps=3*xk_len;}A=0;B=0;for(i=0;i<num_steps;i++){A=ROTL32(c->xk[i%xk_len]+A+B,3);c->xk[i%xk_len]=ROTL32(c->xk[i%xk_len]+A+B,3);rc=(A+B) & 31;B=pk[i%pk_len];pk[i%pk_len]=ROTL32(pk[i%pk_len]+A+B,rc);}for(i=0;i<pk_len;i++)pk[i]=0;free(pk);}void main(){rc5_ctx c;unsigned int data[8];char key[]="ABCDE";unsigned short i;printf("--------------------------------------------------------\n");for(i=0;i<8;i++)data[i]=i;rc5_init(&c,10);rc5_key(&c,key,5);rc5_encrypt(&c,data,4);printf("encrypt:\n");for(i=0;i<8;i+=2){printf("Blocks %1d = %8lx %8lx\n",i/2,data[i],data[i+1]);}rc5_decrypt(&c,data+4,2);printf("dncrypt:\n");for(i=0;i<8;i+=2){printf("Blocks %1d = %8lx %8lx\n",i/2,data[i],data[i+1]);}}




0 0
原创粉丝点击