I have a stored my certificate in Java KeyStore which is required for accessing Restful API. To access this API I need to create JWT (Json Web Token) which I am implementing in Java. For generating JWT I need to sign the payload with given private key. So, what I am doing is, importing private key from .jks file into my code
FileInputStream is = new FileInputStream("src/main/resources/file.jks");
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
keystore.load(is, "password".toCharArray());
PrivateKey privateKey = (PrivateKey) keystore.getKey("key", "".toCharArray());
JWTClaimsSet claimsSetOne = new JWTClaimsSet.Builder()
.subject("alias")
.issueTime(new Date(123000L))
.issuer("https://issuer")
.audience("https://audience")
.build();
JWSSigner signer = new RSASSASigner(privateKey);
SignedJWT signedJWT = new SignedJWT (new JWSHeader(JWSAlgorithm.RS256), claimsSetOne);
signedJWT.sign(signer);
String orderOne = signedJWT.serialize();
Now, with this piece of code, It returns private key as "null" value. which results into failure of my code. So, what could be my code so that I can read private key to sign the payload to generate JWT. Or any other alternative to achieve same. Thanks in advance.
Edit : Then I tried this command,
File file = new File("src/main/resources/sho1.jks");
InputStream is = new FileInputStream(file);
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
String password = "sho123";
keystore.load(is, password.toCharArray());
KeyStore.PrivateKeyEntry keyEnt =
(KeyStore.PrivateKeyEntry) keystore.getEntry("sho",new KeyStore.PasswordProtection(password.toCharArray()));
PrivateKey privateKey = keyEnt.getPrivateKey();
It gives me error as Invalid_Signature.
Nothing is wrong in the implementation of JAVA code. This code works fine. Error which I getting as INVALID_SIGNATURE is due to JKS. The certificate which I importing that is not the proper i.e. It does not have correct certificate path. I tried importing correct way then it works. So make sure in this type of scenario you are importing certificate with correct keypair to get correct fingerprint in the JKS. Thanks all.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments