# Input: Encoded text # Output: Most probable shift-decoded message of text import sys from ciphers import str_to_arr, arr_to_str, encode, shift_decode message = str_to_arr(sys.stdin.read()[:-1]) print arr_to_str(encode(message, 1, shift_decode(message)))
# Input: An affine-encoded text # Output: Most probable affine-decoded message of text import sys from ciphers import arr_to_str, str_to_arr, encode, affine_decode message = str_to_arr(sys.stdin.read()[:-1]) print arr_to_str(encode(message, 1, affine_decode(message)))
# Input: 1 line specifying key length followed by text # Output: Most probable vigenere-decoded message of text, where key is length n import sys from ciphers import str_to_arr, arr_to_str, encode, vigenere_decode v = sys.stdin.read() i = v.index('\n') n = int(v[:i]) message = str_to_arr(v[i + 1:-1]) print arr_to_str(encode(message, n, vigenere_decode(message, n)))
# 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))
# 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))
# 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)))
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"
# Input: One line denoting a shift amount followed by text # Output: text shift-encoded by shift amount import sys from ciphers import arr_to_str, str_to_arr, shift_encode v = sys.stdin.read() i = v.index('\n') shift_amt = int(v[:i]) message = str_to_arr(v[i + 1:-1]) print arr_to_str(shift_encode(message, shift_amt))
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, 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
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)] else: continue # file name not supported message = ciphers.arr_to_str( ciphers.encode(text, len(decoded[0][0]), decoded[0])) fout = open(outpath + fn[:fn.rfind('.')] + '.out', 'w+') fout.write(message) fout.close()
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 key = ciphers.str_to_arr('apple') encoded = ciphers.arr_to_str(ciphers.vigenere_encode(text, key)) fout = open(outname + '_vig5_1.in', mode='w+') fout.write(encoded)
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
# Input: 1 Line specifying (a, b) for an affine cipher, followed by text # Output: text encoded by the affine cipher ax+b import sys from ciphers import arr_to_str, str_to_arr, affine_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]) print arr_to_str(affine_encode(message, a, b))
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)] else: continue # file name not supported message = ciphers.arr_to_str(ciphers.encode(text, len(decoded[0][0]), decoded[0])) fout = open(outpath+fn[:fn.rfind('.')]+'.out', 'w+') fout.write(message) fout.close()
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"
# 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: 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))
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 key = ciphers.str_to_arr('apple') encoded = ciphers.arr_to_str(ciphers.vigenere_encode(text, key)) fout = open(outname+'_vig5_1.in', mode='w+') fout.write(encoded)
# Input: 1 line specifying key length followed by text # Output: Most probable vigenere-decoded message of text, where key is length n import sys from ciphers import str_to_arr, arr_to_str, encode, vigenere_decode v = sys.stdin.read() i = v.index('\n') n = int(v[:i]) message = str_to_arr(v[i+1:-1]) print arr_to_str(encode(message, n, vigenere_decode(message, n)))