Last active
August 29, 2015 14:17
-
-
Save cmattoon/f9c80cf54930f258ef81 to your computer and use it in GitHub Desktop.
Caesar Cipher Solver
This file contains hidden or 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
#!/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