Using private/public key example #827
-
Hi, 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:
And it works just fine. I would like to do the same in Java. |
Beta Was this translation helpful? Give feedback.
Replies: 14 comments
-
Here's what I did during testing using a key pair with the library:
If you need to get the public key, you can do the following:
The openssl command will output the public key. Hope this helps. |
Beta Was this translation helpful? Give feedback.
-
Java doesn't support http://stackoverflow.com/questions/11787571/how-to-read-pem-file-to-get-private-and-public-key |
Beta Was this translation helpful? Give feedback.
-
@csmithmtb your keystore command is missing the |
Beta Was this translation helpful? Give feedback.
-
Can anyone provide another more simple example, please? import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
public class Application {
public static void main(String[] args) {
String privateKey = "privateKey";
String publicKey = "publicKey";
String compactedJWT = Jwts.builder().setSubject("syle")
.setIssuer("localhost")
.signWith(SignatureAlgorithm.RS256, <how about this?>)
.compact();
System.out.println(compactedJWT);
}
} I don't know how to create a Key object if i already have a private and public key pair in two text file? |
Beta Was this translation helpful? Give feedback.
-
@lekhasy your question is a general JVM question (about key pair files) and not related to JJWT specifically. Did you read my comment above about converting .pem files on the JVM? http://stackoverflow.com/questions/11787571/how-to-read-pem-file-to-get-private-and-public-key HTH! |
Beta Was this translation helpful? Give feedback.
-
Ok, is there anyway to just verify a key using just a public key (perhaps pulled from public rest end point), without going through the pain of a key store? |
Beta Was this translation helpful? Give feedback.
-
@cope here is what I got working: that should produce the base64 encoded private key. For the Base64 encoded public key (easy to use with Spring Boot/Cloud oauth2 and jwt libs): BUT, to later "manually" read this public key, you need it in Ok, so now you have that
Refs: |
Beta Was this translation helpful? Give feedback.
-
You should be able to do so. Generate the key pair into a keystore. The OAuth2 server app uses the keystore to sign the JWT. Export the public key to a file from the keystore to a PEM file(?). You can copy & paste the contents in the application.yml file in your client apps as 'security.oauth2.resource.jwt.key-value'. security: |
Beta Was this translation helpful? Give feedback.
-
Just a follow up: This is currently outside the scope of JJWT since it s a general Java question (of how to convert a .pem file (or contents) to java That said, it would be nice if JJWT had a helper function that could do this. However, meeting JWT RFC compliance is our highest priority, and pem-to-javaKey work isn't high on our list right now. I'll tag this issue as If anyone does want to take a crack at this so it can be used by JJWT users, please open a discussion here first to discuss where the code should live and how it should be decoupled before you spend a lot of work on a PR only to find it rejected due to organization or design incompatibilities with our current work. |
Beta Was this translation helpful? Give feedback.
-
I had same requirement, I have solved using Auth0's utils, documenting it for others,
|
Beta Was this translation helpful? Give feedback.
-
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:
|
Beta Was this translation helpful? Give feedback.
-
Full example for generating, saving, loading and using a public/private key pair: import io.jsonwebtoken.*;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.io.Encoders;
import io.jsonwebtoken.security.Keys;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
public class JwtExample {
public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException {
KeyPair keyPair = Keys.keyPairFor(SignatureAlgorithm.ES512);
String encodedPublicKeyBase64 = Encoders.BASE64.encode(keyPair.getPublic().getEncoded());
String encodedPrivateKeyBase64 = Encoders.BASE64.encode(keyPair.getPrivate().getEncoded());
byte[] encodedPublicKeyBytes = Decoders.BASE64.decode(encodedPublicKeyBase64);
byte[] encodedPrivateKeyBytes = Decoders.BASE64.decode(encodedPrivateKeyBase64);
KeyFactory keyFactory = KeyFactory.getInstance("EC");
PublicKey publicKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedPublicKeyBytes));
PrivateKey privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(encodedPrivateKeyBytes));
String jwt = Jwts.builder().setIssuer("example").claim("foo", "bar").signWith(privateKey).compact();
JwtParser parser = Jwts.parserBuilder().setSigningKey(publicKey).build();
Jws<Claims> jws = parser.parseClaimsJws(jwt);
System.out.println(jws); // header={alg=ES512},body={iss=example, foo=bar},signature=AKoAps_bw...
}
} |
Beta Was this translation helpful? Give feedback.
-
thank you. it's helpful |
Beta Was this translation helpful? Give feedback.
-
Using a copy/paste Private key PEM-format and Kotlin/Android:
|
Beta Was this translation helpful? Give feedback.
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: