-
Notifications
You must be signed in to change notification settings - Fork 0
/
hex.py
40 lines (34 loc) · 972 Bytes
/
hex.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
35
36
37
38
39
40
import sys
mode = sys.argv[1]
keyfile = sys.argv[2]
inpfile = sys.argv[3]
key = open(keyfile).read()[:-1] #removes the mandatory \n at the end of the file to support one line messages.
inp = open(inpfile).read()[:-1] #removes the mandatory \n at the end of the file to support one line messages.
debug = False
if(debug):
print("mode: "+mode)
print("key: "+key)
print("inp: "+inp)
def loopKey(text, key):
fullKey = ""
x = 0
while x != len(text):
fullKey = fullKey + key[x % len(key)]
x = x + 1
return fullKey
def human(text, key):
ans = ""
for i in range(len(text)):
ans = ans + chr(ord(text[i]) ^ ord(key[i]))
return ans
def numOut(text, key):
ans = ""
for i in range(len(text)):
ans = ans + hex(ord(text[i]) ^ ord(key[i]))[2:] + " "
return ans
if (len(key) < len(inp)):
key = loopKey(inp, key)
if (mode == "human"):
print(human(inp, key))
else:
print(numOut(inp, key))