-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryption.py
More file actions
28 lines (19 loc) · 912 Bytes
/
encryption.py
File metadata and controls
28 lines (19 loc) · 912 Bytes
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
from Crypto.Cipher import AES
import base64
import os
import binascii
def encrypt(plain_text, secret_key, nonce):
"""encrypts a plain text string using the given secret_key and nonce - usually a session id"""
#nonce = 'j8h6g88uu9ot6r44'
encobj = AES.new(secret_key, AES.MODE_CBC, nonce)
str_length = len(plain_text) + (16 - (len(plain_text) % 16))
padded = plain_text.rjust(str_length, '~')
encrypted_text = encobj.encrypt(padded)
return encrypted_text.encode('hex')
def decrypt(encrypted_text, secret_key, nonce):
"""decrypts an encrypted string using the given secret_key and nonce - usually a session id"""
#nonce = 'j8h6g88uu9ot6r44'
encobj = AES.new(secret_key, AES.MODE_CBC, nonce)
decrypted_text = encobj.decrypt(binascii.unhexlify(encrypted_text))
stripped_text = decrypted_text.lstrip('~')
return stripped_text