Class PgpCipher
java.lang.Object
cloud.opencode.base.crypto.pgp.PgpCipher
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 Summary
Modifier and TypeMethodDescriptionstatic PgpCiphercreate()Creates a new PGP cipher instance.byte[]decrypt(byte[] encryptedData) Decrypts raw encrypted bytes.decryptArmored(String armoredData) Decrypts armored ASCII data.byte[]decryptArmoredToBytes(String armoredData) Decrypts armored ASCII data to bytes.byte[]decryptBase64(String base64Data) Decrypts Base64 encoded data.decryptBase64ToString(String base64Data) Decrypts Base64 encoded data to string.byte[]encrypt(byte[] data) Encrypts data and returns raw bytes.encryptArmored(byte[] data) Encrypts data and returns armored ASCII string.encryptArmored(String plaintext) Encrypts data and returns armored ASCII string.encryptBase64(byte[] data) Encrypts data and returns Base64 encoded string.org.bouncycastle.openpgp.PGPPublicKeyReturns the configured public key.org.bouncycastle.openpgp.PGPSecretKeyReturns the configured secret key.Returns the symmetric algorithm.withCompression(boolean enabled) Enables or disables compression.withIntegrityCheck(boolean enabled) Enables or disables integrity check.withKeyPair(PgpKeyPair keyPair, String passphrase) Sets the key pair for both encryption and decryption.withPublicKey(String armoredKey) Sets the public key from armored ASCII string.withPublicKey(org.bouncycastle.openpgp.PGPPublicKey publicKey) Sets the public key for encryption.withSecretKey(String armoredKey, String passphrase) Sets the secret key from armored ASCII string.withSecretKey(org.bouncycastle.openpgp.PGPSecretKey secretKey, String passphrase) Sets the secret key for decryption.withSymmetricAlgorithm(PgpAlgorithm.Symmetric algorithm) Sets the symmetric algorithm for encryption.
-
Method Details
-
create
Creates a new PGP cipher instance. 创建新的 PGP 加密器实例。- Returns:
- a new PgpCipher instance
-
withPublicKey
Sets the public key for encryption. 设置用于加密的公钥。- Parameters:
publicKey- the PGP public key- Returns:
- this cipher instance
-
withPublicKey
-
withSecretKey
-
withSecretKey
-
withKeyPair
Sets the key pair for both encryption and decryption. 设置用于加密和解密的密钥对。- Parameters:
keyPair- the PGP key pairpassphrase- the passphrase- Returns:
- this cipher instance
-
withSymmetricAlgorithm
Sets the symmetric algorithm for encryption. 设置用于加密的对称算法。- Parameters:
algorithm- the symmetric algorithm- Returns:
- this cipher instance
-
withIntegrityCheck
Enables or disables integrity check. 启用或禁用完整性检查。- Parameters:
enabled- true to enable integrity check- Returns:
- this cipher instance
-
withCompression
Enables or disables compression. 启用或禁用压缩。- Parameters:
enabled- true to enable compression- Returns:
- this cipher instance
-
encryptArmored
Encrypts data and returns armored ASCII string. 加密数据并返回装甲 ASCII 字符串。- Parameters:
plaintext- the data to encrypt- Returns:
- armored encrypted data
- Throws:
OpenCryptoException- if encryption fails
-
encryptArmored
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
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
Decrypts armored ASCII data. 解密装甲 ASCII 数据。- Parameters:
armoredData- the armored encrypted data- Returns:
- decrypted string
- Throws:
OpenCryptoException- if decryption fails
-
decryptArmoredToBytes
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
Decrypts Base64 encoded data. 解密 Base64 编码数据。- Parameters:
base64Data- the Base64 encoded encrypted data- Returns:
- decrypted bytes
- Throws:
OpenCryptoException- if decryption fails
-
decryptBase64ToString
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
Returns the symmetric algorithm. 返回对称算法。- Returns:
- the symmetric algorithm
-