-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
143 lines (116 loc) · 3.86 KB
/
main.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import binascii
from Crypto.Cipher import AES
import os, sys
# Credit to Chris Coe for this code
# Requires pycrypto, which does indeed work for python3
def encrypt(key, raw):
'''
Takes in a string of clear text and encrypts it.
@param raw: a string of clear text
@return: a string of encrypted ciphertext
'''
if (raw is None) or (len(raw) == 0):
raise ValueError('input text cannot be null or empty set')
cipher = AES.AESCipher(key[:32], AES.MODE_ECB)
ciphertext = cipher.encrypt(raw)
return binascii.hexlify(bytearray(ciphertext)).decode('utf-8')
def decrypt(key, enc):
if (enc is None) or (len(enc) == 0):
raise ValueError('input text cannot be null or empty set')
enc = binascii.unhexlify(enc)
cipher = AES.AESCipher(key[:32], AES.MODE_ECB)
enc = cipher.decrypt(enc)
return enc#.decode('utf-8')
def bxor(b1, b2): # use xor for bytes
result = bytearray()
for b1, b2 in zip(b1, b2):
result.append(b1 ^ b2)
return result
def get_hex_iv():
return binascii.hexlify(os.urandom(16)).decode('utf-8')
def xor_hex_string(a, b):
c, d = binascii.unhexlify(a), binascii.unhexlify(b)
result = bxor(c, d)
return binascii.hexlify(result).decode('utf-8')
# Takes a hex string and binary key
# Returns binary encrypted data
def cbc_encrypt(key, hex, iv=""):
result = ""
if iv == "":
iv = get_hex_iv()
result += iv
hex += 'ff'
while len(hex) % 32 != 0:
hex += '00'
last_block = iv
for i in range(0, len(hex), 32):
before_enc = xor_hex_string(last_block, hex[i:i+32])
last_block = encrypt(key, binascii.unhexlify(before_enc))
result += last_block
return binascii.unhexlify(result)
# Returns binary result
def cbc_decrypt(key, hex):
result = ""
iv = hex[:32]#binascii.hexlify(decrypt(key, hex[:32])).decode('utf-8')
last_block = iv
for i in range(32, len(hex), 32):
decrypted = binascii.hexlify(decrypt(key, hex[i:i+32])).decode('utf-8')
message = xor_hex_string(decrypted, last_block)
last_block = hex[i:i+32]
result += message
result = result[:result.rfind('ff')]
return binascii.unhexlify(result)
if __name__ == "__main__":
input = ""
output = ""
keyfile=""
checkiv=0
for a in range(1,len(sys.argv)):
if sys.argv[a] == "-k":
keyfile = sys.argv[a+1]
if sys.argv[a] == "-v":
ivfile = sys.argv[a+1]
checkiv =1
if sys.argv[a]=="-o":
output=sys.argv[a+1]
if sys.argv[a] == "-i":
input = sys.argv[a + 1]
if sys.argv[a] == "-f":
function = sys.argv[a+1]
infile=open(input,"rb")
data= infile.read()
hex_data = binascii.hexlify(data).decode('utf-8')
infile.close()
outfile=open(output,"wb")
keyring=open(keyfile,"r")
key= binascii.unhexlify(keyring.read())
if checkiv:
ivhold=open(ivfile,"rb")
iv=ivhold.read()
else:
iv = get_hex_iv()
last_block=iv
if function == "encrypt":
answer = cbc_encrypt(key, hex_data, iv)
elif function == "decrypt":
answer = cbc_decrypt(key, hex_data)
outfile.write(answer)
if False and __name__ == "__main__":
key = bytes("1234567890abcdef1234567890abcdef", encoding='utf-8')
hex_data = binascii.hexlify(bytes("1234567890abcdef1234567890abcdef1234567890abcdefpoi", encoding='utf-8')).decode('utf-8')
ct = encrypt(key, "1234567890abcdef")
dt = decrypt(key, ct)
rnd = get_hex_iv()
print(ct)
print(dt)
print(rnd)
r = xor_hex_string('11', '22')
print(r)
print(xor_hex_string(r, '11'))
print(xor_hex_string(r, '22'))
print(binascii.unhexlify(hex_data))
print(hex_data)
encrypted = cbc_encrypt(key, hex_data)
print(encrypted)
decrypted = cbc_decrypt(key, encrypted)
print(decrypted.decode('utf-8'))