我如何检查base64文本是否是有效的RSA公钥格式(在java中)。
==> 检查是否为 base64
==> 检查 RSA 4096 位的有效 key 。
谢谢
最佳答案
这样的东西应该适合你,请注意我添加的代码中的注释
我引用了这个答案并对代码做了一些修改:How can I construct a java.security.PublicKey object from a base64 encoded string?
public static PublicKey getKey(String key){
try{
//if base64 is invalid, you will see an error here
byte[] byteKey = Base64.getDecoder().decode(key);
//if it is not in RSA public key format, you will see error here as java.security.spec.InvalidKeySpecException
X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(byteKey);
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePublic(X509publicKey);
}
catch(Exception e){
e.printStackTrace();
}
return null;
}
关于java - 检查 Base64 文本是否是有效的 RSA 公钥 (4096),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61583766/