diff --git a/examples/keys/example512key.json b/examples/keys/example512key.json new file mode 100644 index 0000000..37325ac --- /dev/null +++ b/examples/keys/example512key.json @@ -0,0 +1 @@ +{"private_key_arguments": [10802924268999465233003672463737659932191279041133968058923436754367015686828567560383989892160402220695228233889545658683964318629332408693761505895756447, 65537, 6603041646208715356266858592129685239844946455128316714288951729655521528037145487273616012885516521612688207348765184497451491506274914199323595424930097, 112900874195215049358818352411672948770646187545424444301451131703364915854523, 95685036506628869041897601422905615595924763394819122702857010830658994689389]} diff --git a/examples/keys/example512key.json.license b/examples/keys/example512key.json.license new file mode 100644 index 0000000..936829b --- /dev/null +++ b/examples/keys/example512key.json.license @@ -0,0 +1,2 @@ +# SPDX-FileCopyrightText: 2024 Tim Cocks +# SPDX-License-Identifier: MIT diff --git a/examples/keys/example512key_pub.json b/examples/keys/example512key_pub.json new file mode 100644 index 0000000..ed0316f --- /dev/null +++ b/examples/keys/example512key_pub.json @@ -0,0 +1 @@ +{"public_key_arguments": [10802924268999465233003672463737659932191279041133968058923436754367015686828567560383989892160402220695228233889545658683964318629332408693761505895756447, 65537]} diff --git a/examples/keys/example512key_pub.json.license b/examples/keys/example512key_pub.json.license new file mode 100644 index 0000000..936829b --- /dev/null +++ b/examples/keys/example512key_pub.json.license @@ -0,0 +1,2 @@ +# SPDX-FileCopyrightText: 2024 Tim Cocks +# SPDX-License-Identifier: MIT diff --git a/examples/rsa_json_keys.py b/examples/rsa_json_keys.py new file mode 100644 index 0000000..02e843d --- /dev/null +++ b/examples/rsa_json_keys.py @@ -0,0 +1,46 @@ +# SPDX-FileCopyrightText: 2024 Tim Cocks +# SPDX-License-Identifier: MIT +import binascii +import json +import adafruit_rsa +from adafruit_rsa import PublicKey, PrivateKey + +""" +CircuitPython microcontrollers cannot load PEM key files generated by OpenSSL +because the pyasn1 module is not supported. This example illustrates a way +of loading keys from JSON files instead. +""" + +# load a keypair from JSON files + +with open("keys/example512key.json", "r") as f: + priv_key_obj = json.loads(f.read()) + + +with open("keys/example512key_pub.json", "r") as f: + pub_key_obj = json.loads(f.read()) + + +# initialize the Key objects from data that was loaded from the JSON files +public_key = PublicKey(*pub_key_obj["public_key_arguments"]) +private_key = PrivateKey(*priv_key_obj["private_key_arguments"]) + +# Message to send +message = "hello blinka" + +# Encode the string as bytes (Adafruit_RSA only operates on bytes!) +message = message.encode("utf-8") + +# Encrypt the message using the public key +print("Encrypting message...") +encrypted_message = adafruit_rsa.encrypt(message, public_key) + +print("encrypted b64: ") +print(binascii.b2a_base64(encrypted_message, False).decode()) + +# Decrypt the encrypted message using a private key +print("Decrypting message...") +decrypted_message = adafruit_rsa.decrypt(encrypted_message, private_key) + +# Print out the decrypted message +print("Decrypted Message: ", decrypted_message.decode("utf-8"))