-
Notifications
You must be signed in to change notification settings - Fork 501
Expand file tree
/
Copy pathRSA.py
More file actions
146 lines (115 loc) · 5.3 KB
/
RSA.py
File metadata and controls
146 lines (115 loc) · 5.3 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import sys
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
import zlib
import base64
from keyPair_generation import keyPairGeneration
from utilities import Utilities
class RSA_algorithm:
def encrypt(self, public_key, blob):
# Import the Public Key and use for encryption using PKCS1_OAEP
rsa_key = RSA.importKey(public_key)
rsa_key = PKCS1_OAEP.new(rsa_key)
# compress the data first
blob = zlib.compress(blob)
"""In determining the chunk size, determine the private key length used in bytes and subtract 42 bytes
(when using PKCS1_OAEP). The data will be in encrypted in chunks"""
chunk_size = 470
offset = 0
end_loop = False
encrypted = b""
while not end_loop:
# The chunk
chunk = blob[offset : offset + chunk_size]
# If the data chunk is less then the chunk size, then we need to add
# padding with " ". This indicates the we reached the end of the file
# so we end loop here
if len(chunk) % chunk_size != 0:
end_loop = True
chunk += b"" * (chunk_size - len(chunk))
# Append the encrypted chunk to the overall encrypted file
encrypted += rsa_key.encrypt(chunk)
# Increase the offset by chunk size
offset += chunk_size
# Base 64 encode the encrypted file
return base64.b64encode(encrypted)
def decrypt(self, private_key, encrypted_blob):
# Import the Private Key and use for decryption using PKCS1_OAEP
rsakey = RSA.importKey(private_key)
rsakey = PKCS1_OAEP.new(rsakey)
# Base 64 decode the data
encrypted_blob = base64.b64decode(encrypted_blob)
# In determining the chunk size, determine the private key length used in bytes.
# The data will be in decrypted in chunks
chunk_size = 512
offset = 0
decrypted = b""
# keep loop going as long as we have chunks to decrypt
while offset < len(encrypted_blob):
# The chunk
chunk = encrypted_blob[offset : offset + chunk_size]
# Append the decrypted chunk to the overall decrypted file
decrypted += rsakey.decrypt(chunk)
# Increase the offset by chunk size
offset += chunk_size
# return the decompressed decrypted data
return zlib.decompress(decrypted)
if __name__ == "__main__":
while True:
command = input().strip().split(" ")
# if the user demands to generate the key pair
if command[0] == "generate_keys":
try:
filePath_for_Keys = command[1]
kpg = keyPairGeneration(filePath_for_Keys)
except:
print("Please verify the file path(s) given.")
# if the user demands to encrypt some confidential file
elif command[0] == "encrypt":
try:
# extract the path of the public key file
filePath_of_PublicKey = command[1]
# extract the path of the file to be encrypted
file_to_encrypt = command[2]
# extract the filename user demands to give to will be formed encrypted file
encrypted_filename = command[3]
# creating object of utilities to access
util = Utilities()
# read the public key
publicKey = util.readFile(filePath_of_PublicKey)
# read the to be encrypted content
unencrypted_blob = util.readFile(file_to_encrypt)
rsa = RSA_algorithm()
# encrypt the content
encrypted_blob = rsa.encrypt(publicKey, unencrypted_blob)
# write the encrypted blob to get the encrypted contents of the file
util.writeFile(encrypted_filename, encrypted_blob)
except:
print("Please verify the file path(s) given.")
# if the user demands to decrypt some encrypted file
elif command[0] == "decrypt":
try:
# extract the path of the private key file
filePath_of_PrivateKey = command[1]
# extract the path of the file to be decrypted
file_to_decrypt = command[2]
# extract the filename user demands to give to will be formed decrypted file
decrypted_filename = command[3]
# creating object of utilities to access
util = Utilities()
# read the private key
privateKey = util.readFile(filePath_of_PrivateKey)
# read the to be decrypted content
encrypted_blob = util.readFile(file_to_decrypt)
rsa = RSA_algorithm()
# decrypt the content
decrypted_blob = rsa.decrypt(privateKey, encrypted_blob)
# write the decrypted blob to get the original contents of file
util.writeFile(decrypted_filename, decrypted_blob)
except:
print("Please verify the file path(s) given.")
# user demads to exit from the program
elif command[0] == "exit":
break
else:
print("Invalid Command, please verify from the manual.")