-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathencryption.py
70 lines (49 loc) · 2.21 KB
/
encryption.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
import rsa
from textwrap import wrap
import helperFunctions as helper
PRIVATE_KEY_PATH = "private_key.pem"
MESSAGE_CHUNK_LIMIT = 100
ENCRYPTION_BYTE_LIMIT = 128
# Creates private and public keys for a node in the network and stores these
# keys on the local machine in PEM formats
def create_keys():
#Creates new keys, public and private key
(servers_pubkey, servers_privkey) = rsa.newkeys(1024)
# Saves private and public keys on local machine in PEM format
priv_key_file = open(PRIVATE_KEY_PATH, 'wb')
priv_key_file.write(servers_privkey.save_pkcs1())
priv_key_file.close()
pubkey = servers_pubkey.save_pkcs1().decode()[31:-30]
return pubkey
# Performs encryption a message usign a provided public key
def encrypt_using_public_key(data,public_key):
public_key = "-----BEGIN RSA PUBLIC KEY-----\n" + public_key + \
"\n-----END RSA PUBLIC KEY-----\n"
#print(public_key)
my_final_public_key = rsa.PublicKey.load_pkcs1(public_key)
# #split data into equally sized chunks
# message_split = helper.split_data_chunk_size(data, MESSAGE_CHUNK_LIMIT)
message_split = []
# #create an empty byte string for encrypted message
encrypted_message = b''
for message in message_split:
#client encrypts its linux command using servers public key
encrypted_message += rsa.encrypt(message, my_final_public_key)
return data
#Performs decryption on an encrypted message when keys
#are present locally on a machine
# byte -> byte
def decrypt_using_private_key(encrypted_message):
# #Read the private key stored in secondary memory in PEM format
with open(PRIVATE_KEY_PATH, mode='rb') as privatefile:
private_key_data = privatefile.read()
# #Convert the PEM format to normal private key format
my_final_private_key = rsa.PrivateKey.load_pkcs1(private_key_data)
# message_split = helper.split_data_chunk_size(encrypted_message, ENCRYPTION_BYTE_LIMIT)
message_split = []
decrypted_message = b''
for message in message_split:
#server decrypts it using its own private key
decrypted_message += rsa.decrypt(message,my_final_private_key)
# return decrypted_message
return encrypted_message