2012加密程序的编写.docVIP

  • 10
  • 0
  • 约 6页
  • 2016-12-09 发布于北京
  • 举报
2012加密程序的编写

实验五 加密程序的编写 【实验目的】 1. 简单加密方法的原理 2. 凯撒密码的原理及程序的编写 【实验开设方式】 个人 【实验设备与环境】 1 连网的个人计算机 2 Windows 2000 系统平台 【相关知识点】 凯撒密码就是单表代替密码,它的每一个明文字符都由其右边第3个(模26)字符代替(A由D代替,B由E代替,W由Z代替,X由A代替,Y由B代替,Z由C代替)。 【CAESAR密码几种变换】 (1)加法变换 c≡ (m + k) mod 26 其中m是明文对应的数据,c是与明文对应的密文数据,k是加密用的参数,叫 密钥。 比如:data security对应数据序列4,1,20,1,19,5,3,21,18,9,20, 25,当k=5时,得密文序列9,6,25,6,24,10,8,0,23,14,25,4。 (2)乘同余码: 移位或等间隔抽取码,明密文之间没有一一对应关系。(容易产生多义性)。 变换按照同余乘法进行: 加密变换:C=P′k (mod 26) 解密变换:P=C?k (mod 26) 密钥:k 【实验内容:】 1.请同学们采用一种你熟悉的语言编写通过凯撒密码原理实现的加密器。 要求: 对文档中的任意几个字符能做正确的加密变换。 加密器做加法变换后仍然可以正确加密。 2.通过凯撒密码的原理实现解密器。 【实验方法步骤】 程序: #includestdio.h #includeconio.h char encrypt(char ch,int n)/*加密函数,把字符向右循环移位n*/ { while(ch=Ach=Z) { return (A+(ch-A+n)%26); } while(ch=ach=z) { return (a+(ch-a+n)%26); } return ch; } void menu()/*菜单,1.加密,2.解密,3.暴力破解,密码只能是数字*/ { clrscr(); printf(\n=========================================================); printf(\n1.Encrypt the file); printf(\n2.Decrypt the file); printf(\n3.Force decrypt file); printf(\n4.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]; textbackground(BLACK); textcolor(LIGHTGREEN); clrscr(); sleep(3);/*等待3秒*/ menu(); ch0=getch(); while(ch0!=4) { if(ch0==1) { clrscr(); printf(\nPlease input the infile:); scanf(%s,infile);/*输入需要加密的文件名*/ 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);/*输入加密密码*/ 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); } while(!feof(in))/*加密*/ { fputc(encrypt(fgetc(in),n),out); } printf(\nEncrypt is over!\n); fclose(in); fclose(out); sleep(1); } if(ch0==2) { clrscr(); printf(\nPleas

文档评论(0)

1亿VIP精品文档

相关文档