黄汉杰网络安全实验2.docVIP

  • 0
  • 0
  • 约2.43千字
  • 约 7页
  • 2016-12-11 发布于江西
  • 举报
海南大学信息科学技术学院 实 验 报 告 实验课程: 计算机网络安全技术实验名称:简单密码加解密算法程序设计学号: 310035 姓名: 黄汉杰班级: 计算机2班同组成员:完成日期:2013 年 6 月 2 日 实验目的 理解网络安全、加密/解密的概念 掌握几个典型古典加解密算法的应用 实验任务 使用凯撒和维吉尼亚加密算法编程实现将明文转换成密文。 使用凯撒和维吉尼亚解密算法编程实现将密文转换成明文。 实验环境 安装Windows9x/2000/XP/2003、W7操作系统的PC机;编程环境:Visual c++; 四、实验内容 凯撒密码 #includestdio.h void main() { char p[1000]; char c[1000]; int key,i; key=5;/*定义一个移位个数为5,即加密向右移5位,解密则相反*/printf(please input the Plaintext: \n);gets(p);/*读取字符串,直至接受到换行符停止,并将读取的结果存放在p[1000]中。换行符不作为读取串的内容,读取的换行符被转换为null值,并由此来结束字符串。*/for(i=0;p[i]!=\0;i++)c[i]=(p[i]-a+key)%26+a;/*加密算法,所有明文符号由向右移5位的密文代替*/c[i]=\0;printf(the Ciphertext is:\n%s\n,c);/*输出加密后的密文*/printf(please input the Ciptertext:\n);gets(c);/*读取字符串,直至接受到换行符停止,并将读取的结果存放在c[1000]中。换行符不作为读取串的内容,读取的换行符被转换为null值,并由此来结束字符串。*/for(i=0;c[i]!=\0;i++)p[i]=z-(z+key-c[i])%26;/*解密算法,所有明文符号由向左移5位的明文代替*/p[i]=\0;printf(the Plaintext is:\n%s\n,p);/*输出解密后的明文*/ return; }维吉尼亚密码 #include stdlib.h #include stdio.h #include string.h void Encry() { char key[100]; char ch,temp; int L,i=0,j=0; if(getchar()==\n)temp= ; printf(请输入密钥: ); gets(key); L=strlen(key); printf(输入明文: ); while((ch=getchar())!=\n) { if(ch== ) {i++;continue; } if(ch=ach=z) {printf(%c,(ch+key[j%L]-a-a)%26+A);j++; } if(ch=Ach=Z) {printf(%c,(ch+key[j%L]-A-a)%26+A);j++; } if(j%L==0)printf( ); i++; } putchar(ch); } void Decry() { char key[100]; char ch,temp; int L,i=0,j=0; if(getchar()==\n)temp= ; printf(请输入密钥: ); gets(key); L=strlen(key); printf(输入密文: ); while((ch=getchar())!=\n) { if(ch== ) {i++;continue; } if(ch=Ach=Z) {printf(%c,(ch-key[j%L]-A+a+26)%26+a);j++; } if(j%L==0)printf( ); i++; } putchar(ch); } int Exit() {exit(0); } int main() { char ch; for(;;) {printf(请输入你的操作(e/E加密;d/D解密;q/Q退出):);ch=getchar();if(ch==e||ch==E)Encry();else if(ch==d||ch==D)Decry();else if(ch==q||ch==Q)Exit();else{printf(输入命令错误!);putchar(getchar());continue;} } return 0; }实验总结 凯撒密码是密码学中,一种最简单的加密算法。原理并不复杂,只要理解了,实现起来并不困难。程序并不长,

文档评论(0)

1亿VIP精品文档

相关文档