-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.py
30 lines (24 loc) · 917 Bytes
/
crypto.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
import base64
from Crypto.Cipher import DES
from django.conf import settings
def encrypt_and_encode(string: str) -> str:
"""
Encrypts and encodes into base 16
Additional trailing spaces are added to fit len % 8 = 0
(input should not contain trailing spaces, as they are stripped when decrypting)
"""
string_bytes = string.encode('utf-8')
excess = len(string_bytes) % 8
if excess != 0:
padding = ' ' * (8 - excess)
string_bytes += padding.encode('utf-8')
encrypted = _get_cipher().encrypt(string_bytes)
return base64.b16encode(encrypted).decode('ascii')
def decode_and_decrypt(string: str) -> str:
"""
Decodes from base 16 and decrypts.
"""
encrypted = base64.b16decode(string)
return _get_cipher().decrypt(encrypted).decode('utf-8').rstrip()
def _get_cipher():
return DES.new(settings.SECRET_KEY[0:8].encode('utf-8'), DES.MODE_ECB)