add decrypt and update encrypt

breakmono2
Eggert Jung 5 years ago
parent 723b2bbf66
commit bf106bf76d

@ -1,13 +1,16 @@
def encrypt(key, plaintext):
pln_bytes = bytes(plaintext, 'UTF-8')
key_bytes = bytes(key, 'UTF-8')
out=""
for i in range(0, len(plaintext)):
out = out + chr(((ord(key[i%len(key)])+ord(plaintext[i])-(0x61+0x61))%26)+0x61)
return 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)
def decrypt(key, cipertext):
out=""
for i in range(0, len(cipertext)):
pln = ord(cipertext[i])-(ord(key[i%len(key)]) - 0x61)
if pln < 0x61:
pln = pln + 26
out = out + chr(pln)
return out
if __name__ == "__main__":
@ -20,5 +23,15 @@ if __name__ == "__main__":
parser.add_argument('--out', metavar='FILE')
args = parser.parse_args()
fd = open(args.FILE, "r")
txt_in = fd.read()
txt = ""
for i in txt_in.lower():
if i.isalpha():
txt = txt + i
if(args.encrypt != None):
print(encrypt(args.encrypt, txt))
print(encrypt("bbb", "test"))
if(args.decrypt != None):
print(decrypt(args.decrypt, txt))

Loading…
Cancel
Save