add decrypt and update encrypt
This commit is contained in:
@@ -1,13 +1,16 @@
|
|||||||
def encrypt(key, plaintext):
|
def encrypt(key, plaintext):
|
||||||
pln_bytes = bytes(plaintext, 'UTF-8')
|
|
||||||
key_bytes = bytes(key, 'UTF-8')
|
|
||||||
out=""
|
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)):
|
def decrypt(key, cipertext):
|
||||||
pln_chr_index = pln_bytes[i] - 0x61
|
out=""
|
||||||
key_chr_index = key_bytes[i % len(key)] - 0x61
|
for i in range(0, len(cipertext)):
|
||||||
out_chr_index = (pln_chr_index + key_chr_index) % 26
|
pln = ord(cipertext[i])-(ord(key[i%len(key)]) - 0x61)
|
||||||
out = out + chr(out_chr_index + 0x61)
|
if pln < 0x61:
|
||||||
|
pln = pln + 26
|
||||||
|
out = out + chr(pln)
|
||||||
return out
|
return out
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
@@ -20,5 +23,15 @@ if __name__ == "__main__":
|
|||||||
parser.add_argument('--out', metavar='FILE')
|
parser.add_argument('--out', metavar='FILE')
|
||||||
|
|
||||||
args = parser.parse_args()
|
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