【发布时间】:2015-10-23 01:08:00
【问题描述】:
我正在尝试根据字母替换来解密我的密文(没有固定的偏移量)。我的目标是找到钥匙。
例如:
a -> g
b -> a
c -> k
这是我的纯文本:
直到现代密码学几乎专门指 加密是转换普通信息的过程 变成难以理解的文字
我生成一个随机替换并得到如下内容:
qjnyi teuvoj nytvh mocpnelorpkc ovdvoovu ritehn vwmiqhyfvic ne vjmocpnyej skymk yh nkv 诗vhh ed mejfvonyjl eouyjroc yjdeotrnyej yjne qjyjnviiylyxiv nvwn
规则:
- 纯文本只包含小写字母 a..z
- 空格“”未加密
- 英文文本
我想当我使用英文字母频率时,我可以将加密文本中最常用的字母替换为最常用的字母 链接:https://en.wikipedia.org/wiki/Letter_frequency#/media/File:English_letter_frequency_%28frequency%29.svg
但我无法解密所有内容。我只是得到这样的结果:
untsl midein tsmes riyptigisphy iefeiied slmist exrlussvely ti enriyptsin whsrh ss the piiress if rinveitsng iidsnsiy snfiimstsin sti unsntellsgsble 文本
我不知道如何继续下去......
import collections
import string
import random
mostUsedLetterNumbers = 9
mostUsedLetters = ['e','t','a','o','i','n','s','h','r','d','l','c','u','m','w','f','g','y','p','b','v','k','j','x','q','z']
#plain Text ---------------------------------------------------------------------------
#Lies den Text
text = "until modern times cryptography referred almost exclusively to encryption which is the process of converting ordinary information into unintelligible text"
print "Cleartext:"
text = text.lower()
print text
#crypt with random.shuffle ---------------------------------------------------------------------------
abc = list(string.ascii_lowercase) #abcdef....+ lower letter
key = abc[:]
random.shuffle(key);
#print (key) #this is my key I want to get in the end
e= dict(zip(abc,key))
#print e
#print text
ct = ""
for c in text:
#print(e[c])
if c == ' ':
ct = ct + " "
else:
ct = ct + e[c]
print "\nChippertext:\n",ct
#Count File ---------------------------------------------------------------------------
letters = collections.Counter(text)
#print letters
print "\nFound letters:"
for key,count in letters.iteritems():
if key == '\n':
print "newlines",count
elif key == ' ':
print "spaces",count
else:
print key, count
#del spaces and newlines
del letters['\n']
del letters[' ']
#----------------------------------------------------------------
#get letter count
topLetters = letters.most_common(mostUsedLetterNumbers)
#print topLetters
#replace letters
i=0
replacedText= ct
for i in range(0,mostUsedLetterNumbers):
replacedText = string.replace(replacedText,topLetters[i][0],mostUsedLetters[i])
print "\nDecrypted:\n",replacedText
print "\nOriginal text:\n",text
【问题讨论】:
-
两件事: 1. 您不太可能在如此短的样本中获得基于频率的精确匹配;和 2. 如果您一次替换每个字符,您会遇到问题(例如,假设您将所有
'a's 替换为'b's - 如果您随后替换所有'b's 会发生什么情况?)我建议您重构为简短的、单一用途的函数,然后单独测试每个函数。 -
处理第二个问题的一个好方法是将所有
'a's替换为'B's等等。然后将整个内容小写。 -
u这封信虽然不在前9,怎么破解成功? -
根据*,
u的频率:大约 3%。给定文本中的频率:2.33% - 所以:纯属运气
标签: python encryption brute-force