Class PgpKeyUtil

java.lang.Object
cloud.opencode.base.crypto.pgp.PgpKeyUtil

public final class PgpKeyUtil extends Object
PGP Key Utility - Manages PGP key generation, import, and export PGP 密钥工具 - 管理 PGP 密钥生成、导入和导出

Provides utility methods for:

提供以下功能的工具方法:

  • Key pair generation (RSA, ECDSA, EdDSA) - 密钥对生成
  • Key import from armored ASCII - 从装甲 ASCII 导入密钥
  • Key export to armored ASCII - 导出密钥为装甲 ASCII
  • Key ring management - 密钥环管理

Usage Examples | 使用示例:

// Generate a new key pair
PgpKeyPair keyPair = PgpKeyUtil.generateKeyPair("user@example.com", "passphrase");

// Export public key
String armoredPublicKey = PgpKeyUtil.exportPublicKey(keyPair.publicKey());

// Import public key
PGPPublicKey publicKey = PgpKeyUtil.importPublicKey(armoredPublicKey);

Features | 主要功能:

  • See class description for details - 详见类描述

Security | 安全性:

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

Performance | 性能特性:

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

    • generateKeyPair

      public static PgpKeyPair generateKeyPair(String userId, String passphrase)
      Generates a new PGP key pair with RSA algorithm. 使用 RSA 算法生成新的 PGP 密钥对。
      Parameters:
      userId - the user ID (typically email address)
      passphrase - the passphrase to protect the secret key
      Returns:
      the generated PGP key pair
      Throws:
      OpenKeyException - if key generation fails
    • generateKeyPair

      public static PgpKeyPair generateKeyPair(String userId, String passphrase, int keySize)
      Generates a new PGP key pair with RSA algorithm and specified key size. 使用 RSA 算法和指定密钥大小生成新的 PGP 密钥对。
      Parameters:
      userId - the user ID (typically email address)
      passphrase - the passphrase to protect the secret key
      keySize - the RSA key size in bits (minimum 2048)
      Returns:
      the generated PGP key pair
      Throws:
      OpenKeyException - if key generation fails
    • importPublicKey

      public static org.bouncycastle.openpgp.PGPPublicKey importPublicKey(String armoredKey)
      Imports a PGP public key from armored ASCII format. 从装甲 ASCII 格式导入 PGP 公钥。
      Parameters:
      armoredKey - the armored public key string
      Returns:
      the PGP public key
      Throws:
      OpenKeyException - if import fails
    • importPublicKey

      public static org.bouncycastle.openpgp.PGPPublicKey importPublicKey(byte[] keyData)
      Imports a PGP public key from byte array. 从字节数组导入 PGP 公钥。
      Parameters:
      keyData - the public key bytes
      Returns:
      the PGP public key
      Throws:
      OpenKeyException - if import fails
    • importSecretKey

      public static org.bouncycastle.openpgp.PGPSecretKey importSecretKey(String armoredKey, String passphrase)
      Imports a PGP secret key from armored ASCII format. 从装甲 ASCII 格式导入 PGP 私钥。
      Parameters:
      armoredKey - the armored secret key string
      passphrase - the passphrase to decrypt the key
      Returns:
      the PGP secret key
      Throws:
      OpenKeyException - if import fails
    • importKeyPair

      public static PgpKeyPair importKeyPair(String armoredSecretKey, String passphrase)
      Imports a PGP key pair from armored ASCII format. 从装甲 ASCII 格式导入 PGP 密钥对。
      Parameters:
      armoredSecretKey - the armored secret key string
      passphrase - the passphrase to decrypt the key
      Returns:
      the PGP key pair
      Throws:
      OpenKeyException - if import fails
    • exportPublicKey

      public static String exportPublicKey(org.bouncycastle.openpgp.PGPPublicKey publicKey)
      Exports a PGP public key to armored ASCII format. 将 PGP 公钥导出为装甲 ASCII 格式。
      Parameters:
      publicKey - the public key to export
      Returns:
      the armored public key string
      Throws:
      OpenKeyException - if export fails
    • exportSecretKey

      public static String exportSecretKey(org.bouncycastle.openpgp.PGPSecretKey secretKey)
      Exports a PGP secret key to armored ASCII format. 将 PGP 私钥导出为装甲 ASCII 格式。
      Parameters:
      secretKey - the secret key to export
      Returns:
      the armored secret key string
      Throws:
      OpenKeyException - if export fails
    • exportPublicKeyBytes

      public static byte[] exportPublicKeyBytes(org.bouncycastle.openpgp.PGPPublicKey publicKey)
      Exports a PGP public key to raw bytes. 将 PGP 公钥导出为原始字节。
      Parameters:
      publicKey - the public key to export
      Returns:
      the public key bytes
      Throws:
      OpenKeyException - if export fails
    • extractPrivateKey

      public static org.bouncycastle.openpgp.PGPPrivateKey extractPrivateKey(org.bouncycastle.openpgp.PGPSecretKey secretKey, String passphrase)
      Extracts the private key from a PGP secret key. 从 PGP 私钥中提取私钥。
      Parameters:
      secretKey - the PGP secret key
      passphrase - the passphrase
      Returns:
      the extracted private key
      Throws:
      OpenKeyException - if extraction fails
    • keyIdHex

      public static String keyIdHex(org.bouncycastle.openpgp.PGPPublicKey key)
      Returns the key ID as hexadecimal string. 返回十六进制格式的密钥 ID。
      Parameters:
      key - the PGP public key
      Returns:
      the key ID in hex format
    • fingerprintHex

      public static String fingerprintHex(org.bouncycastle.openpgp.PGPPublicKey key)
      Returns the key fingerprint as hexadecimal string. 返回十六进制格式的密钥指纹。
      Parameters:
      key - the PGP public key
      Returns:
      the fingerprint in hex format