1

I'm working in a program that encripts and decripts stuff using hybrid method, this is, using AES simmetrical and RSA asimmetrical keys. All of this keys get stored in a KeyStore .jks file.

Problem: KeyStore java class needs a Certificate array when you are trying to entry a private key into the keystore file. For example, this code:

String alias = "sth";

Key myKey = generator.generateKeyPair().getPrivate();

char[] keyStorePass = {'1', '2', '3', '4'};

myKeyStore.setKeyEntry(alias, myKey, keyStorePass, null);

will throw an exception because myKey is a private key and I'm providing no certificates to setKeyEntry.

java.lang.IllegalArgumentException: Private key must be accompanied by certificate chain

Now, the real problem here is that I don't know how to generate a certificate to provide to KeyStore class. Java documentation tells you that you need to provide a Certificate[] object, but Eclipse IDE tells me Certificate class is deprecated (which, IIRC, means it shouldn't be used anymore) and also can't be instantiated. So how do I generate a certificate to use across my program?

CR0N0S.LXIII
  • 349
  • 2
  • 11
  • Does Eclipse IDE find [`Certificate`](https://docs.oracle.com/javase/8/docs/api/java/security/cert/Certificate.html), [`Certificate`](https://docs.oracle.com/javase/8/docs/api/javax/security/cert/Certificate.html), or [`Certificate`](https://docs.oracle.com/javase/8/docs/api/java/security/Certificate.html)? – martijno Nov 12 '19 at 13:40
  • It finds all those three. None of them can be instantiated. – CR0N0S.LXIII Nov 12 '19 at 13:45
  • Update: in the meantime I created a certificate with java KeyTool, exported it as a Base64 string, and I'm trying to work with that. I would like to know how to generate the certificate from Jana nonetheless – CR0N0S.LXIII Nov 12 '19 at 14:25
  • Duplicate of https://stackoverflow.com/questions/1615871 and https://stackoverflow.com/questions/11383898 – Robert Nov 12 '19 at 16:16
  • Also https://stackoverflow.com/questions/29852290/self-signed-x509-certificate-with-bouncy-castle-in-java – dave_thompson_085 Nov 12 '19 at 16:58

0 Answers0