-
Notifications
You must be signed in to change notification settings - Fork 0
/
xorCrypto.py
34 lines (30 loc) · 1.15 KB
/
xorCrypto.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env python3
import argparse
import logging
def xorcrypt(cipher_text, key):
#Xor encryption implimentation
endRes = ""
if len(cipher_text) != len(key):
logging.error("cipher and key must be the same length")
else:
for i in range(0, len(cipher_text)):
#Converts a character from cipher_text and key to its decimal value
#Then xors the two
intResult = ord(cipher_text[i]) ^ ord(key[i])
#Convert intResult to its character representation
endRes += chr(intResult)
return endRes
def main():
#Argparse setup
parser = argparse.ArgumentParser(description="xorCrypt")
parser.add_argument("--key", type=argparse.FileType("r"), help="File containing the key")
parser.add_argument("--text", type=argparse.FileType("r"), help="File containing the text")
args = parser.parse_args()
if not args.key or not args.text:
logging.error("arguments required to run")
else:
#call xorcrypt using the input from the two files
res = xorcrypt(str(args.text.read()), str(args.key.read()))
print(res)
if __name__ == "__main__":
main()