Hi,
I want to encrypt and decrypt a string using private and public key.
Does exist any micropython library to do that?
I tried using the "rsa" library that I have taken from this project:https://github.com/GoogleCloudPlatform/ ... icropython, but I have not succeeded.
Thanks.
encrypt string with rsa
-
- Posts: 83
- Joined: Mon Mar 20, 2017 10:22 pm
Re: encrypt string with rsa
Any success with that? I need to do the same.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: encrypt string with rsa
Is this official library any use?
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
-
- Posts: 1
- Joined: Sat Dec 11, 2021 10:30 pm
Re: encrypt string with rsa
I have the same issue. I followed each step of https://github.com/GoogleCloudPlatform/ ... icropython.
The response I get is:
HTTP status:
{
"error": {
"code": 401,
"message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/ ... le-project.",
"status": "UNAUTHENTICATED"
}
}
I checked the JWT with JWT.io and the JWT is wrong. the statement of JWT.io is: "The JWT code is not encoded correctly using base64url." This is strange as the JWT is encoded using b42_urlsafe_encode.
Any idea what is going wrong?
regards Menne
The response I get is:
HTTP status:
{
"error": {
"code": 401,
"message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/ ... le-project.",
"status": "UNAUTHENTICATED"
}
}
I checked the JWT with JWT.io and the JWT is wrong. the statement of JWT.io is: "The JWT code is not encoded correctly using base64url." This is strange as the JWT is encoded using b42_urlsafe_encode.
Code: Select all
def create_jwt(project_id, private_key, algorithm, token_ttl):
print("Creating JWT...")
private_key = rsa.PrivateKey(*private_key)
# Epoch_offset is needed because micropython epoch is 2000-1-1 and unix is 1970-1-1. Adding 946684800 (30 years)
epoch_offset = 946684800 # epoch not needed as RTC is set
claims = {
# The time that the token was issued at
'iat': utime.time(),
# The time the token expires.
'exp': utime.time() + token_ttl,
# The audience field should always be set to the GCP project id.
'aud': project_id
}
#This only supports RS256 at this time.
header = { "alg": algorithm, "typ": "JWT" }
content = b42_urlsafe_encode(ujson.dumps(header).encode('utf-8'))
content = content + '.' + b42_urlsafe_encode(ujson.dumps(claims).encode('utf-8'))
signature = b42_urlsafe_encode(rsa.sign(content,private_key,'SHA-256'))
return content+ '.' + signature #signed JWT
regards Menne
-
- Posts: 83
- Joined: Mon Mar 20, 2017 10:22 pm
Re: encrypt string with rsa
I didn't see anything in there for asymmetric (public/private).
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: encrypt string with rsa
Sorry - it was just a guess on my part.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.