我應該將此Cesar密碼轉換爲仿射密碼,但不幸似乎無法弄清楚。Python:將Cesar密碼轉換爲仿射密碼
最終,我應該將給定的Cesar密碼代碼轉換爲Affine密碼,該密碼解密由26個小寫字母{a,b,c,...,x,y,z}組成的任何字符串密文。這是通過蠻力搜索,嘗試所有可能的密鑰並輸出加密密鑰k =(a,b)和相應的純文本。當「可理解的」純文本消息被解密時,程序應該停止。
任何幫助指出我在正確的方向將不勝感激。
這裏是塞薩爾密碼代碼:
def caeser (s, k):
y = ""
for x in s:
c = ((ord(x) - ord("a")) + k) % 26
y = y + chr(ord("a") + c)
return y
def topLetter(s):
count = [0]*26
for x in s:
x_index = (ord(x) - ord("a"))
count[x_index] += 1
lcount = 0
lchar = ""
for i in range(26):
if count[i] > lcount:
lcount = count[i]
lchar = chr(i + ord('a'))
return lchar
def findShift(s):
return(ord(topLetter(s)) - ord("e"))
cipher = "vwduwljudeehghyhubwklqjlfrxogilqgsohdvhuhwxuqdqbeoxhsulqwviruydxowdqgdodupghvljqedvhgrqzklfkedqnbrxghflghrqldpvhwwlqjxsvdihkrxvhfr"
print("Deciphering:'", cipher +"'")
print("\nBrute Force")
shift = 1
while shift < 26:
ptext = caeser(cipher, -1*shift)
print()
print("Plaintext message when encryption key =", shift)
print(ptext)
shift += 1
下面是一些示例輸出來幫助你們的。
Decrypting the cipher text: 'vwduwljudeehghyhubwklqjlfrxogilqgsohdvhuhwxuqdqbeoxhsulqwviruydxowdqgdodupghvljqedvhgrqzklfkedqnbrxghflghrqldpvhwwlqjxsvdihkrxvhfr'
Encryption key: a = 1 b = 1
Decryption equation: x = 1 *(y - 1)
Plaintext:
uvctvkitcddgfgxgtavjkpikeqwnfhkpfrngcugtgvwtpcpadnwgrtkpvuhqtxcwnvcpfcnctofgukipdcugfqpyjkejdcpmaqwfgekfgqpkcougvvkpiwruchgjqwugeq
Hit enter to continue search or 'S' key to stop:
Encryption key: a = 3 b = 1
Decryption equation: x = 9 *(y - 1)
Plaintext:
yhsphmupsbbctczcpahdmfumkoqntlmftxncsycpchqpfsfabnqcxpmfhylopzsqnhsftsnspwtcymufbsyctofidmkdbsfeaoqtckmtcofmswychhmfuqxyslcdoqycko
Hit enter to continue search or 'S' key to stop:
Encryption key: a = 5 b = 1
Decryption equation: x = 21 *(y - 1)
@SeanBallais我一直在試圖弄清楚Cesar Cipher的來龍去脈,所以我明白它足以試圖將它轉換爲仿射密碼。也盡我所能完全理解仿射密碼和我需要做的事情。 – BBladem83
下面是對凱撒密碼的解釋:從a到z連續兩次寫下字母表:abc..zabc..z。現在,在下一行中,從第一個'a'開始計數'k'個位置(無論您選擇哪個值),並且從a到z寫入一次字母表。例如,如果你的'k'是11,你可以計算11個以上的位置,而第二行的'a'將位於第一行字母'l'(ell)之下。現在運行密碼,在底行找到您的句子的每個字符,並將其替換爲頂行中的相應字符。 –
我不確定我是否理解你在這裏問的問題(而不是你的家庭作業是什麼)。你對仿射密碼是什麼感到困惑嗎?或者關於如何將它整合到你的暴力破解程序中?或者你不明白你已經有了什麼代碼? – Blckknght