-
Notifications
You must be signed in to change notification settings - Fork 15
/
aescbc.py
56 lines (45 loc) · 1.43 KB
/
aescbc.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
#!/usr/bin/env python3
import sys,os
from Crypto.Cipher import AES
from settings import *
mode = AES.MODE_CBC
# PKCS7 padding: adding b bytes each worth b
def padding(text):
b = BYTE_NB - (len(text) % BYTE_NB)
return text + chr(b)*b # PKCS7 padding
def unpadding(data):
return data[:-data[-1]]
# AES CBC Encryption
def encryption(text):
encryptor = AES.new(key, mode, IV=IV)
padded_text = padding(text)
return encryptor.encrypt(padded_text)
# AES CBC decryption without padding
def decryption(encrypted):
decryptor = AES.new(key, mode, IV=IV)
return decryptor.decrypt(encrypted)
# Ckeck validity of PKCS7 padding
def pkcs7_padding(data):
pkcs7 = True
last_byte_padding = data[-1]
if(last_byte_padding < 1 or last_byte_padding > 16):
pkcs7 = False
else:
for i in range(0,last_byte_padding):
if(last_byte_padding != data[-1-i]):
pkcs7 = False
return pkcs7
#### Script ####
usage = """
Usage:
python3 paescbc.py <message> encrypts and displays the message (output in hex format)
python3 aescbc.py -d <hex code> decrypts and displays the message
Cryptographic parameters can be changed in settings.py
"""
if __name__ == '__main__':
if len(sys.argv) == 2 :
print(encryption(sys.argv[1]).hex())
elif len(sys.argv) == 3 and sys.argv[1] == '-d' :
print(unpadding(decryption(bytes.fromhex(sys.argv[2]))))
else:
print(usage)