|
| 1 | +import sys |
| 2 | +from Crypto.PublicKey import RSA |
| 3 | +from Crypto.Cipher import PKCS1_OAEP |
| 4 | +import zlib |
| 5 | +import base64 |
| 6 | +from keyPair_generation import keyPairGeneration |
| 7 | +from utilities import Utilities |
| 8 | + |
| 9 | + |
| 10 | +class RSA_algorithm: |
| 11 | + def encrypt(self, public_key, blob): |
| 12 | + #Import the Public Key and use for encryption using PKCS1_OAEP |
| 13 | + rsa_key = RSA.importKey(public_key) |
| 14 | + rsa_key = PKCS1_OAEP.new(rsa_key) |
| 15 | + |
| 16 | + #compress the data first |
| 17 | + blob = zlib.compress(blob) |
| 18 | + |
| 19 | + '''In determining the chunk size, determine the private key length used in bytes and subtract 42 bytes |
| 20 | + (when using PKCS1_OAEP). The data will be in encrypted in chunks''' |
| 21 | + chunk_size = 470 |
| 22 | + offset = 0 |
| 23 | + end_loop = False |
| 24 | + encrypted = b'' |
| 25 | + |
| 26 | + while not end_loop: |
| 27 | + #The chunk |
| 28 | + chunk = blob[offset:offset + chunk_size] |
| 29 | + |
| 30 | + #If the data chunk is less then the chunk size, then we need to add |
| 31 | + #padding with " ". This indicates the we reached the end of the file |
| 32 | + #so we end loop here |
| 33 | + if len(chunk) % chunk_size != 0: |
| 34 | + end_loop = True |
| 35 | + chunk += b'' * (chunk_size - len(chunk)) |
| 36 | + |
| 37 | + #Append the encrypted chunk to the overall encrypted file |
| 38 | + encrypted += rsa_key.encrypt(chunk) |
| 39 | + |
| 40 | + #Increase the offset by chunk size |
| 41 | + offset += chunk_size |
| 42 | + |
| 43 | + #Base 64 encode the encrypted file |
| 44 | + return base64.b64encode(encrypted) |
| 45 | + |
| 46 | + def decrypt(self, private_key, encrypted_blob): |
| 47 | + #Import the Private Key and use for decryption using PKCS1_OAEP |
| 48 | + rsakey = RSA.importKey(private_key) |
| 49 | + rsakey = PKCS1_OAEP.new(rsakey) |
| 50 | + |
| 51 | + #Base 64 decode the data |
| 52 | + encrypted_blob = base64.b64decode(encrypted_blob) |
| 53 | + |
| 54 | + #In determining the chunk size, determine the private key length used in bytes. |
| 55 | + #The data will be in decrypted in chunks |
| 56 | + chunk_size = 512 |
| 57 | + offset = 0 |
| 58 | + decrypted = b'' |
| 59 | + |
| 60 | + #keep loop going as long as we have chunks to decrypt |
| 61 | + while offset < len(encrypted_blob): |
| 62 | + #The chunk |
| 63 | + chunk = encrypted_blob[offset: offset + chunk_size] |
| 64 | + |
| 65 | + #Append the decrypted chunk to the overall decrypted file |
| 66 | + decrypted += rsakey.decrypt(chunk) |
| 67 | + |
| 68 | + #Increase the offset by chunk size |
| 69 | + offset += chunk_size |
| 70 | + |
| 71 | + #return the decompressed decrypted data |
| 72 | + return zlib.decompress(decrypted) |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == '__main__': |
| 77 | + while True: |
| 78 | + command= input().strip().split(' ') |
| 79 | + |
| 80 | + # if the user demands to generate the key pair |
| 81 | + if command[0] == 'generate_keys': |
| 82 | + try: |
| 83 | + filePath_for_Keys= command[1] |
| 84 | + kpg= keyPairGeneration(filePath_for_Keys) |
| 85 | + except: |
| 86 | + print ("Please verify the file path(s) given.") |
| 87 | + |
| 88 | + # if the user demands to encrypt some confidential file |
| 89 | + elif command[0] == 'encrypt': |
| 90 | + try: |
| 91 | + # extract the path of the public key file |
| 92 | + filePath_of_PublicKey= command[1] |
| 93 | + # extract the path of the file to be encrypted |
| 94 | + file_to_encrypt= command[2] |
| 95 | + # extract the filename user demands to give to will be formed encrypted file |
| 96 | + encrypted_filename= command[3] |
| 97 | + |
| 98 | + # creating object of utilities to access |
| 99 | + util= Utilities() |
| 100 | + |
| 101 | + # read the public key |
| 102 | + publicKey= util.readFile(filePath_of_PublicKey) |
| 103 | + # read the to be encrypted content |
| 104 | + unencrypted_blob= util.readFile(file_to_encrypt) |
| 105 | + |
| 106 | + rsa= RSA_algorithm() |
| 107 | + # encrypt the content |
| 108 | + encrypted_blob= rsa.encrypt(publicKey, unencrypted_blob) |
| 109 | + # write the encrypted blob to get the encrypted contents of the file |
| 110 | + util.writeFile(encrypted_filename, encrypted_blob) |
| 111 | + |
| 112 | + except: |
| 113 | + print ("Please verify the file path(s) given.") |
| 114 | + |
| 115 | + # if the user demands to decrypt some encrypted file |
| 116 | + elif command[0] == 'decrypt': |
| 117 | + try: |
| 118 | + # extract the path of the private key file |
| 119 | + filePath_of_PrivateKey= command[1] |
| 120 | + # extract the path of the file to be decrypted |
| 121 | + file_to_decrypt= command[2] |
| 122 | + # extract the filename user demands to give to will be formed decrypted file |
| 123 | + decrypted_filename= command[3] |
| 124 | + |
| 125 | + # creating object of utilities to access |
| 126 | + util= Utilities() |
| 127 | + |
| 128 | + # read the private key |
| 129 | + privateKey= util.readFile(filePath_of_PrivateKey) |
| 130 | + # read the to be decrypted content |
| 131 | + encrypted_blob= util.readFile(file_to_decrypt) |
| 132 | + |
| 133 | + rsa= RSA_algorithm() |
| 134 | + # decrypt the content |
| 135 | + decrypted_blob= rsa.decrypt(privateKey, encrypted_blob) |
| 136 | + # write the decrypted blob to get the original contents of file |
| 137 | + util.writeFile(decrypted_filename, decrypted_blob) |
| 138 | + |
| 139 | + except: |
| 140 | + print ("Please verify the file path(s) given.") |
| 141 | + |
| 142 | + # user demads to exit from the program |
| 143 | + elif command[0] == 'exit': |
| 144 | + break |
| 145 | + |
| 146 | + else: |
| 147 | + print ("Invalid Command, please verify from the manual.") |
0 commit comments