凯撒的密码C语言for循环(c语言凯撒密码编程简单)

2023-02-15 0:12:42 密语知识 思思

c语言 写一段凯撒密钥时,在这个for循环之前加上一句getchar()与不加getchar()输出的结果不一样原因

在输入

7mazon

第一个程序输出结果应该是:rfets

第二个程序输出结果应该是:7rfet

第一个程序的那个7被for前的getchar()吃了,而加密的是mazon这5个字符,结果就是rfets。

第二个程序加密的是7mazo这5个字符,后面的n字符是不会加密的,因为只能加密5个字符,当加密7的时候,7不会符合任何条件,7不会加密,而加密的是mazo,所以最终显示结果是7rfet

还有什么疑问可以追问我啊,请采纳,谢谢

凯撒的密码C语言for循环(c语言凯撒密码编程简单) 第1张

凯撒密码的问题C语言

(2)kaiser加密算法

具体程序:

#includestdio.h

#includeconio.h

char encrypt(char ch,int n)/*加密函数,把字符向右循环移位n*/

{

while(ch='A'ch='Z')

{

return ('A'+(ch-'A'+n)%26);

}

while(ch='a'ch='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("\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);/*输入解密密码(可以为加密时候的密码)*/

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 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 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);

}

for(i=1;i=25;i++)/*暴力破解过程,在察看信息正确后,可以按'Q'或者'q'退出*/

{

rewind(in);

rewind(out);

clrscr();

printf("==========================================================\n");

printf("The outfile is:\n");

printf("==========================================================\n");

while(!feof(in))

{

ch1=encrypt(fgetc(in),26-i);

putch(ch1);

fputc(ch1,out);

}

printf("\n========================================================\n");

printf("The current key is: %d \n",i);/*显示当前破解所用密码*/

printf("Press 'Q' to quit and other key to continue......\n");

printf("==========================================================\n");

ch1=getch();

if(ch1=='q'||ch1=='Q')/*按'Q'或者'q'时退出*/

{

clrscr();

printf("\nGood Bye!\n");

fclose(in);

fclose(out);

sleep(3);

exit(0);

}

}

printf("\nForce decrypt is over!\n");

fclose(in);

fclose(out);

sleep(1);

}

menu();

ch0=getch();

}

clrscr();

printf("\nGood Bye!\n");

sleep(3);

}

.

希望能够帮助你 ^_^ 也希望能够选为最佳答案!

凯撒密码的算法c语言的怎么实现啊?

凯撒密码是一种非常古老的加密方法,相传当年凯撒大地行军打仗时为了保证自己的命令不被敌军知道,就使用这种特殊的方法进行通信,以确保信息传递的安全。他的原理很简单,说到底就是字母于字母之间的替换。下面让我们看一个简单的例子:“baidu”用凯撒密码法加密后字符串变为“edlgx”,它的原理是什么呢?把“baidu”中的每一个字母按字母表顺序向后移3位,所得的结果就是刚才我们所看到的密文。

#include stdio.h

main()

{

char M[100];

char C[100];

int K=3,i;

printf("请输入明文M(注意不要输入空白串)\n");

gets(M);

for(i=0;M[i]!='\0';i++)

C[i]=(M[i]-'a'+K)%26+'a';

C[i]='\0';

printf("结果是:\n%s\n",C);

}

凯撒密码,C语言,求救!

#include stdio.h

#include string.h

int main()

{

int i = 0;

int len = 0;

char ch;

char buf[256] = {0};

char nor[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

char enc[26] = {'s','u','w','y','a','c','e','g','i','k','m','o','q','r','t','v','x','z','b','d','f','h','j','l','n','p'};

printf("Encode or Decode: ");

scanf("%c",ch);

printf("please input your string: ");

fflush(stdin);

gets(buf);

len = strlen(buf);

switch (ch)

{

case 'e':

case 'E':

for (i=0;ilen;i++)

{

buf[i] = enc[buf[i] - 'a'];

}

break;

case 'd':

case 'D':

for (i=0;ilen;i++)

{

buf[i] = nor[i];

}

break;

default:

printf("wrong input!\n");

}

printf("%s\n",buf);

return 0;

}