-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAES.py
93 lines (65 loc) · 2.24 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
89
90
91
92
93
import hashlib, binascii, hmac
import os, scrypt, secrets
import json, pyaes
from Crypto.Cipher import AES
# 1.1) Symmetric Encryption & Decryption
message = 'exercise-cryptograpy'
password = 'p@$$s0rd~3'
# Scrypt Key Derivation (512 bit = 64 bytes)
salt = os.urandom(32)
print("Salt (256): ", binascii.hexlify(salt))
n = 16384
r = 16
p = 1
derived_key_length = 64
derivedKey = scrypt.hash(password, salt, n, r, p, derived_key_length)
derivedKey_hex = binascii.hexlify(derivedKey)
print("Derived Key: ", derivedKey_hex)
dklen = derived_key_length
# Split Derived Key --> Encryption Key (1st 256 bits)
encryptionKey = derivedKey_hex[:64]
print("Encryption Key: ", encryptionKey)
## for AES CBC (32 bytes)
encryptionKey32 = derivedKey_hex[:32]
print("Encryption Key (32): ", encryptionKey32)
# Split Derived Key --> HMAC Key (2nd 256 bits)
hmacKey = derivedKey_hex[64:]
print("HMAC Key: ", hmacKey)
# AES Message Encryption --> Cipher Text w/ PKCS7 Padding
iv = (secrets.randbelow(256)).to_bytes(16, byteorder = 'big')
print("Initialization Vector (iv16): ", iv)
# block_size = 16
def pad(m):
return m+chr(16-len(m)%16)*(16-len(m)%16)
# return m+chr(AES.block_size-len(m)%AES.block_size)*(AES.block_size-len(m)%AES.block_size)
def unpad(ct):
return ct[:-ct[-1]]
# return ct[:-ord(ct[-1])]
#aes = pyaes.AESModeOfOperationCBC(encryptionKey32, iv)
aes = AES.new(encryptionKey32, AES.MODE_CBC, iv)
cipherText = aes.encrypt(pad(message).encode('utf8'))
print("Encrypted Message: ", binascii.hexlify(cipherText))
# AES Message Decryption --> Plain Text
aes = AES.new(encryptionKey32, AES.MODE_CBC, iv)
plainText = unpad(aes.decrypt(cipherText))
print("Decrypted Message: ", plainText)
# Hash-based MAC using HMAC-SHA256(message, hmac_key)
def hmac_sha256(key, msg):
return hmac.new(key, msg, hashlib.sha256).digest()
key = binascii.unhexlify(hmacKey)
msg = message.encode('utf8')
hmac = binascii.hexlify(hmac_sha256(key, msg))
print("HMAC: ", hmac)
outPut = {
"Scrypt": {
"dklen": str(dklen),
"Salt": str(binascii.hexlify(salt)),
"n": str(n),
"r": str(r),
"p": str(p),
},
"aes": str(binascii.hexlify(cipherText)),
"iv": str(binascii.hexlify(iv)),
"mac": str(hmac),
}
print(json.dumps(outPut))