import rsa import os #from cryptography.fernet import Fernet # create the symmetric key only for the JSON file - we are going to only encrypt the keys #key = Fernet.generate_key() # write the symmetric key to a file #k = open('symmetric.key','wb') #k.write(key) #k.close() # create the pub & private keys for the parties from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives.asymmetric import rsa from cryptography.hazmat.primitives import serialization def make_keys(company): #(pubkey,privkey)=rsa.newkeys(2048) private_key = rsa.generate_private_key( public_exponent=65537, key_size=2048, backend=default_backend() ) public_key = private_key.public_key() pem = public_key.public_bytes( encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo ) with open('companies\\' + company + '_publickey.key','wb') as f: f.write(pem) pem = private_key.private_bytes( encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption() ) with open('companies\\' + company + '_privatekey.key','wb') as f: f.write(pem) def get_keys(company): path = 'companies\\' + company + '_publickey.key' if not os.path.exists(path): make_keys(company) with open(path, 'rb') as f: public_key = serialization.load_pem_public_key( f.read(), backend=default_backend() ) return public_key