我正在尝试将字符串转换为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


我很乐意提供帮助。

最佳答案

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

10-05 18:35