Last active
May 4, 2018 13:59
-
-
Save notdodo/2aa58d1a6e3d14b999763b9b5218aa91 to your computer and use it in GitHub Desktop.
Caesar Cipher bruteforcer with basic support for advanced string trasformation
This file contains 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/env python3 | |
import sys | |
import base64 | |
try: | |
import click | |
except Exception as e: | |
print(e) | |
print("Install click") | |
sys.exit(-1) | |
MAX_KEY_SIZE = 256 | |
def printBrute(msg, after=None): | |
for key in range(1, MAX_KEY_SIZE + 1): | |
try: | |
attempt = getTranslatedMessage(msg, key) | |
if after: | |
print('[{:0>3}]'.format(key), after(attempt).decode()) | |
else: | |
print('[{:0>3}]'.format(key), attempt) | |
except: | |
pass | |
def getTranslatedMessage(message, key): | |
result = "" | |
for char in message: | |
if char.isalpha(): | |
lower_char = char.lower() | |
new_ord = chr((ord(lower_char) - ord('a') + key) % 26 + ord('a')) | |
if char == char.upper(): | |
new_ord = new_ord.upper() | |
result += new_ord | |
else: | |
result += char | |
return result | |
@click.command() | |
@click.option( | |
"--key", | |
"-k", | |
nargs=1, | |
type=click.IntRange(0, MAX_KEY_SIZE), | |
default=13, | |
help="encrypt message with `key`") | |
@click.option("--brute", "-b", is_flag=True, help="Bruteforce message") | |
@click.argument("message") | |
@click.option("--extra", "-e", nargs=1, type=click.Choice(["base64_after"])) | |
def cmd(message, brute, key=None, extra=None): | |
if brute: | |
if extra == "base64_after": | |
printBrute(message, base64.b64decode) | |
else: | |
printBrute(message) | |
else: | |
print(getTranslatedMessage(message, key)) | |
if __name__ == '__main__': | |
cmd() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment