We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Project Euler #59: XOR decryption
Project Euler #59: XOR decryption
Sort by
recency
|
24 Discussions
|
Please Login in order to post a comment
JAva code
COOL AF!
This question was actually very cool to solve. Mad fun!!
Java Solution -
I did it by
Frequency analysis
and by using
XOR properties
Complete brainstorming:I first thought I'd brute force the keys all over the search space and decrypt the whole text and would select the decrypted texts which would make sense and from that I will get the key. I thought I'd search the words 'and', 'or', 'the', 'if', 'so' etc. But this wasn't enough, it would not pass every test case; this approach was rather naive.
It then clicked me,
Because I recalled, the most occuring letter in english (only) is said to be e. Then every 'e' in the plaintext should've been converted to the same cipher character after XORing, right? That would mean the most occuring ascii character in the cipher text should be 'e' of the plain-text! for example, if the most occuring ascii character in the cipher text is, let's say '/' (slash) Then, because '/' occured the most, the XOR of '/' and KEY should give 'e'! ('/' ^ key = 'e') and from that, i can calculate the key, by using the property of XOR:
BUT NO
Because the encryption is done cyclicly by three characters in the key and not just 1 character. It then took me to the approach that I'd cyclicly divide the ciphertext into 3 separate ciphertexts and for every most occured character in each of these 3 ciphers, i'll one by one derive the key.
IT STILL DID NOT WORK!
That's because my assumptions and facts were wrong. The most occuring English Aplhabet is e but it's not true for complete texts. The most occuring character in probably every language is THE SPACE CHARACTER
So I finally changed my implementation from 'e' to ' ',
AND IT WORKED!
Here's the code in Java :)
Imo this is compact and runs quick!
C++ Solution
Code that passes here doesn't work for PE. Any suggestions?