Python凯撒密码函数
我想编写一个python函数,该功能会自动找到任何加密文本的键并解密文本。我想使用Caesar Cipher加密算法。
此代码是这样,您将输入腐烂,但我希望代码能够自动找到腐烂。
import string
from time import sleep
alphabet = string.ascii_lowercase # "abcdefghijklmnopqrstuvwxyz"
def decrypt(text, rot):
decrypted_text = ""
ct = open("rfc8446.txt", "r")
for cipher in text:
if cipher in alphabet: # check if character is an alphabet
position = alphabet.find(cipher) # find the index position of each text
new_position = (position - rot) % 25 # find the new index position to decrypt
new_character = alphabet[new_position] #
decrypted_text += new_character
else:
decrypted_text += cipher
print(decrypted_text)
text = "hnbcnamjh vh krtn unoc vn knqrwm"
rot = 7
decrypt(text, rot)
I want to write a python function that automatically finds the key of any encrypted text and decrypt the text. I want to use the caesar cipher encryption algorithm.
This code is such that, you will enter the rot but I want the code to be able to find the rot by automatically by itself.
import string
from time import sleep
alphabet = string.ascii_lowercase # "abcdefghijklmnopqrstuvwxyz"
def decrypt(text, rot):
decrypted_text = ""
ct = open("rfc8446.txt", "r")
for cipher in text:
if cipher in alphabet: # check if character is an alphabet
position = alphabet.find(cipher) # find the index position of each text
new_position = (position - rot) % 25 # find the new index position to decrypt
new_character = alphabet[new_position] #
decrypted_text += new_character
else:
decrypted_text += cipher
print(decrypted_text)
text = "hnbcnamjh vh krtn unoc vn knqrwm"
rot = 7
decrypt(text, rot)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需通过循环循环循环,这将为您提供所有可能的解决方案。
但是,找到好的是更复杂的,因为您需要使用文本合理性矩阵之类的东西来识别哪种“外观”英语“看起来”英语
this will give you all possible solutions, simply by looping for all rotations.
however finding the good one is way more complicated since you need to identify which solution "looks" english, using stuff like text plausibility matrices