已阅读5页,还剩11页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
#include stdafx.h#include #include #include #include using namespace std;#define BYTE unsigned charint power(int x,int n)/xn if (n=0) return 1; else return power(x,n-1)*x;/-output the binary stream-void ByteStream32(unsigned long HexValue) for (int n=0;n32;n+) cout(31-n)&1); void ByteStream8(unsigned char HexValue) cout7)&1); cout6)&1); cout5)&1); cout4)&1); cout3)&1); cout2)&1); cout1)&1); cout0)&1);void ByteStream6(unsigned char HexValue) cout5)&1); cout4)&1); cout3)&1); cout2)&1); cout1)&1); cout0)&1);void ByteStream4(unsigned char HexValue) cout3)&1); cout2)&1); cout1)&1); cout0)&1);/-above:output the binary Stream-const static char S_Box_1416=/ Sbox114, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13;const static char S_Box_2416=/ S215, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9;const static char S_Box_3416=/ S310, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12;const static char S_Box_4416=/ S47, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14;const static char S_Box_5416=/ S52, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3;const static char S_Box_6416=/ S612, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13;const static char S_Box_7416=/ S74, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12;const static char S_Box_8416=/ S813, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11;unsigned char sbox1(unsigned char HexValue) if (HexValue6)0) coutinvalide Sbox1 input5)&1)1)&0xF;unsigned char sbox2(unsigned char HexValue) if (HexValue6)0) coutinvalide Sbox2 input5)&1)1)&0xF;unsigned char sbox3(unsigned char HexValue) if (HexValue6)0) coutinvalide Sbox3 input5)&1)1)&0xF;unsigned char sbox4(unsigned char HexValue) if (HexValue6)0) coutinvalide Sbox4 input5)&1)1)&0xF;unsigned char sbox5(unsigned char HexValue) if (HexValue6)0) coutinvalide Sbox5 input5)&1)1)&0xF;unsigned char sbox6(unsigned char HexValue) if (HexValue6)0) coutinvalide Sbox6 input5)&1)1)&0xF;unsigned char sbox7(unsigned char HexValue) if (HexValue6)0) coutinvalide Sbox7 input5)&1)1)&0xF;unsigned char sbox8(unsigned char HexValue) if (HexValue6)0) coutinvalide Sbox8 input5)&1)1)&0xF;void expansion (unsigned char beforeE4,unsigned char* afterE)/beforeE is the input to the expansion before sboxes/afterE is a pointer to a array of 8 elements,each of them / is a unsigned char afterE0=(beforeE03)(beforeE3&0x1)5); afterE1=(beforeE02) & 0x3f)(beforeE17); afterE2=(beforeE13)(beforeE0&0x1)5); afterE3=(beforeE12) & 0x3f)(beforeE27); afterE4=(beforeE23)(beforeE1&0x1)5); afterE5=(beforeE22) & 0x3f)(beforeE37); afterE6=(beforeE33)(beforeE2&0x1)5); afterE7=(beforeE32) & 0x3f)(beforeE07);void P(unsigned char table4,unsigned char* afterP)/table4 is the input to the permutation/afterPE is a pointer to a array of 4 elements,each of them / is a unsigned char unsigned long line1=(table08)table1)8)table2)24); Print1=(line124); Print2=(line124); Print3=(line124);/ ByteStream8(Print0);cout-;/ ByteStream8(Print1);cout-;/ ByteStream8(Print2);cout-;/ ByteStream8(Print3);coutendl; / int index32= 16, 7,20,21,29,12,28,17, 1 ,15,23,26, 5,18,31,10, 2 , 8,24,14,32,27, 3, 9, 19,13,30, 6,22,11, 4,25 ; unsigned long line2=0x0; for (int i=0;i32;i+) line2=(line21)(line131)&0x1); /very interesting! :) afterP0=(line224); afterP1=(line224); afterP2=(line224); afterP3=(line224); /construct the F function!(have not been tested!)void F(unsigned char inHex4,unsigned char KeyHex8,unsigned char* outHex)/unsigned char outHex4 unsigned char inHexE8=0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0; expansion(inHex,inHexE); coutinHexE Testing:endl; ByteStream8(inHexE0);coutendl; ByteStream8(inHexE1);coutendl; ByteStream8(inHexE2);coutendl; ByteStream8(inHexE3);coutendl; ByteStream8(inHexE4);coutendl; ByteStream8(inHexE5);coutendl; ByteStream8(inHexE6);coutendl; ByteStream8(inHexE7);coutendl; cout-end inHexE Testing-endl; coutAdd Key Testing:endl; for (int i=0;i8;i+) inHexEi=inHexEiKeyHexi; /add the subkey ByteStream8(inHexEi);coutendl; coutend test add keyendl; unsigned char outS8=0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0; outS0=sbox1(inHexE0); outS1=sbox2(inHexE1); outS2=sbox3(inHexE2); outS3=sbox4(inHexE3); outS4=sbox5(inHexE4); outS5=sbox6(inHexE5); outS6=sbox7(inHexE6); outS7=sbox8(inHexE7); unsigned char beforeP4=0x0,0x0,0x0,0x0; beforeP0=(outS04)outS1; beforeP1=(outS24)outS3; beforeP2=(outS44)outS5; beforeP3=(outS64)outS7; P(beforeP,outHex); /const static unsigned char EncryptionKey8=0x1A,0x62,0x4C,0x89,0x52,0x0D,0xEC,0x46;/Key used in the 3-round DES encryption/odd 1,even 0,pairity check! the last bit of each element:/ 00011010/ 01100010/ 01001100/ 10001001/ 01010010/ 00001101/ 11101100/ 01000110const int PC1_Table56= 57,49,41,33,25,17, 9, 1,58,50,42,34,26,18, 10, 2,59,51,43,35,27, 19,11, 3,60,52,44,36, 63,55,47,39,31,23,15, 7,62,54,46,38,30,22, 14, 6,61,53,45,37,29, 21,13, 5,28,20,12, 4;/K-S-A/void PC1(unsigned char SeedKey8,unsigned long* afterPC1)/KSA/afterPC1 is a pointer to a two elements array/each element is a 32-bit data(only 28 bits are used) int tempstream64; for (int i=0;i64;i+) tempstreami=(SeedKeyi/87)&0x1); int afterstream56; for (int j=0;j56;j+) afterstreamj=tempstreamPC1_Tablej-1; afterPC10=0x0; afterPC11=0x0; for (int k=0;k28;k+) afterPC10=(afterPC101)afterstreamk; for (int l=28;l56;l+) afterPC11=(afterPC1128)0)coutinput is not a 28-bit value!; /input value check return (HexValue28)(HexValue1) & 0xfffffff);const int PC2_Table48= 14,17,11,24, 1, 5, 3,28, 15, 6,21,10,23,19,12, 4, 26, 8,16, 7,27,20,13, 2, 41,52,31,37,47,55,30,40, 51,45,33,48,44,49,39,56, 34,53,46,42,50,36,29,32 ;/置换选择2产生8个元素的数组,每个元素六位,每个元素用一个Byte储存void PC2(unsigned long array2,unsigned char* afterPC2)/|arrayi|=28 int table48= 14,17,11,24, 1, 5, 3,28, 15, 6,21,10,23,19,12, 4, 26, 8,16, 7,27,20,13, 2, 41,52,31,37,47,55,30,40, 51,45,33,48,44,49,39,56, 34,53,46,42,50,36,29,32 ; int tempstream48; for (int i=0;i48;i+) if(i23) tempstreami=(arrayi/2427)&0x1); else tempstreami=(arrayi/2427)&0x1); /下面我们的工作是把这48位装入8个Byte中,每个Byte被占用6位 afterPC20=0x0; for(int k0=0;k0=5;k0+) afterPC20=(afterPC201)tempstreamk0; afterPC21=0x0; for(int k1=6;k1=11;k1+) afterPC21=(afterPC211)tempstreamk1; afterPC22=0x0; for(int k2=12;k2=17;k2+) afterPC22=(afterPC221)tempstreamk2; afterPC23=0x0; for(int k3=18;k3=23;k3+) afterPC23=(afterPC231)tempstreamk3; afterPC24=0x0; for(int k4=24;k4=29;k4+) afterPC24=(afterPC241)tempstreamk4; afterPC25=0x0; for(int k5=30;k5=35;k5+) afterPC25=(afterPC251)tempstreamk5; afterPC26=0x0; for(int k6=36;k6=41;k6+) afterPC26=(afterPC261)tempstreamk6; afterPC27=0x0; for(int k7=42;k7=47;k7+) afterPC27=(afterPC271)tempstreamk7; void main() cout*endl; cout*Differentail attack*endl; cout*endl; coutendl; cout*endl; cout*Caculation of IN_1*endl; cout*endl; coutendl; for (int delta=0;delta=0x3f;delta+) /delta是输入差分 for (int outdiff=0;outdiff=0xf;outdiff+) coutinput differential:; ByteStream6(delta);cout(hexdelta);cout; ByteStream4(outdiff);cout(hexoutdiff);coutendl; for (int ins=0;ins=0x3F;ins+) if (sbox1(ins)sbox1(insdelta)=outdiff) /* ByteStream6(ins);*/cout(hexins); /* coutendl;*/ coutendl; coutendl; cout*endl; cout*endl; coutendl; cout*endl; cout*Expansion testing*endl; cout*endl; coutendl; unsigned char Etest8; unsigned char before4=0x20,0x00,0x00,0x00; expansion (before,Etest); ByteStream8(before0);cout-; ByteStream8(before1);cout-; ByteStream8(before2);cout-; ByteStream8(before3);coutendl; ByteStream6(Etest0);cout-; ByteStream6(Etest1);cout-; ByteStream6(Etest2);cout-; ByteStream6(Etest3);cout-; ByteStream6(Etest4);cout-; ByteStream6(Etest5);cout-; ByteStream6(Etest6);cout-; ByteStream6(Etest7);coutendl; coutendl; cout*endl; cout*Permutation P test*endl; cout*endl; coutendl; unsigned char Ptest4; unsigned char beforeP4=0x00,0x00,0x00,0x01; ByteStream8(beforeP0);cout-; ByteStream8(beforeP1);cout-; ByteStream8(beforeP2);cout-; ByteStream8(beforeP3);coutendl; P(beforeP,Ptest); ByteStream8(Ptest0);cout-; ByteStream8(Ptest1);cout-; ByteStream8(Ptest2);cout-; ByteStream8(Ptest3);coutendl; coutendl; cout*endl; cout* K-S-A testing *endl; cout*endl; coutendl; cout*endl; cout* PC-2 testing *endl; cout*endl; coutendl; unsigned long testvector2=0xffffffff,0x0; int liu48; for (int m=0;m48;m+) if (m23) lium=(testvectorm/2427)&0x1); coutlium; else lium=(testvectorm/2427)&0x1); coutlium; coutendl; coutendl; unsigned char store8; PC2(testvector,store); coutendl; ByteStream6(store0);coutendl; ByteStream6(store1);coutendl; ByteStream6(store2);coutendl; ByteStream6(store3);coutendl; ByteStream6(store4);coutendl; ByteStream6(store5);coutendl; ByteStream6(store6);coutendl; ByteStream6(store7);coutendl; coutendl; cout*endl; cout*Construct the system*endl; cout*endl; coutendl; unsigned char EncryptionKey8=0x1A,0x62,0x4C,0x89,0x52,0x0D,0xEC,0x46; coutEncryptionKey:endl; for (int t=0;t8;t+)ByteStream8(EncryptionKeyt);coutendl; coutendl; unsigned long KeyPC12=0x0,0x0; PC1(EncryptionKey,KeyPC1); coutKeyPC10:;ByteStream32(KeyPC10);coutendl; coutKeyPC11:;ByteStream32(KeyPC11);coutendl; unsigned long KeyLC2=0x0,0x0; KeyLC0=leftcycle(KeyPC10); KeyLC1=leftcycle(KeyPC11); coutKeyLC0 :;ByteStream32(KeyLC0);coutendl; coutKeyLC1 :;ByteStream32(KeyLC1);coutendl; unsigned char KeyPC28=0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0; PC2(KeyLC,KeyPC2); unsigned char SubKey18=0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0; for (int q=0;q8;q+) SubKey1q=KeyPC2q; coutendl; for (int s=0;s8;s+)coutSubKey1s:;ByteStream6(SubKey1s);coutendl; coutendl; unsigned long KeyLC22=0x0,0x0; KeyLC20=leftcycle(KeyLC0); KeyLC21=leftcycle(KeyLC1); unsigned long KeyLC32=0x0,0x0; KeyLC30=leftcycle(leftcycle(KeyLC20); KeyLC31=leftcycle(leftcycle(KeyLC21); unsigned char SubKey38=0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0; PC2(KeyLC3,SubKey3); for (int u=0;u8;u+)coutSubKey
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 高校学生心理健康教育讲座教材
- 建筑施工现场危险源辨识手册
- 教师结对帮扶个人心得及体会
- 食品加工扩建项目可行性研究报告
- 体育课程与学生综合素质培养
- 2025年文山砚山县阿舍乡卫生院第十七期招聘(5人)考试笔试模拟试题及答案解析
- 2025四川凉山州西昌市东城社区卫生服务中心招聘编外卫生专技人员3人考试笔试备考试题及答案解析
- 2025年河北体育学院选聘工作人员15名(第二批)考试笔试备考题库及答案解析
- 小学四年级古诗词赏析与练习
- 2025广东省能源集团有限公司管道公司、惠州天然气开发公司招聘4人笔试考试备考题库及答案解析
- 博士研究生中期考核课件
- (完整版)保安培训课件
- 智能信贷系统应用-深度研究
- 信息安全技术保障措施
- 养殖场巡查制度模板
- 【MOOC】中医与辨证-暨南大学 中国大学慕课MOOC答案
- 中建深基坑工程土方开挖专项施工方案
- 《行业会计比较》教案
- 江苏省南通市海门区2024-2025学年八年级上学期11月期中考试(第二届学生学科素养竞赛)英语试卷
- 山东省菏泽市第一中学2025届高一物理第一学期期中质量检测试题含解析
- 公路工程劳务分包指导价
评论
0/150
提交评论