add decrypt and update encrypt
This commit is contained in:
@@ -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
|
||||
|
||||
print(encrypt("bbb", "test"))
|
||||
if(args.encrypt != None):
|
||||
print(encrypt(args.encrypt, txt))
|
||||
|
||||
if(args.decrypt != None):
|
||||
print(decrypt(args.decrypt, txt))
|
||||
|
||||
Reference in New Issue
Block a user