1、 电气信息学院 实 验 报 告 书 课 程 名 称 数据结构 实 验 项 目 密码的原理与实现 专 业 班 组 通信202班 实 验 时 间 2016.12.12 成 绩 评 定 评 阅 老 师 报告撰写人: 学号:
2、 电气信息学院专业中心实验室 一、实验内容 利用Visual C++设计合理的凯撒密码,对一段英文段落进行加密处理。以每个人学号的最后两位为key。 英文段落如下: I am Wang Yanling,a student from Sichuan University.Now I major in telecommunication engineering.I am nineteen years old and my family live in Sichuan province.It is the second
3、year of my colleage and I am fighting for my future.I also enjoy playing games,listening to some beautiful music and going out for a date with my friends.How wonderful it is.
二、算法流程图
三、详细设计
源程序:
#include
4、移位n*/ { while(ch>='A'&&ch<='Z') { return ('A'+(ch-'A'+n)%26); } while(ch>='a'&&ch<='z') { return ('a'+(ch-'a'+n)%26); } return ch; } void menu()/*菜单,1.加密,2.解密,3.退出*/ { 设计加密函数 printf("\n----------------------"); printf("\n1.Encrypt the file"); printf("\n2.Decrypt the file"); printf(
5、"\n3.Quit\n"); printf("------------------------\n"); printf("Please select a item:"); return; } 创建菜单 main() { int i,n; char ch0,ch1; FILE *in,*out; char infile[20],outfile[20]; menu(); ch0=getch(); while(ch0!='3') { if(ch0=='1') { printf("\nPlease input the infile:"); 键入数字1
6、则表示选择功能:加密文件 scanf("%s",infile);/*输入需要加密的文件名*/ 输入要加密的TXT地址 if((in=fopen(infile,"r"))==NULL) { printf("Can not open the infile!\n"); printf("Press any key to exit!\n"); getch(); exit(0); } 不能打开时提示 printf("Please input the key:"); scanf("%d",&n);/*输入加密密码*/ 打开
7、成功后进入下一步:输入加密密码 printf("Please input the outfile:"); scanf("%s",outfile);/*输入加密后文件的文件名*/ 输入加密后文件储存地址 if((out=fopen(outfile,"w"))==NULL) { printf("Can not open the outfile!\n"); printf("Press any key to exit!\n"); fclose(in); getch(); exit(0); 地址无效则提示结束程序 }
8、 while(!feof(in))/*加密*/ { fputc(encrypt(fgetc(in),n),out); } printf("\nEncrypt is over!\n"); fclose(in); fclose(out); 加密成功 } if(ch0=='2') { printf("\nPlease input the infile:"); scanf("%s",infile);/*输入需要解密的文件名*/ 键入数字2则表示选择功能:解密文件 if((in=fopen(infile,"r")
9、)==NULL) { printf("Can not open the infile!\n"); printf("Press any key to exit!\n"); getch(); exit(0); } 不能打开时提示 printf("Please input the key:"); scanf("%d",&n);/*输入解密密码(可以为加密时候的密码)*/输入密码 n=26-n; printf("Please input the outfile:"); scanf("%s",outfile);/*输入解密后文件
10、的文件名*/ 输入解密后文件的存放地址 if((out=fopen(outfile,"w"))==NULL) { printf("Can not open the outfile!\n"); printf("Press any key to exit!\n"); fclose(in); getch(); exit(0); } 输入地址无效时提示结束程序 while(!feof(in)) { fputc(encrypt(fgetc(in),n),out); } printf("\nDecrypt is over!\
11、n");
fclose(in);
fclose(out);
}
menu();
ch0=getch();
}
printf("\nGood Bye!\n");
}成功得到解密后结果
四、调试分析
1、出现错误
检查后发现是未引入头文件stdlib.h造成。 添加#include
12、算法是加解密算法的一种选择。它最灵活的选择之一是使用对称加密。在这种技术中,加密和解密使用相同的密钥。公钥加密的最大缺点是其性能较差。对称算法对处理能力的要求比公钥算法低得多。如果只是使用加密方法,则不必详细了解其工作原理;但如果要对凯撒加解密算法进行设计,则需要我们考虑以下三个主要因素: ①破解使用该算法加密的消息的难度; ②算法的性能 ; ③密钥的安全性。 我们对凯撒加解密算法进行了分析与研究,了解了怎样的密码体制才能充分发挥凯撒加解密算法的安全作用。分析了凯撒加解密的安全性,以及如何选取凯撒加解密时的密钥长度的问题。同时考虑到在实际的应用过程中,在满足安全性前提下应当降低计算
13、的复杂度,提高信息加、解密的速度。为了得到较快的加/解密速度,主要采用移位的方法,大大提高了凯撒加解密算法实际应用的运算速度和执行效率。 以上详细介绍了凯撒加解密字母的实现方法,同时,也介绍了采用加解密密码方式以后,以及用户鉴别的实现。并讨论了使用这种加解密方式的应用限制。在实际应用中,我们可以将此方法做适当的修改和补充,以更加适合我们的应用需要。 这次课程设计让我学到了很多,完成了此次的课程设计的要求,不仅巩固了先前学的程序设计知识,而且也培养了我的动手能力,更令我的创造性思维得到拓展,编写这些程序很伤大脑,但在编写好程序运行出所想要的程序这使我有了一种成就感,相信只要自己努力去做过,就可以把它做好,所以我懂得了,做任何事只要下恒心,一定就能做好,只要功夫深,铁杵磨成针。






