Using private/public key example
See original GitHub issueHi, can you please provide an example of how to sign/verify using an existing private/public key pair?
I got it working in node with node-jsonwebtoken, like this:
var key = fs.readFileSync('private.key');
var pem = fs.readFileSync('public.pem');
var header = {...};
var payload = {...};
header.algorithm = "RS256";
var message = jsonwebtoken.sign(payload, key, header);
var decoded = jsonwebtoken.verify(message, pem, {algorithm: "RS256"});
And it works just fine.
I would like to do the same in Java.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:14
- Comments:14 (3 by maintainers)
Top Results From Across the Web
Understand Private Key and Public Key with an Example
Example : A encrypts sensitive information using B's public key and sends it across. B can only access that information and decrypt it...
Read more >Cryptography/A Basic Public Key Example - Wikibooks
The private key pair is used to decrypt messages, and this key will only work if the public key of the same site...
Read more >What would be the examples of a public key and a private key?
An example of a public key would be “Prime Minister #ScoMO” , followed by the private key “I know what happened at #EngadineMaccas...
Read more >How to Create a Public/Private Key Pair
In the example, the path is /home/johndoe/.ssh/id_rsa.pub . At this point, you have created a public/private key pair. Copy the public key and...
Read more >Public-key cryptography - Wikipedia
For example, a journalist can publish the public key of an encryption key pair on a web site so that sources can send...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Here’s what I did during testing using a key pair with the library: Create a RSA keypair in a java keystore:
keytool -alias jwtkey -keyalg RSA -dname "CN=Server,OU=Unit,O=Organization,L=City,S=State,C=US" -keypass keypassword -keystore server.jks -storepass jkspassword
Java code:If you need to get the public key, you can do the following:
The openssl command will output the public key.
Hope this helps.
Using an external library (bouncycastle) or another class is not really necessary at all to read a PEM file. It’s like 2 lines of Java code: