Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json key file example #41

Merged
merged 1 commit into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/keys/example512key.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"private_key_arguments": [10802924268999465233003672463737659932191279041133968058923436754367015686828567560383989892160402220695228233889545658683964318629332408693761505895756447, 65537, 6603041646208715356266858592129685239844946455128316714288951729655521528037145487273616012885516521612688207348765184497451491506274914199323595424930097, 112900874195215049358818352411672948770646187545424444301451131703364915854523, 95685036506628869041897601422905615595924763394819122702857010830658994689389]}
2 changes: 2 additions & 0 deletions examples/keys/example512key.json.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# SPDX-FileCopyrightText: 2024 Tim Cocks
# SPDX-License-Identifier: MIT
1 change: 1 addition & 0 deletions examples/keys/example512key_pub.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"public_key_arguments": [10802924268999465233003672463737659932191279041133968058923436754367015686828567560383989892160402220695228233889545658683964318629332408693761505895756447, 65537]}
2 changes: 2 additions & 0 deletions examples/keys/example512key_pub.json.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# SPDX-FileCopyrightText: 2024 Tim Cocks
# SPDX-License-Identifier: MIT
46 changes: 46 additions & 0 deletions examples/rsa_json_keys.py
Original file line number Diff line number Diff line change
@@ -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"))
Loading