Search code examples
cryptographyencryption-asymmetricjwejosepython-jose

What am I doing wrong in the following code where I'm trying to achieve asymmetric signing and encryption using jose-python?


    from jose import jws
    from jose import jwe

    with open('privkey.pem', mode='r') as privatefile:
        privkey = privatefile.read()

    with open('pubkey.pem', mode='r') as pubfile:
        pubkey = pubfile.read()
    

    ####################################
    ## Signing request with private key
    ####################################
    query = {
        "QueryType": 1,
        "QuerySubType": None
    }
    signed = jws.sign(query, privkey, algorithm='RS256')
    print(signed)
    # verify = jws.verify(signed, pubkey, algorithms=['RS256'])
    # print(verify)


    ################################
    ## Encryption
    ################
    encrypted = jwe.encrypt(signed, pubkey, algorithm='RSA_OAEP', encryption='A256CBC_HS512')
    print(encrypted)

I created rsa key pair of size 2048. The singing and verification works fine. I am getting en error "jose.exceptions.JWEError: Algorithm RSA_OAEP not supported". I've tried all the algorithms and encryption, but getting the same error for algorithms not supported for all of them. What am I doing wrong here?


Solution

  • The key management and the content encryption algorithms are specified wrongly. Use hyphens instead of underscores:

    encrypted = jwe.encrypt(signed, pubkey, algorithm='RSA-OAEP', encryption='A256CBC-HS512')
    

    However, it is more robust to apply the provided constants defined in the class jose.constants.Algorithms:

    from jose.constants import Algorithms
    ...
    encrypted = jwe.encrypt(signed, pubkey, algorithm=Algorithms.RSA_OAEP, encryption=Algorithms.A256CBC_HS512)