我将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
我在那个网站上看到了一些答案但是有什么帮助我。你能看看这个吗?
byte[] byteKey = Base64.getDecoder().decode(textFromKey.getBytes());
答案 0 :(得分:-1)
问题不仅仅来自代码。你可以试试吗
X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(publicKey.getEncoded());
然后回来?