I'm a bit out to sea on this one, so I was wondering whether anyone could help.
Does anyone know how to use Public Key encryption/decryption, using RSA keys in PEM format?
I can get it to work if I use the private key in both directions, I can get the public key to encrypt, but I don't know how to structure a script to get it to work if I want to use a public key to encrypt and a private key to decrypt. I see there is an example in the Java based version of the SDK, but I can't even figure it out from that.
Can anyone lead me in the right direction?
Some sample code of the encryption process i'm using with a public key:
import os
import aws_encryption_sdk
from aws_encryption_sdk.internal.crypto import WrappingKey
from aws_encryption_sdk.key_providers.raw import RawMasterKeyProvider
from aws_encryption_sdk.identifiers import WrappingAlgorithm, EncryptionKeyType
class StaticPublicMasterKeyProvider(RawMasterKeyProvider):
provider_id = 'static-public'
def __init__(self, **kwargs):
self._public_keys = {}
def _get_raw_key(self, key_id):
with open("public_key.pem", "rb") as key_file:
public_key = key_file.read()
self._public_keys[key_id] = public_key
return WrappingKey(
wrapping_algorithm=WrappingAlgorithm.RSA_OAEP_SHA512_MGF1,
wrapping_key=public_key,
wrapping_key_type=EncryptionKeyType.PUBLIC
)
if __name__ == '__main__':
source_file = r'myfile.jpg'
source_file_enc = source_file + '.encrypt'
public_key_id = os.urandom(8)
master_key_provider = StaticPublicMasterKeyProvider()
master_key_provider.add_master_key(public_key_id)
with open(source_file, 'rb') as sf, open(source_file_enc, 'wb') as sfe:
with aws_encryption_sdk.stream(
mode='e',
source=sf,
key_provider=master_key_provider
) as encryptor:
for chunk in encryptor:
sfe.write(chunk)
I have reviewed the python examples on AWS and they are using private keys in both directions.
Any help would be greatly appreciated.
EDIT: links to documentation:
AWS Encryption SDK Developers Guide
Python example generating RSA Key but using private key
Java example using RSA Public key
Note: the two examples use multiple key providers, but still incorporate RSA Keys