Skip to content

Instantly share code, notes, and snippets.

@cmattoon
Last active August 29, 2015 14:17
Show Gist options
  • Save cmattoon/f9c80cf54930f258ef81 to your computer and use it in GitHub Desktop.
Save cmattoon/f9c80cf54930f258ef81 to your computer and use it in GitHub Desktop.
Caesar Cipher Solver
#!/usr/bin/python
"""Brute-forces a ROT cipher
http://cmattoon.com/infosec-institute-ctf-level-4/
"""
def rot(text, n):
"""Rotates 'text' by 'n' positions"""
output = ''
for char in text.lower(): # don't use uppercase values
x = ord(char)
if (x >= ord('a') and x <= ord('z')):
x += n # Rotate the character
if (x > ord('z')): # Wrap it around, if it extends past 'z'
x = (ord('a')-1) + (x - ord('z'))
output += chr(x)
else:
# It's not a letter
output += char
return output
ciphertext = 'vasbfrp_syntvf_jrybirpbbxvrf'
for i in range(0, 26):
pt = rot(ciphertext, i)
if 'infosec' in pt or 'flag' in pt:
print("\033[91mKey: %d, Plaintext: %s\033[0m" % (i, pt))
else:
print("Key: %d, Plaintext: %s" % (i, pt))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment