当前位置:首页 » 编程软件 » 凯撒密码编译

凯撒密码编译

发布时间: 2022-04-18 13:39:04

㈠ 如何用python编写凯撒密码

凯撒密码是对字母表整体进行偏移的一种变换加密。因此,建立一个字母表,对明文中每个字母,在这个字母表中偏移固定的长度即可得到对应的密文字母。

最基本的实现如下:

defcaesarcipher(s:str,rot:int=3)->str:
_='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
encode=''
i=0
forcins:
try:
encode+=_[(_.index(c.upper())+rot)%len(_)]
except(Exception,)ase:
encode+=c
returnencode


print(caesarcipher('hellow'))
print(caesarcipher('KHOORZ',-3))

如果要求解密后保持大小写,那么,字母表_还需要包含所有小写字母并且index时不对c做upper处理.

同样的,也可以在字母表中追加数字,各种符号,空格等.

㈡ 将凯撒密码X的加密、解密过程用C语言编程实现

1、在密码学中,恺撒密码(或称恺撒加密、恺撒变换、变换加密)是一种最简单且最广为人知的加密技术。它是一种替换加密的技术,明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文。例如,当偏移量是3的时候,所有的字母A将被替换成D,B变成E,以此类推。这个加密方法是以恺撒的名字命名的,当年恺撒曾用此方法与其将军们进行联系。恺撒密码通常被作为其他更复杂的加密方法中的一个步骤,例如维吉尼尔密码。恺撒密码还在现代的ROT13系统中被应用。但是和所有的利用字母表进行替换的加密技术一样,恺撒密码非常容易被破解,而且在实际应用中也无法保证通信安全。例子恺撒密码的替换方法是通过排列明文和密文字母表,密文字母表示通过将明文字母表向左或向右移动一个固定数目的位置。

2、kaiser加密算法具体程序:

#include<stdio.h>
#include<conio.h>
charencrypt(charch,intn)/*加密函数,把字符向右循环移位n*/
{
while(ch>='A'&&ch<='Z')
{
return('A'+(ch-'A'+n)%26);
}
while(ch>='a'&&ch<='z')
{
return('a'+(ch-'a'+n)%26);
}
returnch;
}
voidmenu()/*菜单,1.加密,2.解密,3.暴力破解,密码只能是数字*/
{
clrscr();
printf(" =========================================================");
printf(" 1.Encryptthefile");
printf(" 2.Decryptthefile");
printf(" 3.Forcedecryptfile");
printf(" 4.Quit ");
printf("========================================================= ");
printf("Pleaseselectaitem:");
return;
}
main()
{
inti,n;
charch0,ch1;
FILE*in,*out;
charinfile[20],outfile[20];
textbackground(BLACK);
textcolor(LIGHTGREEN);
clrscr();
sleep(3);/*等待3秒*/
menu();
ch0=getch();
while(ch0!='4')
{
if(ch0=='1')
{
clrscr();
printf(" Pleaseinputtheinfile:");
scanf("%s",infile);/*输入需要加密的文件名*/
if((in=fopen(infile,"r"))==NULL)
{
printf("Cannotopentheinfile! ");
printf("Pressanykeytoexit! ");
getch();
exit(0);
}
printf("Pleaseinputthekey:");
scanf("%d",&n);/*输入加密密码*/
printf("Pleaseinputtheoutfile:");
scanf("%s",outfile);/*输入加密后文件的文件名*/
if((out=fopen(outfile,"w"))==NULL)
{
printf("Cannotopentheoutfile! ");
printf("Pressanykeytoexit! ");
fclose(in);
getch();
exit(0);
}
while(!feof(in))/*加密*/
{
fputc(encrypt(fgetc(in),n),out);
}
printf(" Encryptisover! ");
fclose(in);
fclose(out);
sleep(1);
}
if(ch0=='2')
{
clrscr();
printf(" Pleaseinputtheinfile:");
scanf("%s",infile);/*输入需要解密的文件名*/
if((in=fopen(infile,"r"))==NULL)
{
printf("Cannotopentheinfile! ");
printf("Pressanykeytoexit! ");
getch();
exit(0);
}
printf("Pleaseinputthekey:");
scanf("%d",&n);/*输入解密密码(可以为加密时候的密码)*/
n=26-n;
printf("Pleaseinputtheoutfile:");
scanf("%s",outfile);/*输入解密后文件的文件名*/
if((out=fopen(outfile,"w"))==NULL)
{
printf("Cannotopentheoutfile! ");
printf("Pressanykeytoexit! ");
fclose(in);
getch();
exit(0);
}
while(!feof(in))
{
fputc(encrypt(fgetc(in),n),out);
}
printf(" Decryptisover! ");
fclose(in);
fclose(out);
sleep(1);
}
if(ch0=='3')
{
clrscr();
printf(" Pleaseinputtheinfile:");
scanf("%s",infile);/*输入需要解密的文件名*/
if((in=fopen(infile,"r"))==NULL)
{
printf("Cannotopentheinfile! ");
printf("Pressanykeytoexit! ");
getch();
exit(0);
}
printf("Pleaseinputtheoutfile:");
scanf("%s",outfile);/*输入解密后文件的文件名*/
if((out=fopen(outfile,"w"))==NULL)
{
printf("Cannotopentheoutfile! ");
printf("Pressanykeytoexit! ");
fclose(in);
getch();
exit(0);
}
for(i=1;i<=25;i++)/*暴力破解过程,在察看信息正确后,可以按'Q'或者'q'退出*/
{
rewind(in);
rewind(out);
clrscr();
printf("========================================================== ");
printf("Theoutfileis: ");
printf("========================================================== ");
while(!feof(in))
{
ch1=encrypt(fgetc(in),26-i);
putch(ch1);
fputc(ch1,out);
}
printf(" ======================================================== ");
printf("Thecurrentkeyis:%d ",i);/*显示当前破解所用密码*/
printf("Press'Q'toquitandotherkeytocontinue...... ");
printf("========================================================== ");
ch1=getch();
if(ch1=='q'||ch1=='Q')/*按'Q'或者'q'时退出*/
{
clrscr();
printf(" GoodBye! ");
fclose(in);
fclose(out);
sleep(3);
exit(0);
}
}
printf(" Forcedecryptisover! ");
fclose(in);
fclose(out);
sleep(1);
}
menu();
ch0=getch();
}
clrscr();
printf(" GoodBye! ");
sleep(3);
}

㈢ 凯撒密码怎么用C语言编,急救!!!附加解释,谢谢

凯撒加密是最简单的加密,就是 把字符移动n位, 例如 :移动1位时,a就用b表示,f用e表示。

#include<stdio.h>
int main(void)
{
char buf[] = "hello";
int i = 0;

printf("before: %s\n", buf);
while (buf[i])
buf[i++] += 1; // 移1位,a 变b
printf("after: %s\n", buf);
return 0;
}

㈣ 怎么用Python编辑出此凯撒密码的解密密码

凯撒密码的加密密钥与解密密钥是相反数,因此,k给相反数即可:
kaisa(kaisa(s, 3), -3)

㈤ C语言编程问题,凯撒密码

#include<stdio.h>
chars[]="(BTBU)isakeystate-,Sciences,Engineering,Law,Economics,History,PhilosophyandManagement.";
voidfun(char*s,intn)
{
inti=0;
while(s[i]!=0)
{
if(s[i]<='z'&&s[i]>='a')
{
s[i]+=n;
if(s[i]>'z')s[i]-=24;
}
elseif(s[i]<='Z'&&s[i]>='A')
{
s[i]+=n;
if(s[i]>'Z')s[i]-=24;
}
i++;
}
}
voidmain()
{
fun(s,3);
puts(s);
}

㈥ 凯撒密码用C++编写

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const int MAX_N=200;
int main(int argc, char *argv[])
{
int i,j,p;
char text[MAX_N];
char alphabet[30];
char op[10];

while(1)
{
printf("1---输入密码表 2---退出\n");
gets(op);

if(strcmp(op,"1")==0)
{
printf("密码表:");
gets(alphabet);

while(1)
{
printf("1---加密 2---解密 3---返回\n");
gets(op);

if(strcmp(op,"1")==0 ||strcmp(op,"2")==0 )
{
printf("输入文本:");
gets(text);

for(i=0;text[i]!='\0';i++)
{
if((text[i]>='a'&&text[i]<='z') || (text[i]>='A'&&text[i]<='Z') )
{
if(strcmp(op,"1")==0)
{
p=text[i]>='a'? (text[i]-'a'):(text[i]-'A');
text[i]=text[i]+ alphabet[p]-(p+'A');
}
else
{
for(j=0;;j++)
if(alphabet[j]==text[i]||alphabet[j]==(text[i]-('a'-'A')))
break;
text[i]= text[i]>='a' ? (j+'a') :(j+'A');
}

}
}//for(i)

if(strcmp(op,"1")==0)
printf("加密后的文本为:" );
else
printf("解密后的文本为:");
printf("%s\n\n",text);

}
else if(strcmp(op,"3")==0)
{
printf("\n");
break;
}
else
{
printf("选择有误!请重新选择!\n");
}
}//while(1)
}

else if(strcmp(op,"2")==0)
{
exit(1);
}
else
{
printf("选择有误!请重新选择!\n");
}
}
return 0;
}

/*
输入样例
QWERTYUIOPASDFGHJKLZXCVBNM
Welcome to ZZSY2009!
输出样例
Vtsegdt zg MMLN2009!
*/

㈦ 凯撒密码,要求C语言编写,求救!

写的一般般,希望对LZ有所帮助
#include <stdio.h>
#include <string.h>
int main()
{
char str[201];//存放字符
char tmp[11];//临时变量
int i;//循环变量
int len;//存放消息长度
scanf("%s",tmp);//这里输入START,开始
getchar();//接收回车
while(strcmp(tmp,"ENDOFINPUT"))
{
gets(str);//由于输入中有空格,所以用gets输入
getchar();//接收回车
len = strlen(str);
for(i=0;i<len;i++)
{
if(str[i]>='A'&&str[i]<='Z')
{
str[i] -= 5 ;
if(str[i] < 65)
{
str[i] +=26;
}
}
}
scanf("%s",tmp);//这里输入END,结束
printf("%s\n",str);//处理完就直接输出结果
scanf("%s",tmp);//输入START表示继续,输入ENDOFINPUT则表示最后一个数据集
getchar();//接收回车
}
return 0;

}

热点内容
算法化是 发布:2025-03-20 03:48:20 浏览:770
拆二代访问 发布:2025-03-20 03:47:34 浏览:62
随机数排序c语言 发布:2025-03-20 03:35:31 浏览:497
当前页面脚本发生错误类型不匹配 发布:2025-03-20 03:26:47 浏览:991
strutsajax上传图片 发布:2025-03-20 03:25:03 浏览:385
手机在线缓存 发布:2025-03-20 03:21:06 浏览:45
ftp路径上传时间 发布:2025-03-20 03:13:42 浏览:103
电脑连接通讯服务器失败怎么回事 发布:2025-03-20 03:10:30 浏览:286
cmake静态编译库 发布:2025-03-20 02:55:25 浏览:409
手机存储修复 发布:2025-03-20 02:48:14 浏览:269