字符串错误引起的Java RSA公钥

时间:2018-07-10 11:56:05

标签: java encryption rsa public

我正在尝试将字符串转换为RSA公钥,但是我总是遇到错误... 公钥:

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

代码:

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);

错误:

java.lang.IllegalArgumentException: Illegal base64 character 2d

我很乐意提供帮助。

1 个答案:

答案 0 :(得分:2)

0x2d为-。连字符等仍然存在。 String.replace() 返回新值。它不会改变您调用它的字符串。