pep up code

breakmono2
Eggert Jung 5 years ago
parent 8078694ee0
commit 7dec1c6eb9

@ -1,17 +1,20 @@
def encrypt(key, plaintext): def encrypt(key, plaintext):
out="" out = ""
for i in range(0, len(plaintext)): for i in range(0, len(plaintext)):
out = out + chr(((ord(key[i%len(key)])+ord(plaintext[i])-(0x61+0x61))%26)+0x61) out += chr(((ord(key[i % len(key)]) +
ord(plaintext[i]) - (2 * 0x61)) % 26)+0x61)
return out return out
def decrypt(key, cipertext): def decrypt(key, cipertext):
out="" out = ""
for i in range(0, len(cipertext)): for i in range(0, len(cipertext)):
pln = ord(cipertext[i])-(ord(key[i%len(key)]) - 0x61) pln = ord(cipertext[i])-(ord(key[i % len(key)]) - 0x61)
pln = pln + 26 if pln < 0x61 else pln pln = pln + 26 if pln < 0x61 else pln
out += chr(pln) out += chr(pln)
return out return out
if __name__ == "__main__": if __name__ == "__main__":
import argparse import argparse
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
@ -22,11 +25,11 @@ if __name__ == "__main__":
parser.add_argument('--out', metavar='FILE') parser.add_argument('--out', metavar='FILE')
args = parser.parse_args() args = parser.parse_args()
#strip non-alphabetic chars from file and convert to lower case # strip non-alphabetic chars from file and convert to lower case
txt = ''.join([x for x in open(args.FILE, "r").read().lower() if x.isalpha()]) t = ''.join([x for x in open(args.FILE, "r").read().lower() if x.isalpha()])
if(args.encrypt != None): if(args.encrypt != None):
print(encrypt(args.encrypt, txt)) print(encrypt(args.encrypt, t))
if(args.decrypt != None): if(args.decrypt != None):
print(decrypt(args.decrypt, txt)) print(decrypt(args.decrypt, t))

Loading…
Cancel
Save