1

I generated a public key in Java from a .PEM file as follows :

PublicKey pub = null;
        KeyFactory factory = KeyFactory.getInstance("RSA", "BC");
        try {
            /*PrivateKey priv = generatePrivateKey(factory, RESOURCES_DIR
                    + "id_rsa");*/

            pub = generatePublicKey(factory, RESOURCES_DIR
                    + "rsa_2048_pub.pem");

        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        }

And then I converted it to string as follows :

String encodedPublicKey = Base64.getEncoder().encodeToString(pub.getEncoded());

Now how can I convert it from string to public key to have it as type of PublicKey again ?

Questioner
  • 662
  • 1
  • 10
  • 26
  • @ Sandesh Gupta Yes but whe i use byte[] publicBytes = Base64.decodeBase64(String_PublicKey) i get this error: "The method Base64.decodeBase64(String) is undefined for the type Base64" you know what is the reason? – Questioner Apr 23 '18 at 08:49
  • That is because there are many base 64 classes (BC, Apache, Guava, Java and the Sun internal one that should not be used); the `java.util` one didn't exist yet. But it is pretty good, so you can just replace the base 64 decoder by `Base64.getDecoder()` (etc.). Or you can lookup the method and use the right `import` statement. – Maarten Bodewes Apr 23 '18 at 08:52
  • Yes i used Base64.getDecoder().decode(encodedPublicKey) and it worked. Thanks – Questioner Apr 23 '18 at 08:59

0 Answers0