Interface AsymmetricCipher
- All Known Implementing Classes:
EccCipher, RsaCipher, RsaOaepCipher, Sm2Cipher
public interface AsymmetricCipher
Interface for asymmetric encryption operations - Provides fluent API for public/private key cryptography
非对称加密操作接口 - 提供公钥/私钥加密的流式 API
Features | 主要功能:
- Public key encryption and private key decryption - 公钥加密和私钥解密
- Key pair generation and management - 密钥对生成和管理
Usage Examples | 使用示例:
AsymmetricCipher cipher = RsaOaepCipher.sha256();
cipher.setPublicKey(publicKey);
byte[] encrypted = cipher.encrypt(data);
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Null-safe: Partial - 空值安全: 部分
Performance | 性能特性:
- Time complexity: O(k^2) to O(k^3) depending on algorithm - 时间复杂度: O(k^2)~O(k^3),取决于算法
- Space complexity: O(k) - 空间复杂度: O(k)
- Since:
- JDK 25, opencode-base-crypto V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionbyte[]decrypt(byte[] ciphertext) Decrypt data using private key 使用私钥解密数据byte[]decryptBase64(String base64Ciphertext) Decrypt Base64 encoded ciphertext 解密 Base64 编码的密文byte[]decryptHex(String hexCiphertext) Decrypt hexadecimal encoded ciphertext 解密十六进制编码的密文decryptToString(byte[] ciphertext) Decrypt data and return as string 解密数据并返回字符串byte[]encrypt(byte[] plaintext) Encrypt data using public key 使用公钥加密数据byte[]Encrypt string using public key 使用公钥加密字符串encryptBase64(byte[] plaintext) Encrypt data and return Base64 encoded result 加密数据并返回 Base64 编码结果encryptHex(byte[] plaintext) Encrypt data and return hexadecimal encoded result 加密数据并返回十六进制编码结果Generate a new key pair for this cipher 为此加密器生成新的密钥对Get the algorithm name 获取算法名称intGet maximum size of data that can be encrypted in a single operation 获取单次操作可加密的最大数据大小setKeyPair(KeyPair keyPair) Set both public and private keys from key pair 从密钥对设置公钥和私钥setPrivateKey(byte[] encodedKey) Set private key from encoded byte array 从编码的字节数组设置私钥setPrivateKey(PrivateKey privateKey) Set private key for decryption operations 设置用于解密操作的私钥setPrivateKeyPem(String pem) Set private key from PEM formatted string 从 PEM 格式字符串设置私钥setPublicKey(byte[] encodedKey) Set public key from encoded byte array 从编码的字节数组设置公钥setPublicKey(PublicKey publicKey) Set public key for encryption operations 设置用于加密操作的公钥setPublicKeyPem(String pem) Set public key from PEM formatted string 从 PEM 格式字符串设置公钥
-
Method Details
-
setPublicKey
Set public key for encryption operations 设置用于加密操作的公钥- Parameters:
publicKey- the public key- Returns:
- this cipher instance for method chaining
- Throws:
NullPointerException- if publicKey is null
-
setPublicKey
Set public key from encoded byte array 从编码的字节数组设置公钥- Parameters:
encodedKey- the encoded public key bytes- Returns:
- this cipher instance for method chaining
- Throws:
NullPointerException- if encodedKey is nullOpenKeyException- if key format is invalid
-
setPublicKeyPem
Set public key from PEM formatted string 从 PEM 格式字符串设置公钥- Parameters:
pem- the PEM formatted public key- Returns:
- this cipher instance for method chaining
- Throws:
NullPointerException- if pem is nullOpenKeyException- if PEM format is invalid
-
setPrivateKey
Set private key for decryption operations 设置用于解密操作的私钥- Parameters:
privateKey- the private key- Returns:
- this cipher instance for method chaining
- Throws:
NullPointerException- if privateKey is null
-
setPrivateKey
Set private key from encoded byte array 从编码的字节数组设置私钥- Parameters:
encodedKey- the encoded private key bytes- Returns:
- this cipher instance for method chaining
- Throws:
NullPointerException- if encodedKey is nullOpenKeyException- if key format is invalid
-
setPrivateKeyPem
Set private key from PEM formatted string 从 PEM 格式字符串设置私钥- Parameters:
pem- the PEM formatted private key- Returns:
- this cipher instance for method chaining
- Throws:
NullPointerException- if pem is nullOpenKeyException- if PEM format is invalid
-
setKeyPair
Set both public and private keys from key pair 从密钥对设置公钥和私钥- Parameters:
keyPair- the key pair containing public and private keys- Returns:
- this cipher instance for method chaining
- Throws:
NullPointerException- if keyPair is null
-
encrypt
byte[] encrypt(byte[] plaintext) Encrypt data using public key 使用公钥加密数据- Parameters:
plaintext- the data to encrypt- Returns:
- encrypted bytes
- Throws:
NullPointerException- if plaintext is nullIllegalStateException- if public key is not setOpenCryptoException- if encryption fails
-
encrypt
Encrypt string using public key 使用公钥加密字符串- Parameters:
plaintext- the string to encrypt- Returns:
- encrypted bytes
- Throws:
NullPointerException- if plaintext is nullIllegalStateException- if public key is not setOpenCryptoException- if encryption fails
-
encryptBase64
Encrypt data and return Base64 encoded result 加密数据并返回 Base64 编码结果- Parameters:
plaintext- the data to encrypt- Returns:
- Base64 encoded ciphertext
- Throws:
NullPointerException- if plaintext is nullIllegalStateException- if public key is not setOpenCryptoException- if encryption fails
-
encryptHex
Encrypt data and return hexadecimal encoded result 加密数据并返回十六进制编码结果- Parameters:
plaintext- the data to encrypt- Returns:
- hexadecimal encoded ciphertext
- Throws:
NullPointerException- if plaintext is nullIllegalStateException- if public key is not setOpenCryptoException- if encryption fails
-
decrypt
byte[] decrypt(byte[] ciphertext) Decrypt data using private key 使用私钥解密数据- Parameters:
ciphertext- the encrypted data- Returns:
- decrypted bytes
- Throws:
NullPointerException- if ciphertext is nullIllegalStateException- if private key is not setOpenCryptoException- if decryption fails
-
decryptToString
Decrypt data and return as string 解密数据并返回字符串- Parameters:
ciphertext- the encrypted data- Returns:
- decrypted string
- Throws:
NullPointerException- if ciphertext is nullIllegalStateException- if private key is not setOpenCryptoException- if decryption fails
-
decryptBase64
Decrypt Base64 encoded ciphertext 解密 Base64 编码的密文- Parameters:
base64Ciphertext- the Base64 encoded ciphertext- Returns:
- decrypted bytes
- Throws:
NullPointerException- if base64Ciphertext is nullIllegalStateException- if private key is not setOpenCryptoException- if decryption fails
-
decryptHex
Decrypt hexadecimal encoded ciphertext 解密十六进制编码的密文- Parameters:
hexCiphertext- the hexadecimal encoded ciphertext- Returns:
- decrypted bytes
- Throws:
NullPointerException- if hexCiphertext is nullIllegalStateException- if private key is not setOpenCryptoException- if decryption fails
-
getAlgorithm
-
getMaxEncryptSize
int getMaxEncryptSize()Get maximum size of data that can be encrypted in a single operation 获取单次操作可加密的最大数据大小- Returns:
- maximum encrypt size in bytes, or -1 if not applicable
-
generateKeyPair
KeyPair generateKeyPair()Generate a new key pair for this cipher 为此加密器生成新的密钥对- Returns:
- generated key pair
- Throws:
OpenKeyException- if key generation fails
-