失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 凯撒密码C语言去掉空格字符 凯撒密码C语言实现

凯撒密码C语言去掉空格字符 凯撒密码C语言实现

时间:2023-06-04 18:52:23

相关推荐

凯撒密码C语言去掉空格字符 凯撒密码C语言实现

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

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

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

3、 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 return ch; main() int i,n; char ch0,ch1; FILE *in,*out; char infile10,outfi。

4、le10; 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!n); getch(); exit(0); prin。

5、tf(Please input the key:); scanf(%d,/*输入加密密码*/ 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); 。

6、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(Please input the key:); scanf。

7、(%d,/*输入解密密码(可以为加密时候的密码)*/ 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(fgetc(in),n),out); printf(nDecrypt is。

8、 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 else *(pwd+i)=z-(key%26-(*(pwd+i)-a)-1; else if(*(pwd+i)=A else *(pwd+i)=Z-(key%26-(*(pwd+i)。

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

10、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(char *keyword) /*生成密钥表*/ char ch; int i; int t; for(t=0;*(keyword+t)!=0;t+); for(ch=a;ch=z;ch+) for(i=0;*(keywor。

11、d+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+,j+) if(*(Plaintext+i)=ch) *(Plaintext+i)=*(keyword+j); break; return Plaintext; char *Decrypt(c。

12、har *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 *keyword,*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); /*解密输出*/。

如果觉得《凯撒密码C语言去掉空格字符 凯撒密码C语言实现》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。