凯撒密码--C语言实现

上传人:沈*** 文档编号:133518611 上传时间:2022-08-10 格式:DOC 页数:8 大小:50.50KB
收藏 版权申诉 举报 下载
凯撒密码--C语言实现_第1页
第1页 / 共8页
凯撒密码--C语言实现_第2页
第2页 / 共8页
凯撒密码--C语言实现_第3页
第3页 / 共8页
资源描述:

《凯撒密码--C语言实现》由会员分享,可在线阅读,更多相关《凯撒密码--C语言实现(8页珍藏版)》请在装配图网上搜索。

1、凯撒密码是一种非常古老的加密方法,相传当年凯撒大地行军打仗时为了保证自己的命令不被敌军知道,就使用这种特殊的方法进行通信,以确保信息传递的安全。他的原理很简单,说到底就是字母于字母之间的替换。下面让我们看一个简单的例子:“baidu”用凯撒密码法加密后字符串变为“edlgx”,它的原理是什么呢?把“baidu”中的每一个字母按字母表顺序向后移3位,所得的结果就是刚才我们所看到的密文。/*凯撒密码实现 要求,将明文字母变成它后面第三个字母,后面的循环到前面! 公式为f(a)=(f(a)+3)%26*/#include int main() char P100;/*定义明文长度*/ char C1

2、00;/*定义密文长度*/ int K=3,i; printf(Please input Plaintext:n); /*输入明文*/ gets(P); /* 接受明文*/ for(i=0;Pi!=0;i+) /*逐个判断字母的大小*/ if(Pi=a&Pi=A&Pi=Z)/*大写字母 */ Ci=(Pi-A+K)%26+A; else Ci= ;/*如果不是字母,转换为空格*/ printf(The Ciphertext is :n%sn,C);/*输出密文*/ getch(); return 0; 1、程序结构化,用函数分别实现2、对文件的加密,解密输出到文件#include#includ

3、evoid menu()/*菜单,1.加密 2.解密 3.退出*/clrscr();printf(n=);printf(n1.Encrypt the file);printf(n2.Decrypt the file);printf(n3.Quitn);printf(=n);printf(Please select a item:);return;char encrypt(char ch,int n)/*加密函数,把字符向右循环移位n*/while(ch=A&ch=a&ch=z)return (a+(ch-a+n)%26);return ch;main()int i,n;char ch0,ch1

4、;FILE *in,*out;char infile10,outfile10;textbackground(RED);textcolor(LIGHTGREEN);clrscr();menu();ch0=getch();while(ch0!=3)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

5、!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)/*加密*/ fp

6、utc(encrypt(fgetc(in),n),out); printf(nEncrypt is over!n); 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

7、(Please input the key:); scanf(%d,&n);/*输入解密密码(可以为加密时候的密码)*/ n=26-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(fge

8、tc(in),n),out); printf(nDecrypt is over!n); fclose(in); fclose(out); sleep(1);clrscr(); printf(nGood Bye!n);sleep(3); getch();-/*移位法:*/#include #include char *Encrypt(char *pwd,int key) /*加密*/for(int i=0;*(pwd+i)!=0;i+)if(*(pwd+i)=a&*(pwd+i)=A&*(pwd+i)=a&*(pwd+i)=key%26)*(pwd+i)=*(pwd+i)-key%26;else

9、 *(pwd+i)=z-(key%26-(*(pwd+i)-a)-1;else if(*(pwd+i)=A&*(pwd+i)=key%26)*(pwd+i)=*(pwd+i)-key%26;else *(pwd+i)=Z-(key%26-(*(pwd+i)-A)-1;return pwd;void main()char *pwd;int key;pwd=(char*)malloc(sizeof(char);printf(Input your password:);gets(pwd);printf(Input a key:);scanf(%d,&key);printf(The Ciphertex

10、t is:);printf(%sn,Encrypt(pwd,key);-/*替换法:*/#include #include #include void table(char *keyword) /*筛选密钥(去重复去空格)*/int i,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-;void newTab(c

11、har *keyword) /*生成密钥表*/char ch;int i;int t;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) /*按密码表加密*/char ch;int i,j;for(i=0;*(Plaintext+i)!=0;i+)for(ch=a,j=0;ch=z;ch+,

12、j+)if(*(Plaintext+i)=ch)*(Plaintext+i)=*(keyword+j);break;return Plaintext;char *Decrypt(char *keyword,char *Plaintext) /*解密*/char ch;int i,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;return Plaintext;void main()char *keywo

13、rd,*Plaintext,*tmp=NULL;keyword=(char*)malloc(sizeof(char);Plaintext=(char*)malloc(sizeof(char);printf(Input key word:); /*输入欲用密钥*/gets(keyword);printf(Input Plaintext:); /*输入要转换的明文*/gets(Plaintext);table(keyword); /*去空格去重复*/newTab(keyword); /*生成密码表*/tmp=Ciphertext(keyword,Plaintext); /*对应着密码表生成密文*/puts(tmp); /*输出密文*/puts(Decrypt(keyword,tmp); /*解密输出*/

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!