我正在尝试找出 KeyPair 的位长。在寻找有关如何执行此操作的解决方案时,我遇到了以下代码片段。
public boolean checkKey(RSAKey key) {
if ( key.getModulus().bitLength() == 1024 )
return true;
return false;
}
我期待的输入是“KeyPair”。有人可以向我指出具有以下功能的文档吗:-
- 演示如何将 key 对转换为 RSAKey
- 或演示如何计算 key 对的位长度
最佳答案
这应该对 RSA key 执行此操作(假设 keyPair
是一个 KeyPair
实例):
PublicKey publicKey = keyPair.getPublic();
if (publicKey instanceof RSAPublicKey) {
return ((RSAPublicKey) publicKey).getModulus().bitLength();
}
如果您需要检查其他 key 类型,只需相应地检查文档和代码,例如:
if (publicKey instanceof DSAPublicKey) {
return ((DSAPublicKey) publicKey).getY().bitLength();
}
关于java - 将 key 对转换为 RSAKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15218944/