|
#!/bin/python |
|
|
|
# Daniel Boerlage 2015 |
|
|
|
# warning: |
|
# this code sucks |
|
|
|
# this is my first script written in python |
|
# no effort was put into optimizing it or using proper data structures |
|
|
|
import sys |
|
|
|
level_of_encryption = 1 |
|
should_decrypt = False |
|
|
|
|
|
if sys.argv[1] == '-d' or sys.argv[1] == '--decrypt': |
|
should_decrypt = True |
|
if len(sys.argv) > 3: |
|
level_of_encryption = int(sys.argv[2]) |
|
infile = open(sys.argv[3], 'rb') |
|
if infile.name.endswith('.navajo'): |
|
outfile = open(sys.argv[3][:sys.argv[3].index('.navajo')], 'w+') |
|
else: |
|
outfile = open(sys.argv[3] + '.decoded', 'w+') |
|
else: |
|
infile = open(sys.argv[2], 'rb') |
|
if infile.name.endswith('.navajo'): |
|
outfile = open(sys.argv[2][:sys.argv[2].index('.navajo')], 'w+') |
|
else: |
|
outfile = open(sys.argv[2] + '.decoded', 'w+') |
|
else: |
|
if len(sys.argv) > 2: |
|
level_of_encryption = int(sys.argv[1]) |
|
infile = open(sys.argv[2], 'rb') |
|
outfile = open(sys.argv[2] + '.navajo', 'w+') |
|
else: |
|
infile = open(sys.argv[1], 'rb') |
|
outfile = open(sys.argv[1] + '.navajo', 'w+') |
|
|
|
|
|
|
|
|
|
alaih_bin = '01100001001001110110110001100001001001110110100101101000' |
|
|
|
donehlini_bin = '0110010001101111001001110110111001100101011010000010011101101100011010010110111001101001' |
|
|
|
def translate(_bin, level): |
|
if level == 1: |
|
return ' '.join(["a'la'ih" if bit == '1' else "do'neh'lini" for bit in _bin]) |
|
return ' '.join([translate(alaih_bin if bit == '1' else donehlini_bin, level-1) for bit in _bin]) |
|
|
|
def to_bin(str): |
|
return ''.join(['{0:08b}'.format(ord(char)) for char in str]) |
|
|
|
def navajo_to_bin(str): |
|
return '1' if str == "a'la'ih" else '0' |
|
|
|
def decode(str, level): |
|
_bin = [navajo_to_bin(word) for word in str.split(' ')] |
|
#print _bin; |
|
while level > 1: |
|
next_bin = [] |
|
while len(_bin) > 0: |
|
#print _bin[:6] |
|
if _bin[:6] == ['0', '1', '1', '0', '0', '0']: |
|
next_bin.extend(['1']) |
|
del _bin[:56] |
|
else: |
|
next_bin.extend(['0']) |
|
del _bin[:88] |
|
#print _bin |
|
_bin = next_bin |
|
level -= 1; |
|
_bin = ''.join(_bin) |
|
#print _bin |
|
return ''.join(chr(int(_bin[i:i+8], 2)) for i in xrange(0, len(_bin), 8)) |
|
|
|
def encrypt(infile, outfile): |
|
outfile.write(translate(to_bin(infile.read()), level_of_encryption)) |
|
|
|
def decrypt(infile, outfile): |
|
outfile.write(decode(infile.read(), level_of_encryption)) |
|
|
|
if should_decrypt: |
|
decrypt(infile, outfile) |
|
else: |
|
encrypt(infile, outfile) |