Class HybridCrypto
java.lang.Object
cloud.opencode.base.crypto.envelope.HybridCrypto
Hybrid encryption implementation - Simplified envelope encryption with transparent format
混合加密实现 - 简化的信封加密,格式透明
Features | 主要功能:
- Hybrid encryption combining asymmetric and symmetric - 结合非对称和对称的混合加密
- Automatic key negotiation - 自动密钥协商
Usage Examples | 使用示例:
HybridCrypto hybrid = HybridCrypto.rsaAes();
byte[] encrypted = hybrid.encrypt(data, publicKey);
byte[] decrypted = hybrid.decrypt(encrypted, privateKey);
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Null-safe: Yes - 空值安全: 是
- Since:
- JDK 25, opencode-base-crypto V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder for creating custom HybridCrypto instances 用于创建自定义 HybridCrypto 实例的构建器 -
Method Summary
Modifier and TypeMethodDescriptionstatic HybridCrypto.Builderbuilder()Create a new builder for custom configuration 创建用于自定义配置的构建器byte[]decrypt(byte[] ciphertext) Decrypt ciphertext using hybrid encryption 使用混合加密解密密文byte[]decryptBase64(String base64Ciphertext) Decrypt Base64 encoded ciphertext 解密 Base64 编码的密文static HybridCryptoecdhAes()Create hybrid crypto with ECDH and AES 创建使用 ECDH 和 AES 的混合加密byte[]encrypt(byte[] plaintext) Encrypt plaintext using hybrid encryption 使用混合加密加密明文encryptBase64(byte[] plaintext) Encrypt plaintext and return Base64 encoded result 加密明文并返回 Base64 编码结果Get the asymmetric algorithm 获取非对称算法Get the symmetric algorithm 获取对称算法static HybridCryptorsaAes()Create hybrid crypto with RSA and AES (Recommended) 创建使用 RSA 和 AES 的混合加密(推荐)setRecipientPrivateKey(PrivateKey privateKey) Set recipient private key for decryption 设置接收者私钥用于解密setRecipientPublicKey(PublicKey publicKey) Set recipient public key for encryption 设置接收者公钥用于加密static HybridCryptoCreate hybrid crypto with X25519 and ChaCha20 创建使用 X25519 和 ChaCha20 的混合加密
-
Method Details
-
rsaAes
Create hybrid crypto with RSA and AES (Recommended) 创建使用 RSA 和 AES 的混合加密(推荐)- Returns:
- new HybridCrypto instance
-
ecdhAes
Create hybrid crypto with ECDH and AES 创建使用 ECDH 和 AES 的混合加密- Returns:
- new HybridCrypto instance
-
x25519ChaCha20
Create hybrid crypto with X25519 and ChaCha20 创建使用 X25519 和 ChaCha20 的混合加密- Returns:
- new HybridCrypto instance
-
builder
Create a new builder for custom configuration 创建用于自定义配置的构建器- Returns:
- new Builder instance
-
setRecipientPublicKey
Set recipient public key for encryption 设置接收者公钥用于加密- Parameters:
publicKey- recipient's public key- Returns:
- this instance for method chaining
- Throws:
NullPointerException- if publicKey is null
-
setRecipientPrivateKey
Set recipient private key for decryption 设置接收者私钥用于解密- Parameters:
privateKey- recipient's private key- Returns:
- this instance for method chaining
- Throws:
NullPointerException- if privateKey is null
-
encrypt
public byte[] encrypt(byte[] plaintext) Encrypt plaintext using hybrid encryption 使用混合加密加密明文The result is serialized in a compact binary format
- Parameters:
plaintext- data to encrypt- Returns:
- encrypted bytes (serialized envelope)
- Throws:
NullPointerException- if plaintext is nullIllegalStateException- if public key is not setOpenCryptoException- if encryption fails
-
encryptBase64
Encrypt plaintext and return Base64 encoded result 加密明文并返回 Base64 编码结果- Parameters:
plaintext- data to encrypt- Returns:
- Base64 encoded encrypted data
- Throws:
NullPointerException- if plaintext is nullIllegalStateException- if public key is not setOpenCryptoException- if encryption fails
-
decrypt
public byte[] decrypt(byte[] ciphertext) Decrypt ciphertext using hybrid encryption 使用混合加密解密密文- Parameters:
ciphertext- encrypted bytes (serialized envelope)- Returns:
- decrypted plaintext
- Throws:
NullPointerException- if ciphertext is nullIllegalStateException- if private key is not setOpenCryptoException- if decryption fails
-
decryptBase64
Decrypt Base64 encoded ciphertext 解密 Base64 编码的密文- Parameters:
base64Ciphertext- Base64 encoded encrypted data- Returns:
- decrypted plaintext
- Throws:
NullPointerException- if base64Ciphertext is nullIllegalStateException- if private key is not setOpenCryptoException- if decryption fails
-
getAsymmetricAlgorithm
Get the asymmetric algorithm 获取非对称算法- Returns:
- asymmetric algorithm
-
getSymmetricAlgorithm
Get the symmetric algorithm 获取对称算法- Returns:
- symmetric algorithm
-