当前位置:文档之家› 信息安全实验-凯撒加密加法乘法变换

信息安全实验-凯撒加密加法乘法变换

【实验目的】1. 简单加密方法的原理2. 凯撒密码的原理及程序的编写【实验设备与环境】(1)计算机(2)TC【实验步骤(内容)】凯撒密码就是单表代替密码,它的每一个明文字符都由其右边第3个(模26)字符代替(A由D代替,B由E代替,W由Z代替,X由A代替,Y由B代替,Z由C代替)。

(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源程序:#include<stdio.h>#include<conio.h>char sumjiami(char ch,int n){n=n%10;while(ch>='A'&&ch<='Z'){return ('A'+(ch-'A'+n)%26);}while(ch>='a'&&ch<='z'){return ('a'+(ch-'a'+n)%26); }while(ch>='0'&&ch<='9'){return ('0'+(ch-'0'+n)%10); }return ch;}char sumjiemi(char ch,int n) {static int k;k=n%10;while(ch>='A'&&ch<='Z'){ n=26-k;return ('A'+(ch-'A'+n)%26); }while(ch>='a'&&ch<='z'){n=26-k;return ('a'+(ch-'a'+n)%26); }while(ch>='0'&&ch<='9'){ n=10-k;return ('0'+(ch-'0'+n)%10); }return ch;char muljiami(char ch,int n) {if(n%2==0) n=n%10+1;else n=n%10;if(n%5==0) n=n+2;else n=n;while(ch>='A'&&ch<='Z'){return ('A'+((ch-'A')*n)%26); }while(ch>='a'&&ch<='z'){return ('a'+((ch-'a')*n)%26); }while(ch>='0'&&ch<='9'){return ('0'+((ch-'0')*n)%10); }return ch;}char muljiemi(char ch,int n) {int i;int k,h;if(n%2==0) n=n%10+1;else n=n%10;if(n%5==0) n=n+2;else n=n;while(ch>='A'&&ch<='Z')for(i=0;i<=n;i++){k=((ch-'A')+i*26)%n; if(k==0)h=((ch-'A')+i*26)/n; if(h>=0&&h<=26)return ('A'+h);}}while(ch>='a'&&ch<='z') {for(i=0;i<=n;i++){k=((ch-'a')+i*26)%n; if(k==0)h=((ch-'a')+i*26)/n; if(h>=0&&h<=26)return ('a'+h);}}while(ch>='0'&&ch<='9') {for(i=0;i<=n;i++){k=((ch-'0')+i*10)%n; if(k==0)h=((ch-'0')+i*10)/n;return ('0'+h);}}return ch;}void menu(){printf("\n========================================================="); printf("\n1.sumjiami the file");printf("\n2.muljiami the file");printf("\n3.sumjiemi the file");printf("\n4.muljiemi the file");printf("\n5.Quit\n");printf("=========================================================\n"); printf("Please select a item:");return;}void main(){int i,n;char ch0,ch1;FILE *in,*out;char infile[20],outfile[20];sleep(3);menu();ch0=getch();{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(sumjiami(fgetc(in),n),out);}fclose(in);fclose(out);sleep(1);}if(ch0=='2'){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(muljiami(fgetc(in),n),out);}printf("\nmuljiami is over!\n");fclose(in);fclose(out);sleep(1);}if(ch0=='3'){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();}while(!feof(in)){fputc(sumjiemi(fgetc(in),n),out);}printf("\nsumjiemi is over!\n");fclose(in);fclose(out);sleep(1);}if(ch0=='4'){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");fclose(in);getch();exit(0);}while(!feof(in)){fputc(muljiemi(fgetc(in),n),out);}printf("\nmuljiemi is over!\n");fclose(in);fclose(out);sleep(1);}menu();ch0=getch();}clrscr();printf("\nGood Bye!\n");sleep(3);}运行:在C盘建立1.txt和2.txt,在文本1中输入以下信息:大写:ABCDEFGHIJKLMNOPKRSTUVWXYZ;小写:abcdefghijklmnopkrstuvwxyz;数字:0123456789.1、加法(sum)加密选择1加密运行结果:大写:FGHIJKLMNOPQRSTUPWXYZABCDE; 小写:fghijklmnopqrstupwxyzabcde;数字:5678901234.2、加法(sum)解密选择3加法解密运行结果:大写:ABCDEFGHIJKLMNOPKRSTUVWXYZ; 小写:abcdefghijklmnopkrstuvwxyz;数字:0123456789.3、乘法(mul)加密选择2乘法加密运行结果:大写:AHOVCJQXELSZGNUBSPWDKRYFMT; 小写:ahovcjqxelszgnubspwdkryfmt;数字:0741852963.4、乘法解密选择4乘法解密运行结果:大写:ABCDEFGHIJKLMNOPKRSTUVWXYZ;小写:abcdefghijklmnopkrstuvwxyz;数字:0123456789.5选择5退出。

相关主题