A Private Key always accompanied by a Certificate Chain sign (which includes the corresponding certificate) in the KeyStore. You cannot just add it to KeyStore yourself.
After creating a Private Key you can create a self-signed certificate, then you can use this certificate to add your private key along with the certificate in KeyStore.
Create a self-signed certificate
openssl req -new -x509 -key [PRIVATE_KEY_FILE] -out [SELF_SIGNED_CERTIFICATE_FILE] days 3650 -subj / [YOUR_SUBJECT_DN]
Creating a PKCS # 12 File Containing PrivateKey and Certificate
openssl pkcs12 -export -inkey [PRIVATE_KEY_FILE] -in [CERTIFICATE_FILE] -out [PKCS12_FILE.p12] -name mykey
Finally, convert KeyStore PKCS12 to your desired BKS storage type
keytool -importkeystore -srckeystore [ABOVE_P12_FILE] -srcstorepass [ABOVE_P12_PASSWORD] -srcstoretype pkcs12 -destkeystore [NEW_P12_FILE.p12] -deststorepass [NEW_P12_PASSWORD] -deststoretype bks -providerclass org.bouncycastle.jce.provider.BouncyCastleProvider -providerpath [ABSOLUTE_PATH_TO__bcprov-jdk15on-152 .jar]
If you need the default Java storage type JKS , you can remove the arguments -providerclass and -providerpath from the last command.
source share