900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 凯撒加密与暴力破解

凯撒加密与暴力破解

时间:2023-12-08 16:23:21

相关推荐

凯撒加密与暴力破解

字符串补充

1.如何随机生成验证码,快速生成内推码

import randomimport stringcode_str=string.ascii_letters+string.digits #字母大小写和数字#print(code_str)def gen_code(len=4):# code=''# for i in range(len):#new=random.choice(code_str)#code+=newreturn ''.join(random.sample(code_str,len))s=gen_code(5)print([s for i in range(10)])

凯撒加密

在密码学中,恺撒密码是一种最简单且最广为人知的加密技术。它是一种替换加密的技术,明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文。但是特别容易破解。

import stringdef kaisacrypt(text='heLlO',k=3):#对原有小写字母向右移动k位lower=string.ascii_lowercase[k:]+string.ascii_lowercase[:k]upper=string.ascii_uppercase[k:]+string.ascii_uppercase[:k]#用于创建字符串映射的转换表 'hello'table=str.maketrans(string.ascii_letters,lower+upper)#根据转换表去转换对应的字符return text.translate(table)crypt=kaisacrypt()print(crypt)

暴力破解

import stringdef kaisacrypt(text='heLlO',k=3):#对原有小写字母向右移动k位lower=string.ascii_lowercase[k:]+string.ascii_lowercase[:k]upper=string.ascii_uppercase[k:]+string.ascii_uppercase[:k]#用于创建字符串映射的转换表 'hello'table=str.maketrans(string.ascii_letters,lower+upper)#根据转换表去转换对应的字符return text.translate(table)def check(text):"""思路:测试文本中是否存在至少两个常见的英文单词"""Commonwords=('the','is','to','you','me','hello','and','ok')return len([1 for word in Commonwords if word in text])>2def bruteforce(text):for i in range(26):t=kaisacrypt(text,-i)if check(t):print(i) #字母推了多少位print(t) #打印解密后的字母breaktext="""If not to the sun for smiling,warm is still in the sun there, But we will laugh more confident calm"""crypt=kaisacrypt(text=text,k=10)print(crypt) #加密bruteforce(crypt) #解密

查看Ascii码值

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。