-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathaes_cbc.py
More file actions
55 lines (39 loc) · 1.46 KB
/
aes_cbc.py
File metadata and controls
55 lines (39 loc) · 1.46 KB
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
from Crypto import Random
from Crypto.Cipher import AES
import base64
import random
BS = 16 #any multiple of 16
def pad(s): return s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
# padding of the input text is done to match up the block size (16,32..)
def unpad(s): return s[0:-ord(s[-1:])]
# the unpadding in python 2 might differ in ord(s[-1]) part
key = ''
key = [key + str((random.randint(0, 9))) for _ in range(16)]
key = ''.join(key)
print (key)
def encrypt(text):
text = pad(text)
# padding of the input text is done to match the byte length of 16
IV = Random.new().read(AES.block_size)
# having a random Initialization vector adds unique randomess to the start of the encryption process
cipher = AES.new(key, mode=AES.MODE_CBC, IV=IV)
cipher_text = cipher.encrypt(text)
key1 = key.encode('utf-8')
base_value = base64.b64encode(IV + key1 + cipher_text)
# all the arguments has to be of bytes type
return base_value
def decrypt(cipher_text):
cipher_text = base64.b64decode(cipher_text)
IV = cipher_text[:16]
key = cipher_text[16:32]
data = cipher_text[32:]
decryptor = AES.new(key, mode=AES.MODE_CBC, IV=IV)
plain_text = (decryptor.decrypt(data)).decode('utf-8')
# decoding the plain text from bytes to str
return unpad(plain_text)
if __name__ == "__main__":
text = "your text here"
ciphertext = encrypt(text)
print (ciphertext)
actualtext = decrypt(ciphertext)
print(actualtext)