- Shan requests for Shiva's
public_key
- Shan performs
symmetric encryption
on themessage
- Shan encrypts the
symmetric_key
using Shiva's public key - Shan signs the
encrypted_message
using Shan'sprivate_key
- Shan sends the
public_key
of Shan,encrypted_message
,encrypted_symmetric_key
andsignature
to Shiva - Shiva verifies the
encrypted_message
using the signature and Shan'spublic_key
- Shiva decryptes the
encrypted_symmetric_key
using Shiva'sprivate_key
- Shiva decryptes the
encrypted_message
usingsymmetric_key
- Symmetric Encryption
- Asymmetric Encryption
- Signing messages
- Verifying signature
- Dockerized the application
- Using RSA to generate Public and Private key of Receiver
import rsa
#----------CREATING RECEIVER'S PUBLIC & PRIVATE KEYS------------#
# CREATE THE PUB & PRIVATE KEYS
(pubkey,privkey)=rsa.newkeys(2048)
# WRITE THE PUBLIC KEY TO A FILE
pukey = open('publickey.key','wb')
pukey.write(pubkey.save_pkcs1('PEM'))
pukey.close()
# WRITE THE PRIVATE KEY TO A FILE
prkey = open('privkey.key','wb')
prkey.write(privkey.save_pkcs1('PEM'))
prkey.close()
print('PUBLIC & PRIVATE KEY OF RECEIVER HAS BEEN GENERATED')
- Perform
Symmetric Encryption
on theMessage
- Encrypt
Symmetric_Key
using Shiva'sPublic_Key
- Generate
public_key
andprivate_key
for Shan - Sign the
encrypted_message
using Shan'sprivate_key
from cryptography.fernet import Fernet
import rsa
#----------FILE ENCRYPTION USING SYMMETRIC KEY---------------#
# CREATE THE SYMMETRIC KEY AND CIPHER
symKey = Fernet.generate_key()
cipher = Fernet(symKey)
# GET FILENAME TO ENCRYPT
filename = input("Enter filename to encrypt [with extension]:\n")
myfile = open(filename,'rb')
myfiledata = myfile.read()
# ENCRYPT THE DATA AND CREATE ENCRYTPED FILE
encrypted_data = cipher.encrypt(myfiledata)
edata = open('enc_' + filename ,'wb')
edata.write(encrypted_data)
#----------USING RECEIVER'S PUBLIC KEY TO ENCRYPT SYMMETRIC KEY------------#
# OPEN AND LOAD THE PUBLIC KEY FILE OF RECEIVER
pkey = open('publickey.key','rb')
pkdata = pkey.read()
pubkey = rsa.PublicKey.load_pkcs1(pkdata)
# ENCRYPT THE SYMMETRIC KEY WITH THE PUBLIC KEY
encrypted_key = rsa.encrypt(symKey,pubkey)
ekey = open('encrypted_key','wb')
ekey.write(encrypted_key)
#----------USING SENDER'S PRIVATE KEY TO SIGN MESSAGE------------#
# GENERATE ASYMMETRIC KEY PAIR [to use for signature]
(public_key, private_key) = rsa.newkeys(512)
vkey = open('public_verify.key','wb')
vkey.write(public_key.save_pkcs1('PEM'))
vkey.close()
# GENERATE SIGNATURE FILE
signature = rsa.sign(encrypted_data, private_key, 'SHA-1')
sign = open('sign.txt','wb')
sign.write(signature)
sign.close()
print('------- ENCRYPTION COMPLETE ---------')
print('\n')
print(' YOU CAN NOW SHARE THE ENCRYPTED FILE: _enc' +filename+ '\n' );
print(' YOU CAN NOW SHARE THE ENCRYPTED SYMMETRIC KEY: encrypted_key \n' );
print(' YOU CAN NOW SHARE THE PUBLIC KEY OF SENDER: public_verify.key \n' );
print(' YOU CAN NOW SHARE THE SIGNATURE: sign.txt \n' );
- Shiva's
private_key
is used to decrypt theencrypted_symmetric_key
symmetric_key
is used to decrypt theMessage
signature
is verified using Shan'spublic_key
import rsa
from cryptography.fernet import Fernet
# OEPN RECEIVER'S PRIVATE KEY [to decrypt the symmetric key]
prkey = open('privkey.key','rb')
pkey = prkey.read()
private_key = rsa.PrivateKey.load_pkcs1(pkey)
# DECRYPT SYMMETRIC KEY USING RECEIVER'S PRIVATE KEY
s = open('encrypted_key','rb')
sym = s.read()
symKey = rsa.decrypt(sym,private_key)
cipher = Fernet(symKey)
# DECRYPTION USING CIPHER OF SYMMETRIC KEY
filename = input("Enter filename to decrypt [with extension]:\n")
encrypted_data = open(filename,'rb')
edata = encrypted_data.read()
decrypted_data = cipher.decrypt(edata)
message = decrypted_data
# OPENING THE PUBLIC KEY OF SENDER AND SIGNATURE
vkey = open('public_verify.key','rb')
vdata = vkey.read()
public_verify = rsa.PublicKey.load_pkcs1(vdata)
sign = open('sign.txt','rb')
signature = sign.read()
# VERIFYING SIGNATURE
try:
rsa.verify(edata, signature, public_verify)
print('signature verified')
# load the file
with open('decrypted_' + filename,'wb') as df:
df.write(decrypted_data)
print('Check----->'+ 'decrypted_'+filename+ '<------to get the decrypted file')
except:
print('message is tampered')
- Encrypt your file.
- Transfer the encrypted file.
- It can be decrypted only by the intended receiver.
If you wish to use your own files for encryption, you need to move them to repository's directory, and add the below line to Dockerfile
docker build -t pycryptor .
Before running the below docker command, make sure to replace '/my/dir' with the path of the directory where you want to have the Keys & Encrypted/Decrypted files
docker run -d --name pycryption -v /my/dir:/usr/src/app -ti pycryptor
docker run -d --name pycryption -v /home/playground/Bloom/pycryptor:/usr/src/app -ti pycryptor
docker exec -it pycryption python create_keys.py
docker exec -it pycryption python encrypt.py
docker exec -it pycryption python decrypt.py
docker stop pycryption
docker rm pycryption
docker rmi pycryptor