-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathSCEE.py
More file actions
69 lines (51 loc) · 2.25 KB
/
SCEE.py
File metadata and controls
69 lines (51 loc) · 2.25 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from Crypto.Hash import SHA256, HMAC
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
from Crypto.Protocol.KDF import PBKDF2
import base64
ALGORITHM_NONCE_SIZE = 12
ALGORITHM_TAG_SIZE = 16
ALGORITHM_KEY_SIZE = 16
PBKDF2_SALT_SIZE = 16
PBKDF2_ITERATIONS = 32767
PBKDF2_LAMBDA = lambda x, y: HMAC.new(x, y, SHA256).digest()
def encryptString(plaintext, password):
# Generate a 128-bit salt using a CSPRNG.
salt = get_random_bytes(PBKDF2_SALT_SIZE)
# Derive a key using PBKDF2.
key = PBKDF2(password, salt, ALGORITHM_KEY_SIZE, PBKDF2_ITERATIONS, PBKDF2_LAMBDA)
# Encrypt and prepend salt.
ciphertextAndNonce = encrypt(plaintext.encode('utf-8'), key)
ciphertextAndNonceAndSalt = salt + ciphertextAndNonce
# Return as base64 string.
return base64.b64encode(ciphertextAndNonceAndSalt)
def decryptString(base64CiphertextAndNonceAndSalt, password):
# Decode the base64.
ciphertextAndNonceAndSalt = base64.b64decode(base64CiphertextAndNonceAndSalt)
# Get the salt and ciphertextAndNonce.
salt = ciphertextAndNonceAndSalt[:PBKDF2_SALT_SIZE]
ciphertextAndNonce = ciphertextAndNonceAndSalt[PBKDF2_SALT_SIZE:]
# Derive the key using PBKDF2.
key = PBKDF2(password, salt, ALGORITHM_KEY_SIZE, PBKDF2_ITERATIONS, PBKDF2_LAMBDA)
# Decrypt and return result.
plaintext = decrypt(ciphertextAndNonce, key)
return plaintext.decode('utf-8')
def encrypt(plaintext, key):
# Generate a 96-bit nonce using a CSPRNG.
nonce = get_random_bytes(ALGORITHM_NONCE_SIZE)
# Create the cipher.
cipher = AES.new(key, AES.MODE_GCM, nonce)
# Encrypt and prepend nonce.
ciphertext, tag = cipher.encrypt_and_digest(plaintext)
ciphertextAndNonce = nonce + ciphertext + tag
return ciphertextAndNonce
def decrypt(ciphertextAndNonce, key):
# Get the nonce, ciphertext and tag.
nonce = ciphertextAndNonce[:ALGORITHM_NONCE_SIZE]
ciphertext = ciphertextAndNonce[ALGORITHM_NONCE_SIZE:len(ciphertextAndNonce) - ALGORITHM_TAG_SIZE]
tag = ciphertextAndNonce[len(ciphertextAndNonce) - ALGORITHM_TAG_SIZE:]
# Create the cipher.
cipher = AES.new(key, AES.MODE_GCM, nonce)
# Decrypt and return result.
plaintext = cipher.decrypt_and_verify(ciphertext, tag)
return plaintext