检查base64文本是否是有效的RSA公钥(4096)



如何检查base64文本是否是有效的RSA公钥格式(在java中(。

===>检查base64 中的

===>检查RSA 4096位的有效密钥。

谢谢

类似的东西应该适用于您,请注意我添加的代码中的注释

我参考了这个答案,并对代码进行了一些修改:如何从base64编码的字符串中构造java.security.PublicKey对象?

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;
}

最新更新