encrypt string with rsa

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
isg-mini
Posts: 3
Joined: Thu Mar 18, 2021 3:56 pm

encrypt string with rsa

Post by isg-mini » Thu Mar 18, 2021 4:11 pm

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.

BetterAutomations
Posts: 83
Joined: Mon Mar 20, 2017 10:22 pm

Re: encrypt string with rsa

Post by BetterAutomations » Mon Dec 06, 2021 3:07 pm

Any success with that? I need to do the same.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: encrypt string with rsa

Post by pythoncoder » Tue Dec 07, 2021 10:18 am

Peter Hinch
Index to my micropython libraries.

Livingonaboat
Posts: 1
Joined: Sat Dec 11, 2021 10:30 pm

Re: encrypt string with rsa

Post by Livingonaboat » Sat Dec 11, 2021 10:41 pm

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.

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
Any idea what is going wrong?

regards Menne

BetterAutomations
Posts: 83
Joined: Mon Mar 20, 2017 10:22 pm

Re: encrypt string with rsa

Post by BetterAutomations » Wed Mar 30, 2022 2:27 am

pythoncoder wrote:
Tue Dec 07, 2021 10:18 am
Is this official library any use?
I didn't see anything in there for asymmetric (public/private).

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: encrypt string with rsa

Post by pythoncoder » Wed Mar 30, 2022 9:50 am

Sorry - it was just a guess on my part.
Peter Hinch
Index to my micropython libraries.

Post Reply