├── README.md ├── decryptData.py ├── App.py ├── createKeys.py └── encryptData.py /README.md: -------------------------------------------------------------------------------- 1 | ## End to End Encryption using Asymmetric & Symmetric Method 2 | 3 | The Program will first Create Keys for both Method 4 | 5 | 1. Symmetric Keys 6 | 2. Private Keys 7 | 3. Public Keys 8 | 9 | Using RSA and Fernet. 10 | 11 | The Program will first Encrypt the Message using Symmetric Key. After then, it will Encrypt the Symmetric Key using Public Key as Asymmetric Method. 12 | 13 | To Decrypt the Message Sucessfully, It will use Private key to first Decrypt the Symmetric key. After Decryption, it will use Symmetric Decryption to decrypt the Message. 14 | 15 | -------------------------------------------------------------------------------- /decryptData.py: -------------------------------------------------------------------------------- 1 | import rsa 2 | from cryptography.fernet import Fernet 3 | 4 | 5 | def Decryption(): 6 | # load the private key to decrypt the public key 7 | prkey = open('privateKey.key', 'rb') 8 | pkey = prkey.read() 9 | private_key = rsa.PrivateKey.load_pkcs1(pkey) 10 | 11 | e = open('encryptedMessageKey', 'rb') 12 | ekey = e.read() 13 | 14 | dpubkey = rsa.decrypt(ekey, private_key) 15 | 16 | cipher = Fernet(dpubkey) 17 | 18 | encrypted_data = open('EncryptedFile', 'rb') 19 | edata = encrypted_data.read() 20 | 21 | decrypted_data = cipher.decrypt(edata) 22 | 23 | print(decrypted_data.decode()) 24 | -------------------------------------------------------------------------------- /App.py: -------------------------------------------------------------------------------- 1 | import createKeys 2 | import encryptData 3 | import decryptData 4 | 5 | 6 | class App: 7 | @staticmethod 8 | def KeysCreation(): 9 | createKeys.KeyGeneration() 10 | 11 | @staticmethod 12 | def DataEncryption(message): 13 | encryptData.Encryption(message) 14 | 15 | @staticmethod 16 | def DataDecryption(): 17 | decryptData.Decryption() 18 | 19 | 20 | 21 | #Create Keys using this 22 | #App.KeysCreation() 23 | 24 | #Once Keys Created, Encrypt Data using this 25 | #App.DataEncryption("Heelllo My Name is Abhisht from App.py!!!") 26 | 27 | #The Encrypted data can be Decrypted using this 28 | #App.DataDecryption() 29 | -------------------------------------------------------------------------------- /createKeys.py: -------------------------------------------------------------------------------- 1 | import rsa 2 | from cryptography.fernet import Fernet 3 | 4 | 5 | def KeyGeneration(): 6 | # create the symmetric key 7 | key = Fernet.generate_key() 8 | 9 | # write the symmetric key to a file 10 | k = open('messageKey.key', 'wb') 11 | k.write(key) 12 | k.close() 13 | 14 | # create the pub & private keys 15 | (pubkey, privkey) = rsa.newkeys(2048) 16 | 17 | # write the public key to a file 18 | pukey = open('publicKey.key', 'wb') 19 | pukey.write(pubkey.save_pkcs1('PEM')) 20 | pukey.close() 21 | 22 | # write the private key to a file 23 | prkey = open('privateKey.key', 'wb') 24 | prkey.write(privkey.save_pkcs1('PEM')) 25 | prkey.close() 26 | -------------------------------------------------------------------------------- /encryptData.py: -------------------------------------------------------------------------------- 1 | from cryptography.fernet import Fernet 2 | import rsa 3 | 4 | 5 | def Encryption(message): 6 | # open the symmetric key file 7 | skey = open('messageKey.key', 'rb') 8 | key = skey.read() 9 | 10 | # create the cipher 11 | cipher = Fernet(key) 12 | 13 | # encrypt the data 14 | encrypted_data = cipher.encrypt(bytes(message, 'utf-8')) 15 | edata = open('EncryptedFile', 'wb') 16 | edata.write(encrypted_data) 17 | 18 | # open the public key file 19 | pkey = open('publicKey.key', 'rb') 20 | pkdata = pkey.read() 21 | 22 | # load the file 23 | pubkey = rsa.PublicKey.load_pkcs1(pkdata) 24 | 25 | # encrypt the symmetric key file with the public key 26 | encrypted_key = rsa.encrypt(key, pubkey) 27 | 28 | ekey = open('encryptedMessageKey', 'wb') 29 | ekey.write(encrypted_key) 30 | --------------------------------------------------------------------------------