-
Notifications
You must be signed in to change notification settings - Fork 0
/
aes.py
88 lines (68 loc) · 2.38 KB
/
aes.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
from Crypto.Cipher import AES
from Crypto import Random
from Crypto.Util import Counter
def encrypt_cbc(key, plaintext, iv=None):
'''
inputs are all hex string
'''
iv = Random.new().read(AES.block_size) if iv is None else bytes.fromhex(iv)
cipher = AES.new(bytes.fromhex(key), AES.MODE_CBC, iv)
return cipher.encrypt(bytes.fromhex(plaintext))
def decrypt_cbc(key, ciphertext):
'''
inputs are all hex string
'''
ciphertext_bytes = bytes.fromhex(ciphertext)
iv, c = ciphertext_bytes[:16], ciphertext_bytes[16:]
cipher = AES.new(bytes.fromhex(key), AES.MODE_CBC, iv)
d = cipher.decrypt(c)
return d[:-d[-1]].decode()
def encrypt_ctr(key, plaintext, iv=None):
'''
inputs are all hex string
'''
iv = Random.new().read(AES.block_size) if iv is None else bytes.fromhex(iv)
ctr = Counter.new(128)
cipher = AES.new(bytes.fromhex(key), AES.MODE_CTR, iv, ctr)
return cipher.encrypt(bytes.fromhex(plaintext))
def decrypt_ctr(key, ciphertext):
'''
inputs are all hex string
'''
ciphertext_bytes = bytes.fromhex(ciphertext)
iv, c = ciphertext_bytes[:16], ciphertext_bytes[16:]
ctr = Counter.new(128)
cipher = AES.new(bytes.fromhex(key), AES.MODE_CTR, iv, ctr)
d = cipher.decrypt(c)
return d
def str_to_hex(s):
return ''.join([format(ord(c), '02x') for c in s])
def hex_to_str(s, prefix=''):
if len(s) == 0:
return prefix + s
return prefix + hex_to_str(s[2:], chr(int(s[:2], base=16)))
def example_cbc():
key = 'This is a key123'.encode()
iv = 'This is an IV456'.encode()
obj = AES.new(key, AES.MODE_CBC, iv)
message = "The answer is no".encode()
ciphertext = obj.encrypt(message)
print(ciphertext)
obj = AES.new(key, AES.MODE_CBC, iv)
print(obj.decrypt(ciphertext))
def example_ctr():
key = 'This is a key123'.encode()
iv = 'This is an IV456'.encode()
ctr = Counter.new(128)
obj = AES.new(key, AES.MODE_CTR, iv, ctr)
message = "The answer is no".encode()
ciphertext = obj.encrypt(message)
print(ciphertext)
ctr = Counter.new(128)
obj = AES.new(key, AES.MODE_CTR, iv, ctr)
print(obj.decrypt(ciphertext))
if __name__ == '__main__':
key = '36f18357be4dbd77f050515c73fcf9f2'
c = '770b80259ec33beb2561358a9f2dc617e46218c0a53cbeca695ae45faa8952aa0e311bde9d4e01726d3184c34451'
d = decrypt_ctr(key, c)
print(d)