Skip to content

Instantly share code, notes, and snippets.

@sbehrens
Last active December 10, 2015 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbehrens/4487661 to your computer and use it in GitHub Desktop.
Save sbehrens/4487661 to your computer and use it in GitHub Desktop.
Break stream cipher
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