今天给各位分享python凯撒密码加密算法的知识,如果能碰巧解决你现在面临的问题,别忘了关注本站。
凯撒密码作为一种最为古老的对称加密体制,在古罗马的时候都已经很流行,他的基本思想是:通过把字母移动一定的位数来实现加密和解密。明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文。例如,当偏移量是3的时候,所有的字母A将被替换成D,B变成E,以此类推X将变成A,Y变成B,Z变成C。由此可见,位数就是凯撒密码加密和解密的密钥。
如下代码是以偏移量为13展开计算的。123
源代码如下:
sr1="abcdefghijklmnopqrstuvwxyz"sr2=sr1.upper()
sr=sr1+sr1+sr2+sr2
st="The Zen of Python"sResult=""for j in st: if j==" ":
sResult = sResult +" "
continue
i=sr.find(j) if(i-1):
sResult=sResult+sr[i+13]print sResult12345678910111213
运行结果为:
Gur Mra bs Clguba
1. 将“We are students.”这个英文词句用k=4的凯萨密码翻译成密码
1. 恺撒密码,
作为一种最为古老的对称加密体制,他的基本思想是:
通过把字母移动一定的位数来实现加密和解密。
例如,如果密匙是把明文字母的位数向后移动三位,那么明文字母B就变成了密文的E,依次类推,X将变成A,Y变成B,Z变成C,由此可见,位数就是凯撒密码加密和解密的密钥。
如:ZHDUHVWXGHQWV(后移三位)
2. 凯撒密码,
是计算机C语言编程实现加密和解密。挺复杂的。你可以研究一下哦。
2. 将凯撒密码(K=7)的加密、解密过程用C语言编程实现
/*
声明:MSVC++6.0环境测试通过
*/
#includestdio.h
#includectype.h
#define maxlen 100
#define K 7
char *KaisaEncode(char *str)//加密
{
char *d0;
d0=str;
for(;*str!='\0';str++)
{
if(isupper(*str))
*str=(*str-'A'+K)%26+'A';
else if(islower(*str))
*str=(*str-'a'+K)%26+'a';
else
continue;
}
return d0;
}
char *KaisaDecode(char *str)//解密
{
char *d0;
d0=str;
for(;*str!='\0';str++)
{
if(isupper(*str))
*str=(*str-'A'-K+26)%26+'A';
else if(islower(*str))
*str=(*str-'a'-K+26)%26+'a';
else
continue;
}
return d0;
}
int main(void)
{
char s[maxlen];
gets(s);
puts(KaisaEncode(s));
puts(KaisaDecode(s));
return 0;
}
3. 将凯撒密码X的加密、解密过程用C语言编程实现
(2)kaiser加密算法 具体程序:#include #include char encrypt(char ch,int n)/*加密函数,把字符向右循环移位n*/ { while(ch='A'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); }。
4. 怎样编写程序:实现恺撒密码加密单词"julus"
用下面程序:新建个txt,放进去任意单词,设置#define N 5中的值,实现字母移位,达到加密目的。
本程序提供解密功能/************************************************************************//* 版权所有:信息工程学院 王明 使用时请注明出处!! *//* 算法:凯撒密码体制 e799bee5baa6e4b893e5b19e31333264643062 *//************************************************************************/#include #define N 5void jiami(char namea[256]) { FILE *fp_jiami,*fp_file2; char c; fp_jiami=fopen(namea,"rb"); fp_file2=fopen("file2.txt","wb"); while(EOF!=(fscanf(fp_jiami,"%c",c))) { if((c='A'c='a'c='A'c='a'c='a'c='A'c='a'c='A'c='a'c='A'c='Z')c=c+32; } fprintf(fp_file3,"%c",c); } fclose(fp_file3); fclose(fp_jiemi); }int main(){ char name[256]; int n; printf("输入你要操作的TXT文本:"); gets(name); printf("\n请选择需要进行的操作:\n"); printf(" 1:加密 2:解密 \n"); printf("输入你的选择:"); scanf("%d",n); switch(n) { case 1:{jiami(name);printf("\t加密成功!!\n\n"); break;} case 2:{jiemi(name);printf("\t解密成功!!\n\n"); break;} default:{printf("输入操作不存在!");} } return 0;}。
5. 谁有PYTHON编写的凯撒密码的加密和解密代码
给你写了一个.
def convert(c, key, start = 'a', n = 26):
a = ord(start)
offset = ((ord(c) - a + key)%n)
return chr(a + offset)
def caesarEncode(s, key):
o = ""
for c in s:
if c.islower():
o+= convert(c, key, 'a')
elif c.isupper():
o+= convert(c, key, 'A')
else:
o+= c
return o
def caesarDecode(s, key):
return caesarEncode(s, -key)
if __name__ == '__main__':
key = 3
s = 'Hello world!'
e = caesarEncode(s, key)
d = caesarDecode(e, key)
print e
print d
运行结果:
Khoor zruog!
Hello world!
def encryption():
str_raw = input("请输入明文:")
k = int(input("请输入位移值:"))
str_change = str_raw.lower()
str_list = list(str_change)
str_list_encry = str_list
i = 0
while i len(str_list):
if ord(str_list[i]) 123-k:
str_list_encry[i] = chr(ord(str_list[i]) + k)
else:
print ("解密结果为:"+"".join(str_list_decry))
while True:
print (u"1. 加密")
print(u"2. 解密")
choice = input("请选择:")
if choice == "1": encryption()
elif choice == "2": decryption()
else: print (u"您的输入有误!")
# codinng=utf-8
x = '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'.split(' ')
y = 'n o p q r s t u v w x y z a b c d e f g h i j k l m'.split(' ')
X = map(lambda x: x.upper(), x)
Y = map(lambda x: x.upper(), y)
dict_kaisa = dict(zip(x + X, y + Y)) # 创建一个字典, 键为原字符串, 值为加密字符串
# 定义凯撒加密函数, 输入字符串, 输出凯撒加密后字符串
def kaisa(string):
result = []
for i in range(len(string)):
if string[i] in dict_kaisa.keys():
result.append(dict_kaisa[string[i]])
else:
result.append(string[i])
return ''.join(result)
print(kaisa('The Zen of Python')) # 结果为Gur Mra bs Clguba
%26是确保加密后的值仍然在字母表范围内(当然为了能解密,前提条件是原文的字符也不能超过这26个字母)
为什么要%26呢?直接用原文的Unicode值加三再转换成字符不就可以了吗? ---
凯撒密码应该是很古老的加密技术吧,那时候计算机都还没出现,ASCII码或者你说的unicode码更不存在。所以说表示字符的就那么26个。当然你说我可以创造一些字符啊,那也没问题,和直接用数字也没多少区别。但有可能保持这26个字母更能起到混淆视听的作用,毕竟它们可能不能一眼看出是密码。
不管在古代还是现代,增加字库都是有成本的,比如雕刻活字、设计字体等,能复用现有字体的话,打印传播密文的成本就大大降低。
import string
def kaisa(s, k): #定义函数 接受一个字符串s 和 一个偏移量k
lower = string.ascii_lowercase #小写字母
upper = string.ascii_uppercase #大写字母
before = string.ascii_letters #无偏移的字母顺序 小写+大写
after = lower[k:] + lower[:k] + upper[k:] + upper[:k] #偏移后的字母顺序 还是小写+大写
#分别把小写字母和大写字母偏移后再加到一起
table = ''.maketrans(before, after) #创建映射表
return s.translate(table) #对s进行偏移 即加密
s = input('请输入一个字符串:')
k = int(input('请输入一个整数密钥:'))
print(kaisa(s, k))
调用此函数
智能化时代的到来涉及了各种核心算法,保护算法就能保障开发者权益,杜绝市面上各种山寨品,加密芯片恰好能起到很好的保护作用,如何选择加密芯片呢?KEROS加密芯片专注于加密领域十余年,行业首选。
1.安全性:采用国际通用aes256算法加密并同时通过KAS传送,除基本认证之外,利用2K安全EEPROM,用户可以自己管理密钥和数据,实现双重保护。
2.唯一性:以定制的方式为每一位用户单独定制“专属型号CID”,多用户之间算法不兼容,并且采用固化的方法直接将算法固化到晶圆上而无需烧入。
3.序列号:每颗芯片制造生产时具有5字节全球唯一SN序列号,每颗芯片SN都不会重复。
4.防抄特性:每颗芯片都有自己独特的密钥系统,破解单颗芯片只对这颗芯片对应的产品有效,对整个同类型的产品是无效的,依旧无法通过验证。而且KEROS采用ASIC方法设计,芯片内为纯逻辑电路,封装内有40多层逻辑电路整合了10万多个逻辑门,爆力刨片破解难度可想而知。
5.安全存储:用户可以将保密数据加密之后安全的存放到EEPROM中。python凯撒密码加密算法的介绍就聊到这里吧,感谢你花时间阅读本站内容。
本文标签:python凯撒密码加密算法