我在将Key(安全性)转换为字符串并将其转换为该对象时遇到问题。我有一些错误:

java.lang.IllegalArgumentException: Illegal base64 character 20

和我的代码:

KeyPairGenerator kpairg = KeyPairGenerator.getInstance("RSA");
kpairg.initialize(1024);
KeyPair kpair = kpairg.genKeyPair();
Key publicKey = kpair.getPublic();

// to String
String textFromKey = Base64.getEncoder().encodeToString(publicKey.getEncoded());

// to Key
byte[] byteKey = Base64.getDecoder().decode(textFromKey.getBytes());
X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(byteKey);
KeyFactory kf = KeyFactory.getInstance("RSA");
Key pubb = kf.generatePublic(X509publicKey);


textFromKey is sending to UDP server
textFromKey is a output from server if I want to use that key
我在那个网站上看到了一些答案,但是有什么可以帮助我的。你能看看这个吗?

IDE告诉我有一个问题:
byte[] byteKey = Base64.getDecoder().decode(textFromKey.getBytes());

最佳答案

仅从代码来看,问题还不清楚。你能尝试做只是

X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(publicKey.getEncoded());


再回来吗

09-05 02:02