This repository has been archived by the owner on Apr 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.py
57 lines (47 loc) · 2 KB
/
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
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
import base64
import hashlib
from Crypto import Random
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
from Crypto.Cipher import AES
from ast import literal_eval
class OAEP():
def generate_keys(self):
random_number_generator = Random.new().read
key = RSA.generate(2048, random_number_generator)
private_key, public_key = key, key.publickey()
return [private_key, public_key]
def decrypt(self, ciphertext: str, private_key: str) -> str:
ciphertext = base64.b64decode(ciphertext)
oaep_key = PKCS1_OAEP.new(private_key)
plaintext = oaep_key.decrypt(literal_eval(str(ciphertext)))
return plaintext.decode('utf-8')
def encrypt(self, plaintext: str, public_key: str) -> str:
oaep_key = PKCS1_OAEP.new(public_key)
ciphertext = oaep_key.encrypt(self._encode_plaintext(plaintext))
return base64.b64encode(ciphertext)
def _encode_plaintext(self, text: str) -> str:
if isinstance(text, int) or isinstance(text, float):
text = str(text)
return text.encode('utf-8')
class AESCipher():
def __init__(self, key):
self.block_size = 16
self.key = hashlib.sha256(key.encode()).digest()
def decrypt(self, ciphertext: str) -> str:
ciphertext = base64.b64decode(ciphertext)
iv = ciphertext[:AES.block_size]
cipher = AES.new(self.key, AES.MODE_CBC, iv)
return self._unpad(cipher.decrypt(ciphertext[AES.block_size:])).decode('utf-8')
def encrypt(self, plaintext: str) -> str:
plaintext = self._pad(plaintext)
iv = Random.new().read(AES.block_size)
cipher = AES.new(self.key, AES.MODE_CBC, iv)
return base64.b64encode(iv + cipher.encrypt(plaintext))
def _pad(self, text: str) -> str:
if isinstance(text, int) or isinstance(text, float):
text = str(text)
return bytes(text+(self.block_size-len(text)%self.block_size)* \
chr(self.block_size-len(text)%self.block_size), encoding=('utf-8'))
def _unpad(self, text: str) -> str:
return text[:-ord(text[len(text)-1:])]