Last active
September 1, 2016 15:47
-
-
Save terrettaz/271461622e378578b2c8db31fa00ba5c to your computer and use it in GitHub Desktop.
Vigenere Cipher tool
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 python | |
import sys, argparse, random | |
start = ord('.') | |
end = ord('Z') | |
modulo = (end - start) + 1 | |
def alphabet(): | |
return reduce(lambda a, b: '%s%s' % (a,b), map(lambda x: chr(x), range(start, end+1))) | |
def print_alphabet(): | |
print alphabet() | |
def print_table(): | |
print ' %s' % alphabet() | |
line = alphabet() | |
for c in alphabet(): | |
print '%s %s' % (c, line) | |
line = line[1:] + line[0] | |
def random_key(): | |
al = alphabet() | |
return reduce(lambda a, b: '%s%s' % (a,b), map(lambda x: al[random.randint(0,len(al)-1)], range(0,11))) | |
def validate(text): | |
for c in text: | |
code = ord(c) | |
if code < start or code > end: | |
raise Exception("char '%s' in text '%s' not in vigenere table" % (c, text)) | |
def extend_key(key, text): | |
while(len(key) < len(text)): | |
key = key + key | |
return key[0:len(text)] | |
def encrypt(key, text): | |
cipher = '' | |
for i in range(0, len(text)): | |
t = ord(text[i]) - start | |
k = ord(key[i]) - start | |
code = ((t + k) % (modulo)) + start | |
ch = chr(code) | |
cipher += chr(code) | |
return cipher | |
def decrypt(key, cipher): | |
text = '' | |
for i in range(0, len(cipher)): | |
t = ord(cipher[i]) - start | |
k = ord(key[i]) - start | |
code = ((t - k) % (modulo)) + start | |
ch = chr(code) | |
text += chr(code) | |
return text | |
def process(processor, key, text): | |
text = text.upper() | |
key = extend_key(key.upper(), text) | |
validate(key) | |
validate(text) | |
print "%s" % (processor(key, text)) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='Vigenere Cipher') | |
parser.add_argument('-A', '--alphabet', dest='print_alphabet', action="store_true", help='Print the alphabet') | |
parser.add_argument('-T', '--table', dest='print_table', action="store_true", help='Print the Vigenere table') | |
parser.add_argument('-d', '--decrypt', dest='processor', const=decrypt, default=encrypt, action="store_const", help='Decipher the text') | |
parser.add_argument('-k', '--key', dest='key', help='Vigenere key') | |
parser.add_argument('text', action='store', nargs="?", help='Text to (de)cipher. If not provided, read from stdin') | |
args = parser.parse_args() | |
if args.print_alphabet: | |
print_alphabet() | |
sys.exit(0) | |
if args.print_table: | |
print_table() | |
sys.exit(0) | |
if args.key: key = args.key | |
else: | |
key = random_key() | |
sys.stderr.write("Missing -k key, using random '%s'\n" % key) | |
if not args.text: | |
for text in sys.stdin: | |
if text[-1] == '\n': text = text[:-1] | |
process(args.processor, key, text) | |
else: | |
process(args.processor, key, args.text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment