Last active
December 10, 2015 20:18
-
-
Save sbehrens/4487661 to your computer and use it in GitHub Desktop.
Break stream cipher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import array | |
import base64 | |
import copy | |
# Found X0R cipher on an app assessment? Got the ciphertext and know the plaintext? Use this to get the key. | |
cipher_text = array.array('B', base64.b64decode("Some Blob of base64 encoded ciphertext remove decoder if not base64")) | |
plain_text = array.array('B', "some known plaintext value") | |
for i in range(len(plain_text)): | |
plain_text[i] ^= cipher_text[i] | |
key_stream = copy.deepcopy(plain_text.tostring()) | |
def findkey(key_stream): | |
new_string = key_stream | |
for i in range(len(key_stream)): | |
new_string = new_string[-1] + new_string[:-1] | |
if new_string == key_stream: | |
return i + 1 | |
key = key_stream[:findkey(key_stream)] | |
print key |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment