def __add_private_key_to_blockchain( self, blockchain: Blockchain, medical_data_transaction_id: str, private_key_for_medical_data: PrivateKey, third_party=None): """ create a new acl entry via acl_data :param blockchain: the wordlwide blockchain :param acl_name: the name of the acl_name :param acl_data: list of {transaction_id : private_key}. The private_key decrypt the data in the transaction with the id transaction_id :return: """ third_party = third_party or self key_name = f'atomic ACL for {medical_data_transaction_id}' acl_data_json = json.dumps({ 'key_name': key_name, 'private_key': private_key_for_medical_data.save_pkcs1().decode('utf8'), 'medical_data_transaction_id': medical_data_transaction_id, }) crypted_medical_data = CryptingTools.crypt_data( acl_data_json, self.get_public_key()) transaction = blockchain.add_transaction(crypted_medical_data, self, third_party) transaction_id = transaction.id self.add_key_to_keychain(key_name, [transaction_id]) return transaction_id
def private_key_to_str(private_key): """ This function produces a string that represents the public key in PEM format. This was it can be written to a database or transferred accross an internet connection. Args: private_key (rsa.PrivateKey): The key that is to be interpreted to a PEM-format string. Returns: bytearray: A string of bytes representing the key in PEM format. """ return PrivateKey.save_pkcs1(private_key, format='PEM')
def private_key_to_file(private_key, filepath): """ Writes a private key to a file in PEM format. This function will create a file if one does not exist and it will erase the contents of the file if it does exist. Args: private_key (rsa.PrivateKey): The key that is to be written to the file. filepath (string): A path to the file where you wish to write the key. Returns: None """ with open(filepath, 'wb+') as f: pk = PrivateKey.save_pkcs1(private_key, format='PEM') f.write(pk)
from rsa import PublicKey, PrivateKey, newkeys, encrypt, decrypt (public, private) = newkeys(512) with open("./public.pem", "wb") as f: pk = PublicKey.save_pkcs1(public) f.write(pk) with open("./private.pem", "wb") as f: pk = PrivateKey.save_pkcs1(private) f.write(pk) #loading the keys ''' with open("./public.pem","rb") as f: data = f.read() public = PublicKey.load_pkcs1(data) with open("./private.pem","rb") as f: data = f.read() private = PrivateKey.load_pkcs1(data) ''' message = 'Bob says hi to Alice!'.encode('utf8') crypto = encrypt(message, public) print(crypto) message = decrypt(crypto, private) print(message.decode('utf8'))
from rsa import verify, sign, encrypt, decrypt, PublicKey, PrivateKey, newkeys, pkcs1 PUB_KEY_DST = '../../tests/testing_data/user2_test_pub.pem' PRIV_KEY_DST = '../../tests/testing_data/user2_test.pem' (public,private) = newkeys(4096, poolsize=4) with open(PUB_KEY_DST, 'wb+') as f: pk = PublicKey.save_pkcs1(public, format='PEM') f.write(pk) with open(PRIV_KEY_DST, 'wb+') as f: pk = PrivateKey.save_pkcs1(private, format='PEM') f.write(pk)