《Diffie-Hellman密钥交换综合实验报告.docx》由会员分享,可在线阅读,更多相关《Diffie-Hellman密钥交换综合实验报告.docx(10页珍藏版)》请在第一文库网上搜索。
1、(网络安全方案设计基础)实验报告#代码可运行时间:2021年 5月 12日 1-2节地点:软件实验室姓名班级学号指导教师实险成绩小李物联网18-1180*实验名称Diffie-Hellman密钥交换算法实验目的1、了解和掌握Diffie-Hellman算法原理及过程;2、通过密钥交换算法实现对称加解密密钥的保密传输;3、能够编写代码实现Diffie-Hellman算法,并实现数据保密传输。实验内容1、编写Diffie-Hellman程序,协商出用于收、发双方的加、解密密钥K;2、模拟发送端A,用协商出的密钥K,通过对称加密算法(具体算法不限)将文本文件加密;模拟接收端B,用协商出的密钥K,对接
2、收到的密文解密。实验过程#include #include /*函数声明*/void InitSbox(unsigned char sbox);void KeyExpansion(unsigned char key, char *k, int len);void UpsetSbox(unsigned char sboxfL unsigned char keyfl);void DataProcess(unsigned char sbox, FILE *fpl, FILE *fp2);void DataEncrypt(char *k, unsigned char *key, unsigned ch
3、ar *sbox, FILE *fpl, FILE *void DataDecrypt(char *kl, unsigned char *key, unsigned char *sbox, FILE *fpl, FILEint wrile_file(char name,char dataJnt size);int read_file(char namefLchar data,int size);int usera();int userb();int Xa = 3, Xb = 5, Ya, Yb;/*初始化S盒*/void InitSbox(unsigned char sboxf)fp2);*f
4、p2);int i;for( i = 0; i 256; i+) sboxfi = i;)/*密钥填充256数组*/void KeyExpansion(unsigned char key, char *k, int len)int i;if(len = 256) for( i = 0; i 256)(for( i = 0; i 256; i+) keyfi = ki;)/*打乱S盒*/void UpsetSbox(unsigned char sboxfL unsigned char keyl)int j = 0,i;unsigned char temp;int n;for( i = 0; i
5、256; i+)n = j + (int)sboxi + (int)keyfi;j = n% 256;temp = sboxij;sboxi = sboxj;sboxfj = temp;)/*加解密数据*/void DataProcess(unsigned char sbox, FILE *fpl, FILE *fp2)/0 加密,1 解密int i=0, j=O,size=O;int temp2,templet;char k,cipherchar,data_Stream 100J= 0x00;unsigned char temp;char ch = fgetc(fpl);while(ch !
6、= EOF)printf(”c”,ch);i = (i+ 1)%256;temp2 = j + (int)sboxi;j = temp2 % 256;temp = sboxfi;sboxi = sboxj;sbox|jj = temp;tempi = (int)sboxi + (int)sboxj;t = tempi % 256;data_Streamfsize+= k = sboxft;cipherchar = ch A k;fputc(cipherchar, fp2);ch = fgetc(fpl);)write.f le(”Stream. txt,data_Stream,size);)/
7、*加密总函数*/void DataEncrypt(unsigned char *k, unsigned char *key, unsigned char *sbox, FILE *fpl, FILE*fp2) int len = strlen(k);KeyExpansion(key, k, len);InitSbox(sbox);UpsetSbox(sbox, key);printf(”n 明文为:);DataProcess(sbox, fpl, fp2);fclose(fpl);fclose(fp2);printf(”n 加密成功,加密内容存于 “Ciphertext.txt” 中nn”);
8、)/*解密总函数*/void DataDecrypt(unsigned char *kl, unsigned char *key, unsigned char *sbox, FILE *fpl,FILE *fp2) int len = strlen(kl);KeyExpansion(key, kl, len);InitSbox(sbox);UpsetSbox(sbox, key);printf(n密文为:为DataProcess(sbox, fpl, fp2);fclose(fpl);fclose(fp2);printf(n 解密成功,解密内容存于 Plaintext.txt” 中!nn);)
9、/*文件的写操作 */int write_file(char name,char data,int size)FILE *file=fopen(name,ab+);if(file=NULL)return 0;fwrite(data,sizeof(char),size,file);/写入一个数组rewind(file); 移动指针到开头fclose(file);return 1;/*文件的读操作*/int read_file(unsigned char name,char dataJ,int size)FILE *file=fopen(name,rb4-u);if(file=NULL)retur
10、n 0;rewind(file); 移动指针到开头fread(data,sizeof(char),size,file); 读出一个数组fclose(file);return 1;int main(int argc, const char * argv) int q = 11, alpha = 2;unsigned char k 1 res ;unsigned char k2res ;/*rC4 变量*/unsigned char key 125J= 0x00 ,key225= 0x00;unsigned char key256 = 0x00);unsigned char sbox256 = 0
11、x00;FILE*fpl,*fp2;int flag = l,i;/*A 端操作*/printf( * A 立帚操作 *printf(q=l 1, a=2, Xa=3 n);/计算YaYa = pow(alpha,Xa);Ya = Ya % q;printf(计算得出:Ya=(a)人Xa mod q = %dnYa);printf(把 Ya 发送给 B 端.n);/*B 端操作*/pr* * *b M * *n) printf(nq=ll,a=2, Xb=5 nu);计算YbYb = pow(alpha,Xb);Yb = Yb % q;printf(计算得出:Yb二(aXb mod q = %
12、dnYb);printf(把 Yb 发送给 A 端.nn);/*Ab &带l|攵至ij数据后解密*/printf(*AB 端计算秘钥结果*n”);/把Yb给程序A并解出秘钥k 1 res = usera(alpha,q);把Ya给程序B并解出秘钥k2res = userb(alpha,q);key l0=k Ires;key20=k2res;printf(HnA程序收到Yb后计算,得到秘钥Ka:%dH,klres);printf(MnB程序收到Ya后计算,得到秘钥Kb:%dn,klres);/*rC4 加密*/* * * 力口 窖 */printf(n*A 端进彳亍 RC4 加密*n)fpl
13、= fopen(Source.txt;T);if(fpl = NULL)printf(打开源文件失败!n);getchar();exit(O);)fp2 = fopen(,Ciphertext.txt,wH);if(fp2 = NULL)printf(打开加密后文件失败!n);getchar();exit(O);printf(利用Ka加密进行加密)DataEncrypt(key 1, key, sbox, fpl, fp2);/* 解密 */printf(*B 端进行 RC4 解密*n)fpl = fopen(nCiphertext.txt,nr);if(fpl =NULL)printf(打开加密后文件失败!n);getchar();exit(O);fp2 = fopen(,Plaintext.txt,