Class KeyPairUtil

java.lang.Object
cloud.opencode.base.crypto.key.KeyPairUtil

public final class KeyPairUtil extends Object
Key pair utility class - Utility methods for working with asymmetric key pairs 密钥对工具类 - 用于处理非对称密钥对的实用方法

Features | 主要功能:

  • Key pair serialization and deserialization - 密钥对序列化和反序列化
  • PEM and DER format support - PEM 和 DER 格式支持

Usage Examples | 使用示例:

String pem = KeyPairUtil.toPem(keyPair);
KeyPair restored = KeyPairUtil.fromPem(pem, "RSA");

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Null-safe: Yes - 空值安全: 是

Performance | 性能特性:

  • Time complexity: O(k^3) - 时间复杂度: O(k^3),k为密钥位数
  • Space complexity: O(k) - 空间复杂度: O(k)
Since:
JDK 25, opencode-base-crypto V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • generate

      public static KeyPair generate(String algorithm, int keySize)
      Generate key pair with specified algorithm and key size 使用指定的算法和密钥大小生成密钥对
      Parameters:
      algorithm - key algorithm (e.g., "RSA", "EC")
      keySize - key size in bits
      Returns:
      generated key pair
      Throws:
      OpenKeyException - if key generation fails
    • generateEc

      public static KeyPair generateEc(CurveType curve)
      Generate elliptic curve key pair with specified curve 使用指定的曲线生成椭圆曲线密钥对
      Parameters:
      curve - elliptic curve type
      Returns:
      generated EC key pair
      Throws:
      OpenKeyException - if key generation fails
    • extractPublicKey

      public static PublicKey extractPublicKey(PrivateKey privateKey)
      Extract public key from private key (for RSA and EC keys) 从私钥中提取公钥(适用于 RSA 和 EC 密钥)
      Parameters:
      privateKey - private key
      Returns:
      extracted public key
      Throws:
      OpenKeyException - if extraction fails
    • isMatchingPair

      public static boolean isMatchingPair(PublicKey publicKey, PrivateKey privateKey)
      Check if public and private keys form a matching pair 检查公钥和私钥是否匹配
      Parameters:
      publicKey - public key
      privateKey - private key
      Returns:
      true if keys form a matching pair
      Throws:
      OpenKeyException - if validation fails