-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrpt.py
More file actions
48 lines (40 loc) · 1.44 KB
/
crpt.py
File metadata and controls
48 lines (40 loc) · 1.44 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
from colorama import Fore, Back, Style, init
from cryptography.hazmat.primitives.asymmetric import rsa, padding
import cryptography.hazmat.backends.openssl.rsa as ts
import cryptography.hazmat.primitives._serialization as ser
from cryptography.hazmat.primitives import serialization, hashes
def derivepublickey(privkey: ts._RSAPublicKey):
key = privkey
return (
key.public_key()
.public_bytes(ser.Encoding.PEM, ser.PublicFormat.PKCS1)
.decode("utf-8")
)
def generatekey(keysize=1024):
key = rsa.generate_private_key(public_exponent=65537, key_size=keysize)
pubkey = derivepublickey(key)
return (key, pubkey)
def encrypt(message: str, publickey: str):
keybytes = publickey.encode("utf-8")
messagebytes = message.encode("utf-8")
loadedPublic = serialization.load_pem_public_key(keybytes)
cipher = loadedPublic.encrypt(
messagebytes,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None,
),
)
return cipher
def decrypt(message: str, privatekey: str):
# loadedPrivate = serialization.load_pem_private_key(privatekey, password=)
decryptedString = privatekey.decrypt(
message,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None,
),
)
return decryptedString.decode("utf-8")