-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncrypt.py
More file actions
63 lines (56 loc) · 1.84 KB
/
Encrypt.py
File metadata and controls
63 lines (56 loc) · 1.84 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
from Crypto import Random
from base64 import b64encode,b64decode
class Encryption:
#Specified long of the encrption key
key_long = 2048
"""
* Constructor for Encryption
"""
def __init__ (self):
pass
"""
* Generates both private and public keys, using the default key key_long
* Returns a list with the private and public keys
"""
def Newkey (self):
#Generates a random object for encryption
random = Random.new().read
key = RSA.generate(self.key_long,random)
private = key;
public = key.publickey()
return [private,public]
"""
* Encrypt generates the padding using the standar PKCS1_OAEP for the public
* key and the encrypts the message to later convert it into base64 encode
* @public_key Public Key from the user
* @message Message to be encrypted
* Return the message encrypted in base64
"""
def Encrypt (self,public_key,message):
#Generates padding using standard PKCS1_OAEP for the public key
cypher = PKCS1_OAEP.new(public_key)
#Encrypts the message with the cypher and converts it to base64
encrypted = b64encode(cypher.encrypt(message))
return encrypted
"""
* Decrypt takes the private key, decodes the message from base64 and then
* decrypts it using the key
* @private_key {[bytes]} Private key in as_bytes
* @message {[bytes]} Message in bytes
* Return the message decrypted
"""
def Decrypt (self,private_key,message):
#Generates padding using standard PKCS1_OAEP for the private key
cypher = PKCS1_OAEP.new(private_key)
#Converts the message from base64 and the decrypts it
decrypted = cypher.decrypt(b64decode(message))
return decrypted
"""
* ViewKey exports the key so it can be visualized
* @key key from the user
* Returns the key vor visualization
"""
def ViewKey (self,key):
return key.exportKey('PEM')