java - 将 key 对转换为 RSAKey

标签 java cryptography rsa bouncycastle jce

我正在尝试找出 KeyPair 的位长。在寻找有关如何执行此操作的解决方案时,我遇到了以下代码片段。

public boolean checkKey(RSAKey key) {
    if ( key.getModulus().bitLength() == 1024 )
        return true;
    return false;
}

我期待的输入是“KeyPair”。有人可以向我指出具有以下功能的文档吗:-

  1. 演示如何将 key 对转换为 RSAKey
  2. 或演示如何计算 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/

相关文章:

java - 整数会调用一个类,但不会调用另一个类?

java - 使用脚本引擎管理器评估 Java 字符串代码时出错

c - SipHash24 key "too large for its type"

c# - .NET 和 MySQL 中 SHA1 的区别

c# - 设置 RSA 参数

c - PEM_read_RSAPrivateKey 函数返回 NULL

java - 在 DynamicReports 上创建图表以按年份比较销售额

java - setSize() 与 setPreferredSize() 和 pack()

java.security.NoSuchAlgorithmException : Signature MD5WITHRSA implementation not found

performance - 使用 expt-mod 函数的 Lisp 性能问题