Reputation: 1665
I want to substitute from cypher text to plain text. But there can be n! possibilities where n is the number of characters
for example
cypher: [ a, b, c, d ]
plain : [ x, y, z, m ]
Let's say that I am sure one of the combination is correct plain text. In the example I know that there are 4! possible plaintext but I have to calculate it in python, because in my real problem n is 10.
I am asking for a piece of code or algorithm
Upvotes: 0
Views: 249
Reputation: 114038
you need to do letter frequency analysis. read this article about it https://inventwithpython.com/hacking/chapter20.html
there is source there for frequency analysis in python
once you implement the source code from that tutorial you can then write
def test_solution(cypher,plaintext,encrypted_text):
tab = string.transtab(cypher,plaintext)
decrypted = encrypted_text.translate(tab)
return (englishFreqMatchScore(decrypted),plaintext, decrypted)
tests = [test_solution(cypher,k,enc_text) for k in itertools.permutations(plain)]
print "SOLUTION:",max(tests)
note that this assumes the plaintext will be English ... letter frequency will be different for different languages (I think at least)
unfortunately this problem is np hard i think ... in order to find the best solution you must explore the entire solution space (so as N increases length of plaintext alphabet it gets alot harder...)
Upvotes: 1