嘿伙计,如果你看下面我的代码,你将能够看到我能够创建一个程序,可以打开文件解码文件的内容并将其保存到另一个文件,但我需要输入密钥每一次,但如果我不知道他们关键如何使用频率分析来计算密钥。
import sys
def decrypt(cipher, key):
plain = ""
for index in range(len(cipher)):
if cipher[index].isalpha():
if cipher[index].isupper():
plain = plain + chr((ord(cipher[index]) - 64 - key) % 26 + 64)
elif cipher[index].islower():
plain = plain + chr((ord(cipher[index]) - 96 - key) % 26 + 96)
else:
plain = plain + cipher[index]
return plain
in_filename = sys.argv[1]
key = int(sys.argv[2])
out_filename = sys.argv[3]
with open(in_filename, "r") as f:
encrypted = f.read()
decrypted = decrypt(encrypted, key)
with open(out_filename, "w+") as f:
f.write(decrypted)