java - need to import private key from jks(java keystore)

Vatsal Mehta

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.

Vatsal Mehta

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Loading private key from string or resource in Java JSch in Android app

From Java

Importing the private-key/public-certificate pair in the Java KeyStore

From Dev

How to retrieve my public and private key from the keystore we created

From Dev

how to load the private key from a .der file into java private key object

From Dev

Importing private key to keystore

From Dev

How to export .key and .crt from keystore

From Dev

link between private key and signed certificate in keystore

From Dev

iOS: How to create PKCS12 (P12) keystore from private key and x509certificate in application programmatically?

From Dev

Certificate chain not found, but keystore contains private key

From Dev

Create java keystore from private key and CA certificate bundle

From Dev

Cannot import certificate pfx with private key to store from .net c# 3.5 on windows server 2012

From Dev

In Java, how do I decrypt using the private key from an X509 certificate (public/private key pair) inside a JKS keystore?

From Dev

Import existing private key into BKS Keystore

From Dev

Android - export single key/alias from keystore

From Dev

ssh-keygen command in Java to extract public key from private key

From Dev

Import encrypted AES key into Android Keystore and store it under new alias

From Dev

Import elliptic curve Certificate and Private Key into Java Keystore using java.security.KeyStore

From Dev

Loading private key from string or resource in Java JSch in Android app

From Dev

Import encrypted AES key into Android Keystore and store it under new alias

From Dev

Import elliptic curve Certificate and Private Key into Java Keystore using java.security.KeyStore

From Dev

Java Keystore Private key import

From Dev

Import private key from pem file into keystore

From Dev

Certificate chain not found, but keystore contains private key

From Dev

Import self-signed certificate with private key on Windows from command prompt

From Dev

In Java, how do I decrypt using the private key from an X509 certificate (public/private key pair) inside a JKS keystore?

From Dev

Saving private key with certificate in keystore - Android

From Dev

Java casting error when retrieving private key from keystore

From Dev

Import RSA public/private key from a string Crypto++

From Dev

Import Keystore from Xamarin to Android Studio

Related Related

  1. 1

    Loading private key from string or resource in Java JSch in Android app

  2. 2

    Importing the private-key/public-certificate pair in the Java KeyStore

  3. 3

    How to retrieve my public and private key from the keystore we created

  4. 4

    how to load the private key from a .der file into java private key object

  5. 5

    Importing private key to keystore

  6. 6

    How to export .key and .crt from keystore

  7. 7

    link between private key and signed certificate in keystore

  8. 8

    iOS: How to create PKCS12 (P12) keystore from private key and x509certificate in application programmatically?

  9. 9

    Certificate chain not found, but keystore contains private key

  10. 10

    Create java keystore from private key and CA certificate bundle

  11. 11

    Cannot import certificate pfx with private key to store from .net c# 3.5 on windows server 2012

  12. 12

    In Java, how do I decrypt using the private key from an X509 certificate (public/private key pair) inside a JKS keystore?

  13. 13

    Import existing private key into BKS Keystore

  14. 14

    Android - export single key/alias from keystore

  15. 15

    ssh-keygen command in Java to extract public key from private key

  16. 16

    Import encrypted AES key into Android Keystore and store it under new alias

  17. 17

    Import elliptic curve Certificate and Private Key into Java Keystore using java.security.KeyStore

  18. 18

    Loading private key from string or resource in Java JSch in Android app

  19. 19

    Import encrypted AES key into Android Keystore and store it under new alias

  20. 20

    Import elliptic curve Certificate and Private Key into Java Keystore using java.security.KeyStore

  21. 21

    Java Keystore Private key import

  22. 22

    Import private key from pem file into keystore

  23. 23

    Certificate chain not found, but keystore contains private key

  24. 24

    Import self-signed certificate with private key on Windows from command prompt

  25. 25

    In Java, how do I decrypt using the private key from an X509 certificate (public/private key pair) inside a JKS keystore?

  26. 26

    Saving private key with certificate in keystore - Android

  27. 27

    Java casting error when retrieving private key from keystore

  28. 28

    Import RSA public/private key from a string Crypto++

  29. 29

    Import Keystore from Xamarin to Android Studio

HotTag

Archive