求密文 凯撒密码 (凯撒密码计算题)

2023-03-14 2:53:12 密码用途 思思

直接把每个字母往后推三位

出来的就是密文了

明文:COMPUTERSYSTEM

密文:FRPSXWHUVBVWHP

而解密时 只需要把密文每个字母前推3位(推三位这是标准的凯撒密码 加密时不一定推三位 这时只要统计各字母出现的频率便很容易解开)

凯撒密码的问题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);

}

.

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

设密文为love,试利通用凯撒密码(k=3)对其解密,得出明文

1、首先通过把字母移动一定的位数来实现加密和解密。明文中的所有字母都在字母表上向后按照一个固定数目进行偏移后被替换成密文。

2、在使用凯撒密码时需要将自己想加密的内容写下来,加密时需要通过字母加密,则写加密内容时需将内容用英文表达或用汉语拼音表达。

3、然后确定偏移方向和偏移度,即向前还是向后偏移,以及偏移的位数,都是向后偏移度为3,则A—D,B—E,下面就以该加密条件进行加密。

4、将每一个字母都一一加密后按照原来的顺序写下,则完成了加密,密文的内容无法直接看出。在加密过程中若某些字母后没有字母了,则可以从头数起,即采用上述加密条件,则X—A,Y—B,Z—C。

5、若想对密文进行解密,则需找到正确的偏移方向已经偏移量,这样才能成功解密。以上就是凯撒密码的方法,这个方法可含蓄表达一些不好直接表达的内容。