1

I'm trying to convert a string to RSA public key, but I always get errors... Public Key:

    -----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1OzdsHD2yN2USnmgTplY
otoh974+mQ9nw9T3UmxCossD9WjdPJ7UbHXeMVgFrY53x9YfPFmuA5/YNs1eXzSq
Y+GdJIrbdQCHDzWLzNv0mbbAvvv7s706F5HoAPMgjBYV7vZDbiIinupxpx5rns4u
AYJTWOyByCoSsJ9ucVg3kxl3dYrRk9VXH3dHar8CiypvDJPnBCS58xXgh12dPJES
35kvtkDmDsMoBcol3PIK7a8HaMQYmYsAPF+TzhURaUAcVJu/jhOUaDf/4w28JdaW
xykTaDqOZD72RYb1rovK3uFEfgKBJgaAFhPlY2/4m/eNW/SLfwSCR9FvHc88fYpJ
bwIDAQAB
-----END PUBLIC KEY-----

The code:

pub.replace("-----BEGIN PUBLIC KEY-----\n", "").replace("-----END PUBLIC KEY-----", "");
        byte[] publicByte = Base64.getDecoder().decode(pub);
        System.out.println(publicByte.length+"deb");

        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicByte);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");

        PublicKey pkey = keyFactory.generatePublic(keySpec);

Error:

java.lang.IllegalArgumentException: Illegal base64 character 2d

I would be happy for any help.

1 Answer 1

2

0x2d is -. The hyphens etc. are still there. String.replace() returns the new value. It doesn't mutate the string you called it on.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.