How to get string representing a PEM-encoded RSA private key ?

Hi All,

I want to use JWT generate policy using RSA256 algorithm.
This is for learning purpose, so I'm using assign message policy for "private.privatekey" which has to be PEM encoded RSA private key.
I thought of using a sample base64 encoded strin, but it did'nt worked out.

I'm a 500 response:

{ "fault": { "faultstring": "Failed to parse key: policy(Generate-JWT-2) ", "detail": { "errorcode": "steps.jwt.KeyParsingFailed" } } }

Solved Solved
0 6 14.3K
1 ACCEPTED SOLUTION

I generate RSA key pairs, in PEM-encoded format, using openssl, like this:

openssl genrsa -out private.pem 2048 

The result is something like this:

-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAsgMI1AyV8J8TKf4rNRXmgBfeYVqZsP53vRRIgAyZboPELuTS
qTZqXl1W4zkKKAGJXU/nsllMW6bfAavCRDMsOPKyAp/8VnRqSWrF+Gyw2CVoNtG6
htqcFT3EqIazcRewD/Jl96OpAscjj+HHcxX44tQj6Y4e0zHOs/vN+YvHNjL6oG6e
GNeVQGvqIO180EUKnIbSzTF73CrCxAHG/wfFMY+mZabcDhrrY0pbaZn1yaqRI9Q4
...
QqCIZGrBRRW1TqpZbwYh5Cmw+dqcGFzKEp2hjUDgkqQA/HRaAiDg8fSDtBE1IRI6
VW/sA9n7/74kNbKGVgf8LXanVzMIxef7OiRqqHmTek6V+pJImt4SmQ==
-----END RSA PRIVATE KEY-----

Then, you can extract the public key like this:

openssl rsa -in private.pem -outform PEM -pubout -out public.pem

And the result is like this:

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsgMI1AyV8J8TKf4rNRXm
gBfeYVqZsP53vRRIgAyZboPELuTSqTZqXl1W4zkKKAGJXU/nsllMW6bfAavCRDMs
OPKyAp/8VnRqSWrF+Gyw2CVoNtG6htqcFT3EqIazcRewD/Jl96OpAscjj+HHcxX4
4tQj6Y4e0zHOs/vN+YvHNjL6oG6ePNW09sqZos5pRRbMvZVikax6d8AuPUPSwmLH
3HDdrGD/n/CclMxMnISRnQjFCW03GNeVQGvqIO180EUKnIbSzTF73CrCxAHG/wfF
MY+mZabcDhrrY0pbaZn1yaqRI9Q4MhMLV+9CGiUF0DzDCnzzx5q7ngN1N5lOg5pO
BQIDAQAB
-----END PUBLIC KEY-----

As you know, for GenerateJWT you need the private key. And for VerifyJWT you need the public key.

Just FYI, the gunk between the ----BEGIN XXXX---- and ----END XXXX---- in both of the above examples, is actually a byte array that is base64-encoded. So you're on the right track - base64 is what you want. But the PEM format is pretty standard and it includes that framing.

The private key is obviously secret. There are ways to encrypt it, when you generate it. But if you're just doing this for learning purposes, then you don't need encryption there.

View solution in original post

6 REPLIES 6

I generate RSA key pairs, in PEM-encoded format, using openssl, like this:

openssl genrsa -out private.pem 2048 

The result is something like this:

-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAsgMI1AyV8J8TKf4rNRXmgBfeYVqZsP53vRRIgAyZboPELuTS
qTZqXl1W4zkKKAGJXU/nsllMW6bfAavCRDMsOPKyAp/8VnRqSWrF+Gyw2CVoNtG6
htqcFT3EqIazcRewD/Jl96OpAscjj+HHcxX44tQj6Y4e0zHOs/vN+YvHNjL6oG6e
GNeVQGvqIO180EUKnIbSzTF73CrCxAHG/wfFMY+mZabcDhrrY0pbaZn1yaqRI9Q4
...
QqCIZGrBRRW1TqpZbwYh5Cmw+dqcGFzKEp2hjUDgkqQA/HRaAiDg8fSDtBE1IRI6
VW/sA9n7/74kNbKGVgf8LXanVzMIxef7OiRqqHmTek6V+pJImt4SmQ==
-----END RSA PRIVATE KEY-----

Then, you can extract the public key like this:

openssl rsa -in private.pem -outform PEM -pubout -out public.pem

And the result is like this:

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsgMI1AyV8J8TKf4rNRXm
gBfeYVqZsP53vRRIgAyZboPELuTSqTZqXl1W4zkKKAGJXU/nsllMW6bfAavCRDMs
OPKyAp/8VnRqSWrF+Gyw2CVoNtG6htqcFT3EqIazcRewD/Jl96OpAscjj+HHcxX4
4tQj6Y4e0zHOs/vN+YvHNjL6oG6ePNW09sqZos5pRRbMvZVikax6d8AuPUPSwmLH
3HDdrGD/n/CclMxMnISRnQjFCW03GNeVQGvqIO180EUKnIbSzTF73CrCxAHG/wfF
MY+mZabcDhrrY0pbaZn1yaqRI9Q4MhMLV+9CGiUF0DzDCnzzx5q7ngN1N5lOg5pO
BQIDAQAB
-----END PUBLIC KEY-----

As you know, for GenerateJWT you need the private key. And for VerifyJWT you need the public key.

Just FYI, the gunk between the ----BEGIN XXXX---- and ----END XXXX---- in both of the above examples, is actually a byte array that is base64-encoded. So you're on the right track - base64 is what you want. But the PEM format is pretty standard and it includes that framing.

The private key is obviously secret. There are ways to encrypt it, when you generate it. But if you're just doing this for learning purposes, then you don't need encryption there.

Did this work for you?

Yes, it worked out finally. I have used an assign message policy to process the keys(Public/Private Keys)

I have also tried using KVM, using it I found that while we are entering the key in KVM, it gets appended with white spaces although when I check the same in notepad it looks just fine.


So now I check the added key in KVM after updating whether it's correct or not.

how did you do that. how to generate jwt using rs256 algorithm

Did you look at the Apigee documentation? There's a sample on the main doc page. Top google result for "GenerateJWT Apigee".