-
Notifications
You must be signed in to change notification settings - Fork 0
/
encryptor.py
122 lines (96 loc) · 3.62 KB
/
encryptor.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from rsa import newkeys, PublicKey, PrivateKey, encrypt, decrypt
from json import loads, dumps
from base64 import b64encode, b64decode
def generate_pub(json: str):
try:
json = loads(json)
except:
return -1 # Data is not proper json
try:
n = json['n'] # p x q
e = json['e'] # Something in 1 < x < lcm(p − 1, q − 1)
except:
return -2 # The parsed json does not contain the arguments needed for making a public key
try:
return PublicKey(n, e)
except:
return -3 # Cannot create a public key with the arguments
def generate_priv(json: str):
try:
json = loads(json)
except:
return -1 # Data is not proper json
try:
n = json['n'] # p x q
e = json['e'] # Something in 1 < x < lcm(p − 1, q − 1)
d = json['d']
p = json['p'] # First prime
q = json['q'] # Second prime
except:
return -2 # The parsed json does not contain the arguments needed for making a private key
try:
return PrivateKey(n, e, d, p, q)
except:
return -3 # Cannot create a private key with the arguments
def get_json_from_priv_key(key: PrivateKey):
# Gets all the arguments stored in the key and returns a dictionary
return dumps({
'n': key.n,
'e': key.e,
'd': key.d,
'p': key.p,
'q': key.q
})
def generate_pair(length: int):
if length < 150:
return -1 # The length is not long enough
priv = newkeys(length, 1, 10)[1]
return get_json_from_priv_key(priv) # Returns a dictionary containing all the variables needed in order to create both public and private keys
def encrypt_with_json_key(json: str, data: str):
data = b64decode(data.encode())
json = generate_pub(json)
'''
When you cast negative numbers into bool, you get False.
When you cast PrivateKey instances into bool, you get True.
When there's a problem, the generate_priv functions returns a negative number;
and when it succeeds, it returns a PrivateKey instance.
If the returned object is a negative number, it will be like this:
if not False:
return json - 1
Not False is true, so, the decrypt_with_json_key will return the error code - 1
(ig: we get -1, we return -2)
If the returned object is a PrivateKey instance:
if not True:
return json - 1
Not True is false, so, we won't return anything.
'''
if not json:
return json - 1
try:
return b64encode(encrypt(data, json)).decode()
except:
return -5 # Cannot encrypt data
def decrypt_with_json_key(json: str, data_base64: str):
data = b64decode(data_base64.encode()) # Decodes the encrypted data
json = generate_priv(json) # Turns the json data into a private key
'''
When you cast negative numbers into bool, you get False.
When you cast PrivateKey instances into bool, you get True.
When there's a problem, the generate_priv functions returns a negative number;
and when it succeeds, it returns a PrivateKey instance.
If the returned object is a negative number, it will be like this:
if not False:
return json - 1
Not False is true, so, the decrypt_with_json_key will return the error code - 1
(ig: we get -1, we return -2)
If the returned object is a PrivateKey instance:
if not True:
return json - 1
Not True is false, so, we won't return anything.
'''
if not json:
return json - 1
try:
return decrypt(data, json).decode()
except:
return -5 # Cannot decrypt the data