@nakov/

AES-256-GCM with Scrypt in Python

Python

NOT working properly yet (too slow) - AES-256-GCM + Scrypt in Python

fork
loading
Files
  • main.py
  • requirements.txt
main.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
from Crypto.Cipher import AES
import pyscrypt, os, binascii

def encrypt_AES_GCM(msg, password):
  kdfSalt = os.urandom(16)
  secretKey = pyscrypt.hash(password, kdfSalt, 1024, 8, 1, 32) # slower but more correct by 16384
  aesCipher = AES.new(secretKey, AES.MODE_GCM)
  ciphertext, authTag = aesCipher.encrypt_and_digest(msg)
  return (kdfSalt, ciphertext, aesCipher.nonce, authTag)

def decrypt_AES_GCM(encryptedMsg, password):
  (kdfSalt, ciphertext, nonce, authTag) = encryptedMsg
  secretKey = pyscrypt.hash(password, kdfSalt, 1024, 8, 1, 32) # slower but more correct by 16384
  aesCipher = AES.new(secretKey, AES.MODE_GCM, nonce)
  plaintext = aesCipher.decrypt_and_verify(ciphertext, authTag)
  return plaintext

msg = b'Message for AES-256-GCM + Scrypt encryption'
password = b's3kr3tp4ssw0rd'
encryptedMsg = encrypt_AES_GCM(msg, password)
print("encryptedMsg", {
'kdfSalt': binascii.hexlify(encryptedMsg[0]),
'ciphertext': binascii.hexlify(encryptedMsg[1]),
'aesIV': binascii.hexlify(encryptedMsg[2]),
'authTag': binascii.hexlify(encryptedMsg[3])
})

decryptedMsg = decrypt_AES_GCM(encryptedMsg, password)
print("decryptedMsg", decryptedMsg)