免费预览已结束,剩余16页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
/* Rijndael Block Cipher - rijndael.c Edited by Jianqin Zhou 28st April 2004 Written by Mike Scott 21st April 1999 mikecompapp.dcu.ie An alternative faster version is implemented in MIRACL puting.dcu.ie/pub/crypto/miracl.zip Copyright (c) 1999 Mike Scott Simply compile and run, e.g. cl /O2 rijndael.c (Microsoft C) bcc32 /O2 rijndael.c (Borland C) gcc -O2 rijndael.c -o rijndael (Gnu C) Compiles and runs fine as a C+ program also. See rijndael documentation. The code follows the documentation as closely as possible, and where possible uses the same function and variable names. Permission for free direct or derivative use is granted subject to compliance with any conditions that the originators of the algorithm place on its exploitation. Inspiration from Brian Gladmans implementation is acknowledged. Written for clarity, rather than speed. Assumes long is 32 bit quantity. Full implementation. Endian indifferent.*/#include #define BYTE unsigned char /* 8 bits */#define WORD unsigned long /* 32 bits */* rotates x one bit to the left */#define ROTL(x) (x)7)|(x)1)/* Rotates 32-bit word left by 1, 2 or 3 byte */#define ROTL8(x) (x)24)#define ROTL16(x) (x)16)#define ROTL24(x) (x)8)/* Fixed Data */static BYTE InCo4=0xB,0xD,0x9,0xE; /* Inverse Coefficients */static BYTE fbsub256;static BYTE rbsub256;static BYTE ptab256,ltab256;static WORD ftable256;static WORD rtable256;static WORD rco30;/* Parameter-dependent data */int Nk,Nb,Nr;BYTE fi24,ri24;WORD fkey120;WORD rkey120;static WORD pack(BYTE *b) /* pack bytes into a 32-bit Word */ return (WORD)b324)|(WORD)b216)|(WORD)b18); b2=(BYTE)(a16); b3=(BYTE)(a24);/关于模多项式0x011b的乘10b运算static BYTE xtime(BYTE a) BYTE b; if (a&0x80) b=0x1B; else b=0; a=1; a=b; return a;static BYTE bmul(BYTE x,BYTE y) /* x.y= AntiLog(Log(x) + Log(y) */ if (x & y) return ptab(ltabx+ltaby)%255; else return 0;static WORD SubByte(WORD a) BYTE b4; unpack(a,b); b0=fbsubb0; b1=fbsubb1; b2=fbsubb2; b3=fbsubb3; return pack(b); static BYTE product(WORD x,WORD y) /* dot product of two 4-byte arrays */ BYTE xb4,yb4; unpack(x,xb); unpack(y,yb); return bmul(xb0,yb0)bmul(xb1,yb1)bmul(xb2,yb2)bmul(xb3,yb3);static WORD InvMixCol(WORD x) /* matrix Multiplication */ WORD y,m; BYTE b4; m=pack(InCo); b3=product(m,x); m=ROTL24(m); b2=product(m,x); m=ROTL24(m); b1=product(m,x); m=ROTL24(m); b0=product(m,x); y=pack(b); return y;BYTE ByteSub(BYTE x) BYTE y=ptab255-ltabx; /* multiplicative inverse */ x=y; x=ROTL(x); y=x; x=ROTL(x); y=x; x=ROTL(x); y=x; x=ROTL(x); y=x; y=0x63; return y;void gentables(void) /* generate tables */ int i; BYTE y,b4; /* use 3 as primitive root to generate power and log tables */ ltab0=0; ptab0=1; ltab1=0; ptab1=3; ltab3=1; for (i=2;i256;i+) ptabi=ptabi-1xtime(ptabi-1); ltabptabi=i; /* affine transformation:- each bit is xored with itself shifted one bit 仿射变换*/ fbsub0=0x63; rbsub0x63=0; for (i=1;i256;i+) y=ByteSub(BYTE)i); fbsubi=y; rbsuby=i; for (i=0,y=1;i30;i+) rcoi=y; y=xtime(y); /* calculate forward and reverse tables */ for (i=0;i256;i+) y=fbsubi; b3=yxtime(y); b2=y; b1=y; b0=xtime(y); ftablei=pack(b); y=rbsubi; b3=bmul(InCo0,y); b2=bmul(InCo1,y); b1=bmul(InCo2,y); b0=bmul(InCo3,y); rtablei=pack(b); void strtoHex(char *str,char *hex)char ch;int i=0, by = 0; while(i = 0 & ch = 9) by = (by = A & ch = F) by = (by 4) + ch - A + 10; else / error if not hexadecimal printf(key must be in hexadecimal notationn); exit(0); / store a key byte for each pair of hexadecimal digits if(i+ & 1) hexi / 2 - 1 = by & 0xff; void hextoStr(char *hex,char *str) int i=0, by = 0; while(i 4 &0x0f; if(by = 0 & by = 0x0A & by = 0 & by = 0x0A & by =Nk) Nr=6+Nb; else Nr=6+Nk; C1=1; if (Nb8) C2=2; C3=3; else C2=3; C3=4; /* pre-calculate forward and reverse increments */ for (m=j=0;jnb;j+,m+=3) fim=(j+C1)%nb; fim+1=(j+C2)%nb; fim+2=(j+C3)%nb; rim=(nb+j-C1)%nb; rim+1=(nb+j-C2)%nb; rim+2=(nb+j-C3)%nb; N=Nb*(Nr+1); for (i=j=0;iNk;i+,j+=4) CipherKeyi=pack(BYTE *)&keyj); for (i=0;iNk;i+) fkeyi=CipherKeyi; for (j=Nk,k=0;jN;j+=Nk,k+) fkeyj=fkeyj-NkSubByte(ROTL24(fkeyj-1)rcok; if (Nk=6) for (i=1;iNk & (i+j)N;i+) fkeyi+j=fkeyi+j-Nkfkeyi+j-1; else for (i=1;i4 &(i+j)N;i+) fkeyi+j=fkeyi+j-Nkfkeyi+j-1; if (j+4)N) fkeyj+4=fkeyj+4-NkSubByte(fkeyj+3); for (i=5;iNk & (i+j)N;i+) fkeyi+j=fkeyi+j-Nkfkeyi+j-1; /* now for the expanded decrypt key in reverse order */ for (j=0;jNb;j+) rkeyj+N-Nb=fkeyj; for (i=Nb;iN-Nb;i+=Nb) k=N-Nb-i; for (j=0;jNb;j+) rkeyk+j=InvMixCol(fkeyi+j); for (j=N-Nb;jN;j+) rkeyj-N+Nb=fkeyj;/* There is an obvious time/space trade-off possible here. * * Instead of just one ftable, I could have 4, the other * * 3 pre-rotated to save the ROTL8, ROTL16 and ROTL24 overhead */ void encrypt(char *buff) int i,j,k,m; WORD a8,b8,*x,*y,*t; for (i=j=0;iNb;i+,j+=4) ai=pack(BYTE *)&buffj); ai=fkeyi; k=Nb; x=a; y=b;/* State alternates between a and b */ for (i=1;iNr;i+) /* Nr is number of rounds. May be odd. */* if Nb is fixed - unroll this next loop and hard-code in the values of fi */ for (m=j=0;j8) ROTL16(ftable(BYTE)(xfim+116) ROTL24(ftablexfim+224); t=x; x=y; y=t; /* swap pointers */ /* Last Round - unroll if possible */ for (m=j=0;j8) ROTL16(WORD)fbsub(BYTE)(xfim+116) ROTL24(WORD)fbsubxfim+224); for (i=j=0;iNb;i+,j+=4) unpack(yi,(BYTE *)&buffj); xi=yi=0; /* clean up stack */ return;void decrypt(char *buff) int i,j,k,m; WORD a8,b8,*x,*y,*t; for (i=j=0;iNb;i+,j+=4) ai=pack(BYTE *)&buffj); ai=rkeyi; k=Nb; x=a; y=b;/* State alternates between a and b */ for (i=1;iNr;i+) /* Nr is number of rounds. May be odd. */* if Nb is fixed - unroll this next loop and hard-code in the values of ri */ for (m=j=0;j8) ROTL16(rtable(BYTE)(xrim+116) ROTL24(rtablexrim+224); t=x; x=y; y=t; /* swap pointers */ /* Last Round - unroll if possible */ for (m=j=0;j8) ROTL16(WORD)rbsub(BYTE)(xrim+116) ROTL24(WORD)rbsubxrim+224); for (i=j=0;iNb;i+,j+=4) unpack(yi,(BYTE *)&buffj); xi=yi=0; /* clean up stack */ return;int main() /* test driver */ int i,nb,nk; char s
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 铁路行车规章课件-掌握信号显示相关规定
- 2026年机械员之机械员专业管理实务练习题包完整版附答案详解
- 2026年全国中心血站上岗证测试卷含完整答案详解(夺冠)
- 2026年幼儿园乘电梯
- 2026年幼儿园放学排队的
- 2026年幼儿园我会坚持
- 2025福建福州城市泊车管理有限公司招聘2人笔试参考题库附带答案详解
- 2025福建泉州文旅集团第二批招聘17人笔试参考题库附带答案详解
- 2025神木市选聘高校毕业生到非公企业工作(75人)笔试参考题库附带答案详解
- 2025湖南省君山农垦集团有限公司劳务派遣人员招聘4人笔试参考题库附带答案详解
- 共用支架施工方案
- 村镇建设科培训课件
- 药物作用机制-洞察及研究
- 物业营销溢价方案(3篇)
- 2025年浙江省中考科学试题卷(含答案解析)
- 太原市城市供热收费管理办法
- 白癜风诊疗共识(2025版)解读
- T/CHES 89-2022河湖生态流量保障实施方案编制技术导则
- 青岛2025年自主招生考试物理试卷试题及答案详解
- 四川省旅游景区精细化管理服务质量提升规范 DB51 -T 2878-2022
- 2025年河南工业贸易职业学院单招职业技能考试题库及答案1套
评论
0/150
提交评论