0

I have the following code:

private PublicKey getKey(String certificateValue) {
    try {
      CertificateFactory cf = CertificateFactory.getInstance("X.509");
      ByteArrayInputStream bis =
          new ByteArrayInputStream(certificateValue.getBytes(StandardCharsets.UTF_8));

      Certificate certificate = null;
      while (bis.available() > 0) {
        certificate = cf.generateCertificate(bis);
      }

      return certificate.getPublicKey();
  }

I tried to use https://hecpv.wordpress.com/2017/03/18/how-to-generate-x-509-certificate-in-java-1-8/ But I get error:

import sun.security.x509.*;
                   ^
  (package sun.security.x509 is declared in module java.base, which does not export it to the unnamed module)

How I can generate a valid X.509 certificate in order to get the public key?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • 1
    Have you researched [similar questions](https://www.google.com/search?q=java+Generate+X.509+certificate+site:stackoverflow.com) on SO? – andrewJames Nov 21 '22 at 00:32
  • For example: [Create X509 certificate programmactically](https://stackoverflow.com/q/72562465/12567365). Specifically see the [comment](https://stackoverflow.com/questions/72562465/create-x509-certificate-programmactically#comment128184918_72562465) about methods which "_do not create the stated things but rather read them in from preexisting data..._" and about using Bouncy Castle. – andrewJames Nov 21 '22 at 00:32
  • yes, I tried to generate it using keytool but I get `keytool error: java.io.FileNotFoundException: clientkeystore (Access is denied)` – Peter Penzov Nov 21 '22 at 00:41
  • Maybe that is the (new) question, in that case. Maybe you were running the `keytool` command in a directory where you do not have write permission to create a new keystore file. [clientkeystore Access is denied](https://stackoverflow.com/q/50520362/12567365) - and maybe other similar questions. – andrewJames Nov 21 '22 at 01:34

0 Answers0