凯撒密码是一种非常古老的加密方法,相传当年凯撒大地行军打仗时为了保证自己的命令不被敌军知道,就使用这种特殊的方法进行通信,以确保信息传递的安全。他的原理很简单,说到底就是字母于字母之间的替换。下面让我们看一个简单的例子:“baidu”用凯撒密码法加密后字符串变为“edlgx”,它的原理是什么呢?把“baidu”中的每一个字母按字母表顺序向后移3位,所得的结果就是刚才我们所看到的密文。/**凯撒密码实现要求,将明文字母变成它后面第三个字母,后面的循环到前面!公式为f(a)=(f(a)+3)%26*/#includestdio.hintmain(){charP[100];/*定义明文长度*/charC[100];/*定义密文长度*/intK=3,i;printf(PleaseinputPlaintext:\n);/*输入明文*/gets(P);/*接受明文*/for(i=0;P[i]!='\0';i++){/*逐个判断字母的大小*/if(P[i]='a'&&P[i]='z')/*小写字母*/C[i]=(P[i]-'a'+K)%26+'a';elseif(P[i]='A'&&P[i]='Z')/*大写字母*/C[i]=(P[i]-'A'+K)%26+'A';elseC[i]='';/*如果不是字母,转换为空格*/}printf(TheCiphertextis:\n%s\n,C);/*输出密文*/getch();return0;}1、程序结构化,用函数分别实现2、对文件的加密,解密输出到文件#includestdio.h#includeconio.hvoidmenu()/*菜单,1.加密2.解密3.退出*/{clrscr();printf(\n===============================================================================);printf(\n1.Encryptthefile);printf(\n2.Decryptthefile);printf(\n3.Quit\n);printf(===============================================================================\n);printf(Pleaseselectaitem:);return;}charencrypt(charch,intn)/*加密函数,把字符向右循环移位n*/{while(ch='A'&&ch='Z'){return('A'+(ch-'A'+n)%26);}while(ch='a'&&ch='z'){return('a'+(ch-'a'+n)%26);}returnch;}main(){inti,n;charch0,ch1;FILE*in,*out;charinfile[10],outfile[10];textbackground(RED);textcolor(LIGHTGREEN);clrscr();menu();ch0=getch();while(ch0!='3'){if(ch0=='1'){clrscr();printf(\nPleaseinputtheinfile:);scanf(%s,infile);/*输入需要加密的文件名*/if((in=fopen(infile,r))==NULL){printf(Cannotopentheinfile!\n);printf(Pressanykeytoexit!\n);getch();exit(0);}printf(Pleaseinputthekey:);scanf(%d,&n);/*输入加密密码*/printf(Pleaseinputtheoutfile:);scanf(%s,outfile);/*输入加密后文件的文件名*/if((out=fopen(outfile,w))==NULL){printf(Cannotopentheoutfile!\n);printf(Pressanykeytoexit!\n);fclose(in);getch();exit(0);}while(!feof(in))/*加密*/{fputc(encrypt(fgetc(in),n),out);}printf(\nEncryptisover!\n);fclose(in);fclose(out);sleep(1);}if(ch0=='2'){clrscr();printf(\nPleaseinputtheinfile:);scanf(%s,infile);/*输入需要解密的文件名*/if((in=fopen(infile,r))==NULL){printf(Cannotopentheinfile!\n);printf(Pressanykeytoexit!\n);getch();exit(0);}printf(Pleaseinputthekey:);scanf(%d,&n);/*输入解密密码(可以为加密时候的密码)*/n=26-n;printf(Pleaseinputtheoutfile:);scanf(%s,outfile);/*输入解密后文件的文件名*/if((out=fopen(outfile,w))==NULL){printf(Cannotopentheoutfile!\n);printf(Pressanykeytoexit!\n);fclose(in);getch();exit(0);}while(!feof(in)){fputc(encrypt(fgetc(in),n),out);}printf(\nDecryptisover!\n);fclose(in);fclose(out);sleep(1);}clrscr();printf(\nGoodBye!\n);sleep(3);getch();}}---------------------------------------------------------------------------------------------------------------------------------------------/*移位法:*/#includestdio.h#includestdlib.hchar*Encrypt(char*pwd,intkey)/*加密*/{for(inti=0;*(pwd+i)!='\0';i++){if(*(pwd+i)='a'&&*(pwd+i)='z')*(pwd+i)=(*(pwd+i)-'a'+key)%26+'a';elseif(*(pwd+i)='A'&&*(pwd+i)='Z')*(pwd+i)=(*(pwd+i)-'A'+key)%26+'A';}returnpwd;}char*Decrypt(char*pwd,intkey)/*解密*/{for(inti=0;*(pwd+i)!='\0';i++){if(*(pwd+i)='a'&&*(pwd+i)='z'){if(*(pwd+i)-'a'=key%26)*(pwd+i)=*(pwd+i)-key%26;else*(pwd+i)='z'-(key%26-(*(pwd+i)-'a'))-1;}elseif(*(pwd+i)='A'&&*(pwd+i)='Z'){if(*(pwd+i)-'A'=key%26)*(pwd+i)=*(pwd+i)-key%26;else*(pwd+i)='Z'-(key%26-(*(pwd+i)-'A'))-1;}}returnpwd;}voidmain(){char*pwd;intkey;pwd=(char*)malloc(sizeof(char));printf(Inputyourpassword:);gets(pwd);printf(Inputakey:);scanf(%d,&key);printf(TheCiphertextis:);printf(%s\n,Encrypt(pwd,key));}------------------------------------------------------------------/*替换法:*/#includestdio.h#includestdlib.h#includestring.hvoidtable(char*keyword)/*筛选密钥(去重复去空格)*/{inti,j,k;for(i=0;*(keyword+i)!='\0';i++){for(j=i;*(keyword+j)!='\0';j++){if(i!=j)if(*(keyword+i)==*(keyword+j)||*(keyword+j)==''){for(k=j;*(keyword+k)!='\0';k++)*(keyword+k)=*(keyword+k+1);j--;}}}}voidnewTab(char*keyword)/*生成密钥表*/{charch;inti;intt;for(t=0;*(keyword+t)!='\0';t++);for(ch='a';ch='z';ch++){for(i=0;*(keyword+i)!=ch;i++){if(*(keyword+i)=='\0'){*(keyword+t)=ch;t++;break;}}}*(keyword+t)='\0';}char*Ciphertext(char*keyword,char*Plaintext)/*按密码表加密*/{charch;inti,j;for(i=0;*(Plaintext+i)!='\0';i++){for(ch='a',j=0;ch='z';ch++,j++){if(*(Plaintext+i)==ch){*(Plaintext+i)=*(keyword+j);break;}}}returnPlaintext;}char*Decrypt(char*keyword,char*Plaintext)/*解密*/{charch;inti,j;for(i=0;*(Plaintext+i)!='\0';i++){for(ch='a',j=0;*(keyword+j)!='\0';ch++,j++){if(*(Plaintext+i)==*(keyword+j)){*(Plaintext+i)=ch;break;}}}returnPlaintext;}voidmain(){char*keyword,*Plaintext,*tmp=NULL;keyword=(char*)malloc(sizeof(char));Plaintext=(char*)malloc(sizeof(char));printf(Inputkeyword:);/*输入欲用密钥*/gets(keyword);printf(InputPlaintext:);/*输入要转换的明文*/gets(Plaintext);table(keyword);/*去空格去重复*/newTab(keyword);/*生成密码表*/tmp=Ciphertext(keyword,Plaintext);/*对应着密码表生成密文*/puts(tmp);/*输出密文*/puts(Decrypt(keyword,tmp));/*解密输出*/}