Page 1 of 1

encrypt string with rsa

Posted: Thu Mar 18, 2021 4:11 pm
by isg-mini
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.

Re: encrypt string with rsa

Posted: Mon Dec 06, 2021 3:07 pm
by BetterAutomations
Any success with that? I need to do the same.

Re: encrypt string with rsa

Posted: Tue Dec 07, 2021 10:18 am
by pythoncoder

Re: encrypt string with rsa

Posted: Sat Dec 11, 2021 10:41 pm
by Livingonaboat
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

Re: encrypt string with rsa

Posted: Wed Mar 30, 2022 2:27 am
by BetterAutomations
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).

Re: encrypt string with rsa

Posted: Wed Mar 30, 2022 9:50 am
by pythoncoder
Sorry - it was just a guess on my part.