|
|
|
|
@ -1,15 +1,20 @@
|
|
|
|
|
def encrypt(key, plaintext):
|
|
|
|
|
pln_bytes = bytes(plaintext, 'UTF-8')
|
|
|
|
|
key_bytes = bytes(key, 'UTF-8')
|
|
|
|
|
out=""
|
|
|
|
|
|
|
|
|
|
for i in range(len(pln_bytes)):
|
|
|
|
|
pln_chr_index = pln_bytes[i] - 0x61
|
|
|
|
|
key_chr_index = key_bytes[i % len(key)] - 0x61
|
|
|
|
|
out_chr_index = (pln_chr_index + key_chr_index) % 26
|
|
|
|
|
out = out + chr(out_chr_index + 0x61)
|
|
|
|
|
out = ""
|
|
|
|
|
for i in range(0, len(plaintext)):
|
|
|
|
|
out += chr(((ord(key[i % len(key)]) +
|
|
|
|
|
ord(plaintext[i]) - (2 * 0x61)) % 26)+0x61)
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def decrypt(key, cipertext):
|
|
|
|
|
out = ""
|
|
|
|
|
for i in range(0, len(cipertext)):
|
|
|
|
|
pln = ord(cipertext[i])-(ord(key[i % len(key)]) - 0x61)
|
|
|
|
|
pln = pln + 26 if pln < 0x61 else pln
|
|
|
|
|
out += chr(pln)
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
import argparse
|
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
|
@ -18,7 +23,18 @@ if __name__ == "__main__":
|
|
|
|
|
command_group.add_argument('--encrypt', metavar='KEY')
|
|
|
|
|
command_group.add_argument('--decrypt', metavar='KEY')
|
|
|
|
|
parser.add_argument('--out', metavar='FILE')
|
|
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
print(encrypt("bbb", "test"))
|
|
|
|
|
# strip non-alphabetic chars from file and convert to lower case
|
|
|
|
|
t = ''.join([x for x in open(args.FILE, "r").read().lower() if x.isalpha()])
|
|
|
|
|
|
|
|
|
|
if(args.encrypt != None):
|
|
|
|
|
out = encrypt(args.encrypt, t)
|
|
|
|
|
|
|
|
|
|
if(args.decrypt != None):
|
|
|
|
|
out = decrypt(args.decrypt, t)
|
|
|
|
|
|
|
|
|
|
if(args.out != None):
|
|
|
|
|
open(args.out, "w").write(out)
|
|
|
|
|
else:
|
|
|
|
|
print(out)
|
|
|
|
|
|