# Shift, affine, vigenere - key length known, vigenere - key length unknown, generic inpath = './tests/texts/' outpath = './tests/encodes/' import ciphers from os import listdir fs = listdir(inpath) for fn in fs: f = open(inpath+fn) text = f.read() f.close() text = ciphers.str_to_arr(text) outname = outpath+fn # Shift, n = 1 encoded = ciphers.arr_to_str(ciphers.shift_encode(text, 1)) fout = open(outname+'_shift_1.in', mode='w+') fout.write(encoded) fout.close() # Affine, a = 3, b = 4 encoded = ciphers.arr_to_str(ciphers.affine_encode(text,3,4)) fout = open(outname+'_affine_1.in', mode='w+') fout.write(encoded) fout.close() # Vigenere length known & unknown, key = apple
# Input: Text # Output: Most probable vigenere-decoded message of text, where key length is unknown import sys from ciphers import str_to_arr, arr_to_str, encode, vigenere_decode v = sys.stdin.read() i = v.index('\n') message = str_to_arr(v[i + 1:-1]) m = vigenere_generic_decode(message) # Grab key length from mapping n = len(m[0]) print arr_to_str(encode(message, n, m))
# Input: A generic, single substitution encoded text # Output: Most probable single substitution decoded text import sys from ciphers import arr_to_str, str_to_arr, encode, generic_decode message = str_to_arr(sys.stdin.read()[:-1]) print arr_to_str(encode(message, 1, generic_decode(message)))
# Input: A vigenere key on line 1 followed by text # Output: text encoded by the vignere cipher using key import sys from ciphers import arr_to_str, str_to_arr, vigenere_encode v = sys.stdin.read().upper() i = v.index('\n') key = str_to_arr(v[0:i]) message = str_to_arr(v[i+1:-1]) print arr_to_str(vigenere_encode(message, key))
inpath = './tests/encodes/' outpath = './tests/decodes/' import ciphers from os import listdir fs = listdir(inpath) for fn in fs: print 'Decoding ', fn f = open(inpath+fn) text = f.read() f.close() text = ciphers.str_to_arr(text) i = fn.find('_') j = fn.rfind('_') rule = fn[i+1:j] decoded = [] if rule == 'shift': decoded = ciphers.shift_decode(text) elif rule == 'affine': decoded = ciphers.affine_decode(text) elif rule[0:3] == 'vig' and rule[3] != 'g': n = int(rule[3:j]) decoded = ciphers.vigenere_decode(text, n) elif rule == 'viggen': decoded = ciphers.vigenere_generic_decode(text) elif rule == 'generic': decoded = [ciphers.generic_decode(text)]
# Input: Text # Output: Most probable vigenere-decoded message of text, where key length is unknown import sys from ciphers import str_to_arr, arr_to_str, encode, vigenere_decode v = sys.stdin.read() i = v.index('\n') message = str_to_arr(v[i+1:-1]) m = vigenere_generic_decode(message) # Grab key length from mapping n = len(m[0]) print arr_to_str(encode(message, n, m))
# Input: A vigenere key on line 1 followed by text # Output: text encoded by the vignere cipher using key import sys from ciphers import arr_to_str, str_to_arr, vigenere_encode v = sys.stdin.read().upper() i = v.index('\n') key = str_to_arr(v[0:i]) message = str_to_arr(v[i + 1:-1]) print arr_to_str(vigenere_encode(message, key))
import sys from ciphers import arr_to_str, str_to_arr, affine_encode, affine_decode, encode v = sys.stdin.read().upper() i = v.index('\n') [a,b] = v[:i].split(' ') a = int(a) b = int(b) message = str_to_arr(v[i+1:-1]) encoded = arr_to_str(affine_encode(message, a, b)) print encoded decoded = arr_to_str(encode(str_to_arr(encoded), 1, affine_decode(str_to_arr(encoded)))) message = arr_to_str(message) if message == decoded: print "TRUE: 100% MATCH" else: matches = 0 total = 0 for i in range(len(encoded)): if "ABCDEFGHIJKLMNOPQRSTUVWXYZ".count(message[i]) == 1: if message[i] == decoded[i]: matches += 1 total += 1 print "FAlSE: " + str(float(matches)/total * 100) + "% MATCH" #print decoded
import sys from ciphers import arr_to_str, str_to_arr, shift_encode, shift_decode, encode v = sys.stdin.read().upper() i = v.index('\n') shift_amt = int(v[:i]) message = str_to_arr(v[i+1:-1]) encoded = arr_to_str(shift_encode(message, shift_amt)) print encoded decoded = arr_to_str(encode(str_to_arr(encoded), 1, shift_decode(str_to_arr(encoded)))) message = arr_to_str(message) if message == decoded: print "TRUE: 100% MATCH" else: matches = 0 total = 0 for i in range(len(encoded)): if "ABCDEFGHIJKLMNOPQRSTUVWXYZ".count(message[i]) == 1: if message[i] == decoded[i]: matches += 1 total += 1 print "FAlSE: " + str(float(matches)/total * 100) + "% MATCH"
inpath = './tests/encodes/' outpath = './tests/decodes/' import ciphers from os import listdir fs = listdir(inpath) for fn in fs: print 'Decoding ', fn f = open(inpath + fn) text = f.read() f.close() text = ciphers.str_to_arr(text) i = fn.find('_') j = fn.rfind('_') rule = fn[i + 1:j] decoded = [] if rule == 'shift': decoded = ciphers.shift_decode(text) elif rule == 'affine': decoded = ciphers.affine_decode(text) elif rule[0:3] == 'vig' and rule[3] != 'g': n = int(rule[3:j]) decoded = ciphers.vigenere_decode(text, n) elif rule == 'viggen': decoded = ciphers.vigenere_generic_decode(text) elif rule == 'generic': decoded = [ciphers.generic_decode(text)]
import sys from ciphers import arr_to_str, str_to_arr, vigenere_encode, vigenere_decode, encode v = sys.stdin.read().upper() i = v.index('\n') key = str_to_arr(v[0:i]) message = str_to_arr(v[i+1:-1]) encoded = arr_to_str(vigenere_encode(message, key)) print encoded decoded = arr_to_str(encode(str_to_arr(encoded), len(key), vigenere_decode(str_to_arr(encoded),len(key)))) message = arr_to_str(message) if message == decoded: print "TRUE: 100% MATCH" else: matches = 0 total = 0 for i in range(len(encoded)): if "ABCDEFGHIJKLMNOPQRSTUVWXYZ".count(message[i]) == 1: if message[i] == decoded[i]: matches += 1 total += 1 print "FAlSE: " + str(float(matches)/total * 100) + "% MATCH" print decoded
# Shift, affine, vigenere - key length known, vigenere - key length unknown, generic inpath = './tests/texts/' outpath = './tests/encodes/' import ciphers from os import listdir fs = listdir(inpath) for fn in fs: f = open(inpath + fn) text = f.read() f.close() text = ciphers.str_to_arr(text) outname = outpath + fn # Shift, n = 1 encoded = ciphers.arr_to_str(ciphers.shift_encode(text, 1)) fout = open(outname + '_shift_1.in', mode='w+') fout.write(encoded) fout.close() # Affine, a = 3, b = 4 encoded = ciphers.arr_to_str(ciphers.affine_encode(text, 3, 4)) fout = open(outname + '_affine_1.in', mode='w+') fout.write(encoded) fout.close() # Vigenere length known & unknown, key = apple
import sys from ciphers import arr_to_str, str_to_arr, vigenere_encode, vigenere_decode, encode v = sys.stdin.read().upper() i = v.index('\n') key = str_to_arr(v[0:i]) message = str_to_arr(v[i + 1:-1]) encoded = arr_to_str(vigenere_encode(message, key)) print encoded decoded = arr_to_str( encode(str_to_arr(encoded), len(key), vigenere_decode(str_to_arr(encoded), len(key)))) message = arr_to_str(message) if message == decoded: print "TRUE: 100% MATCH" else: matches = 0 total = 0 for i in range(len(encoded)): if "ABCDEFGHIJKLMNOPQRSTUVWXYZ".count(message[i]) == 1: if message[i] == decoded[i]: matches += 1 total += 1 print "FAlSE: " + str(float(matches) / total * 100) + "% MATCH" print decoded
import sys from ciphers import arr_to_str, str_to_arr, shift_encode, shift_decode, encode v = sys.stdin.read().upper() i = v.index('\n') shift_amt = int(v[:i]) message = str_to_arr(v[i + 1:-1]) encoded = arr_to_str(shift_encode(message, shift_amt)) print encoded decoded = arr_to_str( encode(str_to_arr(encoded), 1, shift_decode(str_to_arr(encoded)))) message = arr_to_str(message) if message == decoded: print "TRUE: 100% MATCH" else: matches = 0 total = 0 for i in range(len(encoded)): if "ABCDEFGHIJKLMNOPQRSTUVWXYZ".count(message[i]) == 1: if message[i] == decoded[i]: matches += 1 total += 1 print "FAlSE: " + str(float(matches) / total * 100) + "% MATCH"