Пример #1
0
# 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)))
Пример #2
0
# 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)))
Пример #3
0
# 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)))
Пример #4
0
# 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))
Пример #5
0
# 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))
Пример #6
0
# 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)))
Пример #7
0
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"
Пример #8
0
# 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))
Пример #9
0
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
 
Пример #10
0
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
Пример #11
0
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()
Пример #12
0
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)
Пример #13
0
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
Пример #14
0
# 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))
Пример #15
0
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()
Пример #16
0
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"
Пример #17
0
# 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))
Пример #18
0
# 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))
Пример #19
0
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)
Пример #20
0
# 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)))