包含凯撒密码问题的词条

2023-02-11 1:57:58 密语知识 思思

凯撒密码

我觉得创建26个文件实在太冗杂了。其实一个就够了,因为随着你的选择的改变(比如以9为加密条件,所有字母循环后移9位),目标文件里面的内容就自动更新了。

当然你也可以使用函数 int create(char *filename , int mode) 在执行框里手动输入像 e:\\original.txt 这样的地址字符,但你想象一下,这是不是很麻烦?

在实际加密中,可使用随机函数 rand()产生循环后移位数,而且完全可以不限制在26位,扩展ASCII码可以产生成千上万的字符,将文件加密到那些几乎无规律,难以识别的字符上,安全性就提高了许多。当然还有什么多轮加密之类的。可以自己慢慢摸索,挺有趣的。祝你好运!

修改如下(已成功执行):

#includestdio.h

#includestdlib.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 main()

{

FILE *in,*out;

char ch1,ch2;

int i;

printf("Please input the number(1~26) you want to use for encrypt:");

scanf("%d",i);

if((in=fopen("e:\\original.txt","r"))==NULL) /*文件名根据自己建立的位置修改,

我建在e盘的根目录下

{

printf("Can not open this file!\n");

exit(0);

}

if((out=fopen("e:\\encrypt.txt","w"))==NULL) //同上

{

printf("Can not open this file!\n");

exit(0);

}

while(!feof(in)){

if((ch1=fgetc(in))!=EOF)

ch2=encrypt(ch1,i);

fputc(ch2,out);

}

printf("Encrypt is over!\n");

fclose(in);

fclose(out);

}

包含凯撒密码问题的词条 第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);

}

.

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

凯撒密码位移规律表怎么用

凯撒移位密码。

也就是一种最简单的错位法,将字母表前移或者后错几位

,例如:

明码表:ABCDEFGHIJKLMNOPQRSTUVWXYZ。

密码表:DEFGHIJKLMNOPQRSTUVWXYZABC。

这就形成了一个简单的密码表,如果我想写frzy(即明文),那么对照上面密码表编成密码也就是iucb(即密文)了。密码表可以自己选择移几位,移动的位数也就是密钥。

进制转换密码。比如给你一堆数字,乍一看头晕晕的,你可以观察数字的规律,将其转换为10进制数字,然后按照每个数字在字母表中的排列顺序,拼出正确字母。

什么是凯撒密码?

根据苏维托尼乌斯的记载,恺撒曾用此方法对重要的军事信息进行加密: 如果需要保密,信中便用暗号,也即是改变字母顺序,使局外人无法组成一个单词。如果想要读懂和理解它们的意思,得用第4个字母置换第一个字母,即以D代A,余此类推。

同样,奥古斯都也使用过类似方式,只不过他是把字母向右移动一位,而且末尾不折回。每当他用密语写作时,他都用B代表A,C代表B,其余的字母也依同样的规则;用A代表Z。

扩展资料:

密码的使用最早可以追溯到古罗马时期,《高卢战记》有描述恺撒曾经使用密码来传递信息,即所谓的“恺撒密码”,它是一种替代密码,通过将字母按顺序推后起3位起到加密作用,如将字母A换作字母D,将字母B换作字母E。因据说恺撒是率先使用加密函的古代将领之一,因此这种加密方法被称为恺撒密码。这是一种简单的加密方法,这种密码的密度是很低的,只需简单地统计字频就可以破译。 现今又叫“移位密码”,只不过移动的为数不一定是3位而已。

参考资料来源:百度百科-凯撒密码

凯撒加密算法(最简单的对称加密)

凯撒密码是罗马扩张时期朱利斯• 凯撒(Julius Caesar)创造的,用于加密通过信使传递的作战命令。它将字母表中的字母移动一定位置而实现加密。例如如果向右移动 2 位,则 字母 A 将变为 C,字母 B 将变为 D,…,字母 X 变成 Z,字母 Y 则变为 A,字母 Z 变为 B。

因此,假如有个明文字符串“Hello”用这种方法加密的话,将变为密文: “Jgnnq” 。而如果要解密,则只要将字母向相反方向移动同样位数即可。如密文“Jgnnq”每个字母左移两位 变为“Hello” 。这里,移动的位数“2”是加密和解密所用的密钥。

该程序既可用于加密又可用于解密。只要传入明文和偏移量即可加密,解密需要传入密文和负的偏移量就可以解密。

输出的结果:

凯撒密码由于加解密比较简单,密钥总共只有 26 个,攻击者得到密文后即使不知道密钥,也可一个一个地试过去,最多试 26 次就可以得到明文。

这里不光根据 offset 偏移进行加密,还加上了字符所在的下标进行混合加密。

输出的结果:

凯撒密码怎么解

它是一种代换密码。据说恺撒是率先使用加密函的古代将领之一,因此这种加密方法被称为恺撒密码。

凯撒密码作为一种最为古老的对称加密体制,在古罗马的时候都已经很流行,他的基本思想是:通过把字母移动一定的位数来实现加密和解密。明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文。例如,当偏移量是3的时候,所有的字母A将被替换成D,B变成E,以此类推X将变成A,Y变成B,Z变成C。由此可见,位数就是凯撒密码加密和解密的密钥。