Skip to content

Instantly share code, notes, and snippets.

@tech2077
Created October 1, 2012 02:45
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 tech2077/3809202 to your computer and use it in GitHub Desktop.
Save tech2077/3809202 to your computer and use it in GitHub Desktop.
A rot cypher decryption toolkit
import re
def findall(src, key):
'''Returns list of all instances of a key item in src list'''
return [m.start() for m in re.finditer(key, src)]
def insert(original, new, pos):
'''Inserts new inside original at pos.'''
return original[:pos] + new + original[pos:]
def rotcrack(code):
'''Brute force cracking of rotational cypher'''
'''Returns list of all rotations for ROT-n n=1-26'''
results = []
for n in range(-26, 0):
tmp = []
for char in code:
if char == ' ':
tmp.append(' ')
else:
tmp.append(chr((ord(char)%97 - n)%26 + 97))
results.append('ROT-' + str(-n) + ': ' + ''.join(tmp))
return results
def rotkeycrack(assumed, code):
'''Cracks rotational key based cyphers based on a found word and the encrpted version'''
'''Returns key string '''
result = []
for x in map((lambda x, y: [x, y]), assumed.replace(' ', ''), code):
result.append(chr((ord(x[1])%97 - ord(x[0])%97)%26 + 97))
return ''.join(result)
def rotkeysolver(code, key):
'''Solves rotational cypher code based off key'''
'''Returns solved string, remove punctuation before using'''
output = []
for x in map((lambda x, y: [x, y]), code.lower().replace(' ', ''), key*(len(code)/len(key))):
if x[0] == None:
output.append('')
else:
output.append(chr((ord(x[0])%97 - (ord(x[1])%97))%26 + 97))
result = ''.join(output)
for x in findall(code, ' '):
result = insert(result, ' ', x)
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment