-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAES_CTR.py
46 lines (36 loc) · 1.43 KB
/
AES_CTR.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
from Crypto.Cipher import AES
from Crypto import Random
from Crypto.Util import Counter
BS = AES.block_size
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
unpad = lambda s : s[0:-ord(s[-1])]
class AESCipher:
def __init__(self, key):
self.key = key.decode("hex")
def encrypt(self, pt):
#raw = pad(pt)
iv = Random.new().read(BS);
# Return iv || ct
ctr = Counter.new(128, initial_value = long(iv.encode("hex"), BS))
cipher = AES.new(self.key, AES.MODE_CTR, counter = ctr)
return (iv + cipher.encrypt(pt)).encode("hex")
def decrypt(self, ct):
ct = ct.decode("hex")
# Extract iv from ciphertext
iv = ct[:BS]
ct = ct[BS:]
ctr = Counter.new(128, initial_value = long(iv.encode("hex"), BS))
cipher = AES.new(self.key, AES.MODE_CTR, counter = ctr)
return unpad(cipher.decrypt(ct))
if __name__== "__main__":
key = '012345678'
plaintext = 'abcdefghijkmnopqa1234s1222s';
# AES key must be either 16, 24, or 32 bytes long. Hence padding with block_size
key = pad(key).encode('hex')
# AES Input strings must be a multiple of 16 in length. Hence padding with block_size
plaintext = pad(plaintext)
crypto = AESCipher(key)
ciphertext = crypto.encrypt(plaintext)
print "Cipher text - %s" % ciphertext
plaintext = crypto.decrypt(ciphertext)
print "Plain text - %s" % plaintext