Creating a PrivateKey from a String

Issue #31 new
Former user created an issue

Can you please provide an example of how to create a PrivateKey when you have a private key as a String;

Eventually, need to use the PrivateKey to sign the JWT;

PrivateKey myKey = "abcd"; // this is where the trouble is.. converting a string to the PrivateKey object

JsonWebSignature jws = new JsonWebSignature(); jws.setKey(myKey.getPrivateKey());

Comments (4)

  1. Brian Campbell repo owner

    It depends on how the key is encoded and if it's encrypted or not. And beyond that, I honestly don't know.

    You might want to look at http://stackoverflow.com/questions/8454677/create-privatekey-and-publickey-from-a-string-base64-encoding-with-der-format or other similar http://stackoverflow.com/questions/8454677/create-privatekey-and-publickey-from-a-string-base64-encoding-with-der-format

    Generally using openssl to get it into a format that is easy to use in Java is where I'd go with it. The link above talks about PKCS#8. But another thing to consider is maybe converting to PKCS#12 which java can read as a keystore.

  2. Magnus Andersson

    I had some code that already read a jwk.json file. This was my workaround: but I used this tool rsa-pem-to-jwk to convert a pem to a jwk file that jose4j could read directly. I don't use node.js typically but it was quite easy to hack a script together that could convert the private key to a jwk.json file.

  3. Log in to comment