-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from FoamyGuy/json_key_file_example
json key file example
- Loading branch information
Showing
5 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"private_key_arguments": [10802924268999465233003672463737659932191279041133968058923436754367015686828567560383989892160402220695228233889545658683964318629332408693761505895756447, 65537, 6603041646208715356266858592129685239844946455128316714288951729655521528037145487273616012885516521612688207348765184497451491506274914199323595424930097, 112900874195215049358818352411672948770646187545424444301451131703364915854523, 95685036506628869041897601422905615595924763394819122702857010830658994689389]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# SPDX-FileCopyrightText: 2024 Tim Cocks | ||
# SPDX-License-Identifier: MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"public_key_arguments": [10802924268999465233003672463737659932191279041133968058923436754367015686828567560383989892160402220695228233889545658683964318629332408693761505895756447, 65537]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# SPDX-FileCopyrightText: 2024 Tim Cocks | ||
# SPDX-License-Identifier: MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) |