Java RSA 公钥字符串错误

问题描述 投票:0回答:2

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

我很乐意提供任何帮助。

java encryption rsa public
2个回答
2
投票

0x2d 是

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


0
投票

-----开始公钥----- MIIBIJANBgkqhkiG9w0BAQEFAOCAQ8AMIIBCgKCAQEA1OzdsHD2yN2USnmgTpLY otoh974+mQ9nw9T3UmxCossD9WjdPJ7UbHXeMVgFrY53x9YfPFmuA5/YNs1eXzSq Y+GdJIrbdQCHDzWLzNv0mbbAvvv7s706F5HoAPMgjBYV7vZDbiIinupxpx5rns4u AYJTWOyByCoSsJ9ucVg3kxl3dYrRk9VXH3dHar8CiypvDJPnBCS58xXgh12dPJES 35kvtkDmDsMoBcol3PIK7a8HaMQYmYSAPF+TzhURaUAcVJu/jhOUaDf/4w28JdaW xykTaDqOZD72RYb1rovK3uFEfgKBJgaAFhPly2/4m/eNW/SLfwSCR9FvHc88fYpJ bwIDAQAB -----结束公钥-----

© www.soinside.com 2019 - 2024. All rights reserved.