0

I have generated a keypair of Public-private keys, and am trying to store the privatekey in Java keyStore. But I am getting error everytime.

My piece of code:

            KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
            keyGen.initialize(2048);
            KeyPair pair = keyGen.generateKeyPair();
            PrivateKey privateKey = pair.getPrivate();
            PublicKey publicKey = pair.getPublic();
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
            String ss = Base64.encodeBase64String(cipher.doFinal(ppp.getBytes("UTF-8")));
            System.out.println(ss);

            // Creating the KeyStore object
            KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());

            // Loading the KeyStore object
            char[] ksPassword = "changeit".toCharArray();
            String path = "C:/Program Files/Java/jre1.8.0_201/lib/security/cacerts";
            java.io.FileInputStream fis = new FileInputStream(path);
            keyStore.load(fis, ksPassword);

            // Creating the KeyStore.ProtectionParameter object
            KeyStore.ProtectionParameter protectionParam = new 
            KeyStore.PasswordProtection(ksPassword);

            // Creating SecretKey object
            SecretKey mySecretKey = new SecretKeySpec(ppp.getBytes(), "RSA");

            // Creating SecretKeyEntry object
            KeyStore.SecretKeyEntry secretKeyEntry = new KeyStore.SecretKeyEntry(mySecretKey);
            keyStore.setEntry("mykeyalias", secretKeyEntry, protectionParam);

            // Storing the KeyStore object
            java.io.FileOutputStream fos = null;
            fos = new java.io.FileOutputStream("newKeyStoreName");
            keyStore.store(fos, ksPassword);

But i'm getting below exception at the line keyStore.setEntry while running:

java.security.KeyStoreException: Cannot store non-PrivateKeys
    at sun.security.provider.JavaKeyStore.engineSetKeyEntry(JavaKeyStore.java:261)
    at sun.security.provider.JavaKeyStore$JKS.engineSetKeyEntry(JavaKeyStore.java:56)
    at java.security.KeyStoreSpi.engineSetEntry(KeyStoreSpi.java:550)
    at sun.security.provider.KeyStoreDelegator.engineSetEntry(KeyStoreDelegator.java:179)
    at sun.security.provider.JavaKeyStore$DualFormatJKS.engineSetEntry(JavaKeyStore.java:70)
    at java.security.KeyStore.setEntry(KeyStore.java:1557)
    at com.sprint.neo.bc4j.util.TestMain.StoringKeys(TestMain.java:33)

Can anyone help to resolve this issue so that I can store Privatekeys to the java keyStore along with an alias name, to be used later. Where is the exact wrong in the above piece of code. Thanks a lot.

Jerry
  • 281
  • 4
  • 21
  • There is no such thing as an RSA `SecretKey`. See [`javax.crypto.spec.SecretKeypSpec`](https://docs.oracle.com/javase/8/docs/api/javax/crypto/spec/SecretKeySpec.html) and [`SecretKeyFactory` Algorithms](https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#SecretKeyFactory). RSA is an asymmetric (public/private) key mechanism, not a symmetric ('secret') key mechanism. – user207421 Sep 08 '20 at 06:23
  • Okay I understood. Then if I want the `public/private` key mechanism, how I can implement that. Can you please suggest? – Jerry Sep 08 '20 at 06:30
  • Unfortunately it is not that simple to store a private key along with the public key in a keystore. You need to build a (self signed) certificate (holds the public key) and store it together with the private key in the keystore. If you don't need a keystore you could save the encoded private and public key (as simple byte array) to a file. For restoration use the keyspecs for private and public key and use the keys. – Michael Fehr Sep 08 '20 at 06:37
  • Ok. :( Can you please have a look at this link below, is it exactly same what you are saying? - https://stackoverflow.com/questions/925377/generate-certificates-public-and-private-keys-with-java – Jerry Sep 08 '20 at 06:44
  • 1
    I recommend that you create a keystore with the Java keytool and use it instead of generating a self sign certificate programmatically. One article showing this is https://www.sslshopper.com/article-how-to-create-a-self-signed-certificate-using-java-keytool.html and the command line for the creation is 'keytool -genkey -keyalg RSA -alias selfsigned -keystore keystore.jks -storepass password -validity 360 -keysize 2048.' – Michael Fehr Sep 08 '20 at 07:40

0 Answers0