免费预览已结束,剩余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教资面试必练考题题库及答案
- 老年吞咽障碍护理课件
- 文水县2025届四下数学期末复习检测试题(含解析)
- 2026年糕点制作(月饼皮配方调整)试题及答案
- 2026高职(建筑工程技术)钢筋绑扎施工资格考试试题及答案
- 2026年中职(园林工程)园林施工资格考试试题及答案
- 老年护理师资培训课件
- 老年病人基础护理课件
- 人工皮革项目可行性研究报告
- PTP1B-IN-33-生命科学试剂-MCE
- 2026年党支部基层总结报告
- 2026秋小学教科版科学五年级上册第二单元测试练习卷含参考答案
- 2026中国邮政社会招聘笔试真题(完整版+详细答案解析)
- WorkBuddy-HR实战场景AI手册-AI+智能体赋能HR+六大模块·从入门到实战
- CSCO非小细胞肺癌诊疗指南(2026版)
- 小学语文盲校低视力版四年级上册《盘古开天地》融合教学设计
- 2026年小学教师资格证《教育教学知识与能力》真题及答案解析
- 项目经理团队协作情况评估表
- 2026年公司有限空间作业安全管理制度
- 门店垃圾分类管理操作规范
- 管道修复工程验收规范与标准
评论
0/150
提交评论