免费预览已结束,剩余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年小学成语故事《多多益善》故事赏析教学设计教案
- ISO 17987-7-2025 中文版 道路车辆 LIN 总线 物理层一致性测试规范
- 2026秋人教版小学数学三年级上册(新教材)教学计划含教学进度表
- 2026广东广州市南沙区黄阁镇人民政府招聘编外工作人员10人笔试参考题库及答案详解
- 2026年秋季学期苏科版四年级上册信息科技教学计划
- 2026年秋道德与法治六年级上册教学工作计划
- T∕CCEAS008-2026 建设工程造价咨询成果文件质量标准
- 启智润心安全成长-2026年五年级秋季开学第一课
- 2025年呼和浩特农商行招聘笔试真题(附答案)
- 2026年全国高考1卷高考语文真题(教师版)
- Unit1Differentfriends句型讲解(课件)-人教PEP版英语五年级上册
- 注浆堵漏施工方案安全措施与环境保护
评论
0/150
提交评论