| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- 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(contract_name):
- #(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
- )
- path = 'contract_keys\\' + contract_name + '\\'
- os.mkdir(path)
-
- with open(path + '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(path + 'privatekey.key','wb') as f:
- f.write(pem)
-
- def get_keys(contract_name):
- path = 'contract_keys\\' + contract_name + '\\publickey.key'
- with open(path, 'rb') as f:
- public_key = serialization.load_pem_public_key(
- f.read(),
- backend=default_backend()
- )
- return public_key
- def get_private_key(contract_name):
- path = 'contract_keys\\' + contract_name + '\\privatekey.key'
- with open(path, "rb") as key_file:
- private_key = serialization.load_pem_private_key(
- key_file.read(),
- password=None,
- backend=default_backend()
- )
- return private_key
- def get_plain_key(contract_name):
- path = 'contract_keys\\' + contract_name + '\\publickey.key'
- with open(path, 'rt') as f:
- return f.read().split('-----')[2].strip().replace('\n', '')
|