Skip to content

Instantly share code, notes, and snippets.

@NatashaKSS
Last active March 31, 2018 07:37
Show Gist options
  • Save NatashaKSS/0aaf30069d6814c2d66f729b9fef130c to your computer and use it in GitHub Desktop.
Save NatashaKSS/0aaf30069d6814c2d66f729b9fef130c to your computer and use it in GitHub Desktop.
Decrypts an ciphertext with a 1-byte XOR scheme (brute force approach)
"""
TASK:
Decrypt a piece of ciphertext which uses a 1-byte XOR Scheme.
Brute force approach.
"""
# The flag in this one contains the word "Congratulations!"
ciphertext = "W{zsfu`axu`}{zg54@|}g4}g4`|q4rxus.4wg&%$#o#%vwp&pu&'p&\"!r$%!$!&r $ur\",!!u$i:"
def decrypt(input_str):
KEY_SPACE = 128
# search through key space
for i in range(KEY_SPACE - 1):
candidate_plaintext = ""
# XOR-ing each char with the candidate key
for c in list(input_str):
XOR_value = ord(c) ^ i
candidate_plaintext += chr(XOR_value)
# Printing it all out on console with a line separator!
print(candidate_plaintext)
print("---------------------------------------------------")
decrypt(ciphertext)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment