forked from smk762/Dragonhound
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enc.py
37 lines (30 loc) · 1.07 KB
/
enc.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
#!/usr/bin/env python3
# AES 256 encryption/decryption using pycrypto library
import string
import random
import base64
from Crypto.Cipher import AES
from Crypto import Random
from Crypto.Protocol.KDF import PBKDF2
BLOCK_SIZE = 16
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * chr(BLOCK_SIZE - len(s) % BLOCK_SIZE)
unpad = lambda s: s[:-ord(s[len(s) - 1:])]
def get_private_key(password):
salt = b"salty"
kdf = PBKDF2(password, salt, 64, 1000)
key = kdf[:32]
return key
def encrypt(raw, password):
private_key = get_private_key(password)
raw = pad(raw)
iv = Random.new().read(AES.block_size)
cipher = AES.new(private_key, AES.MODE_CBC, iv)
return base64.b64encode(iv + cipher.encrypt(raw))
def decrypt(enc, password):
private_key = get_private_key(password)
enc = base64.b64decode(enc)
iv = enc[:16]
cipher = AES.new(private_key, AES.MODE_CBC, iv)
return unpad(cipher.decrypt(enc[16:]))
def genPass(stringLength=16):
return ''.join(random.choice(string.ascii_letters + string.digits) for i in range(stringLength))