Class PgpCipher

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

public final class PgpCipher extends Object
PGP Cipher - Provides PGP encryption and decryption operations PGP 加密器 - 提供 PGP 加密和解密操作

This class provides fluent API for PGP encryption and decryption, commonly used in email security scenarios.

此类提供 PGP 加密和解密的流式 API,常用于电子邮件安全场景。

Features | 主要功能:

  • Public key encryption - 公钥加密
  • Private key decryption - 私钥解密
  • Armored ASCII output - 装甲 ASCII 输出
  • Compression support - 压缩支持
  • Integrity check - 完整性检查

Usage Examples | 使用示例:

// Create cipher instance
PgpCipher cipher = PgpCipher.create();

// Encrypt with public key
String encrypted = cipher
    .withPublicKey(publicKey)
    .encryptArmored("Hello, World!");

// Decrypt with private key
String decrypted = cipher
    .withSecretKey(secretKey, "passphrase")
    .decryptArmored(encrypted);

Security | 安全性:

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

Performance | 性能特性:

  • Time complexity: O(n) for data, O(k^3) for key operations - 时间复杂度: O(n) 数据处理,O(k^3) 密钥操作
  • Space complexity: O(n) - 空间复杂度: O(n)
Since:
JDK 25, opencode-base-crypto V1.2.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • create

      public static PgpCipher create()
      Creates a new PGP cipher instance. 创建新的 PGP 加密器实例。
      Returns:
      a new PgpCipher instance
    • withPublicKey

      public PgpCipher withPublicKey(org.bouncycastle.openpgp.PGPPublicKey publicKey)
      Sets the public key for encryption. 设置用于加密的公钥。
      Parameters:
      publicKey - the PGP public key
      Returns:
      this cipher instance
    • withPublicKey

      public PgpCipher withPublicKey(String armoredKey)
      Sets the public key from armored ASCII string. 从装甲 ASCII 字符串设置公钥。
      Parameters:
      armoredKey - the armored public key
      Returns:
      this cipher instance
    • withSecretKey

      public PgpCipher withSecretKey(org.bouncycastle.openpgp.PGPSecretKey secretKey, String passphrase)
      Sets the secret key for decryption. 设置用于解密的私钥。
      Parameters:
      secretKey - the PGP secret key
      passphrase - the passphrase to decrypt the key
      Returns:
      this cipher instance
    • withSecretKey

      public PgpCipher withSecretKey(String armoredKey, String passphrase)
      Sets the secret key from armored ASCII string. 从装甲 ASCII 字符串设置私钥。
      Parameters:
      armoredKey - the armored secret key
      passphrase - the passphrase
      Returns:
      this cipher instance
    • withKeyPair

      public PgpCipher withKeyPair(PgpKeyPair keyPair, String passphrase)
      Sets the key pair for both encryption and decryption. 设置用于加密和解密的密钥对。
      Parameters:
      keyPair - the PGP key pair
      passphrase - the passphrase
      Returns:
      this cipher instance
    • withSymmetricAlgorithm

      public PgpCipher withSymmetricAlgorithm(PgpAlgorithm.Symmetric algorithm)
      Sets the symmetric algorithm for encryption. 设置用于加密的对称算法。
      Parameters:
      algorithm - the symmetric algorithm
      Returns:
      this cipher instance
    • withIntegrityCheck

      public PgpCipher withIntegrityCheck(boolean enabled)
      Enables or disables integrity check. 启用或禁用完整性检查。
      Parameters:
      enabled - true to enable integrity check
      Returns:
      this cipher instance
    • withCompression

      public PgpCipher withCompression(boolean enabled)
      Enables or disables compression. 启用或禁用压缩。
      Parameters:
      enabled - true to enable compression
      Returns:
      this cipher instance
    • encryptArmored

      public String encryptArmored(String plaintext)
      Encrypts data and returns armored ASCII string. 加密数据并返回装甲 ASCII 字符串。
      Parameters:
      plaintext - the data to encrypt
      Returns:
      armored encrypted data
      Throws:
      OpenCryptoException - if encryption fails
    • encryptArmored

      public String encryptArmored(byte[] data)
      Encrypts data and returns armored ASCII string. 加密数据并返回装甲 ASCII 字符串。
      Parameters:
      data - the data to encrypt
      Returns:
      armored encrypted data
      Throws:
      OpenCryptoException - if encryption fails
    • encrypt

      public byte[] encrypt(byte[] data)
      Encrypts data and returns raw bytes. 加密数据并返回原始字节。
      Parameters:
      data - the data to encrypt
      Returns:
      encrypted bytes
      Throws:
      OpenCryptoException - if encryption fails
    • encryptBase64

      public String encryptBase64(byte[] data)
      Encrypts data and returns Base64 encoded string. 加密数据并返回 Base64 编码字符串。
      Parameters:
      data - the data to encrypt
      Returns:
      Base64 encoded encrypted data
      Throws:
      OpenCryptoException - if encryption fails
    • decryptArmored

      public String decryptArmored(String armoredData)
      Decrypts armored ASCII data. 解密装甲 ASCII 数据。
      Parameters:
      armoredData - the armored encrypted data
      Returns:
      decrypted string
      Throws:
      OpenCryptoException - if decryption fails
    • decryptArmoredToBytes

      public byte[] decryptArmoredToBytes(String armoredData)
      Decrypts armored ASCII data to bytes. 解密装甲 ASCII 数据为字节。
      Parameters:
      armoredData - the armored encrypted data
      Returns:
      decrypted bytes
      Throws:
      OpenCryptoException - if decryption fails
    • decrypt

      public byte[] decrypt(byte[] encryptedData)
      Decrypts raw encrypted bytes. 解密原始加密字节。
      Parameters:
      encryptedData - the encrypted data
      Returns:
      decrypted bytes
      Throws:
      OpenCryptoException - if decryption fails
    • decryptBase64

      public byte[] decryptBase64(String base64Data)
      Decrypts Base64 encoded data. 解密 Base64 编码数据。
      Parameters:
      base64Data - the Base64 encoded encrypted data
      Returns:
      decrypted bytes
      Throws:
      OpenCryptoException - if decryption fails
    • decryptBase64ToString

      public String decryptBase64ToString(String base64Data)
      Decrypts Base64 encoded data to string. 解密 Base64 编码数据为字符串。
      Parameters:
      base64Data - the Base64 encoded encrypted data
      Returns:
      decrypted string
      Throws:
      OpenCryptoException - if decryption fails
    • getPublicKey

      public org.bouncycastle.openpgp.PGPPublicKey getPublicKey()
      Returns the configured public key. 返回配置的公钥。
      Returns:
      the public key, or null if not set
    • getSecretKey

      public org.bouncycastle.openpgp.PGPSecretKey getSecretKey()
      Returns the configured secret key. 返回配置的私钥。
      Returns:
      the secret key, or null if not set
    • getSymmetricAlgorithm

      public PgpAlgorithm.Symmetric getSymmetricAlgorithm()
      Returns the symmetric algorithm. 返回对称算法。
      Returns:
      the symmetric algorithm