Interface AeadCipher
- All Known Implementing Classes:
AesGcmCipher, ChaChaCipher, Sm4Cipher
public interface AeadCipher
Interface for AEAD (Authenticated Encryption with Associated Data) ciphers.
AEAD(关联数据认证加密)加密接口。
Features | 主要功能:
- Authenticated encryption with associated data - 带关联数据的认证加密
- Automatic nonce generation - 自动随机数生成
Usage Examples | 使用示例:
AeadCipher cipher = AesGcmCipher.aes256Gcm();
byte[] encrypted = cipher.encrypt(data, key);
byte[] decrypted = cipher.decrypt(encrypted, key);
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Null-safe: Partial - 空值安全: 部分
Performance | 性能特性:
- Time complexity: O(n) - 时间复杂度: O(n),n为数据长度
- Space complexity: O(1) - 空间复杂度: O(1)
- 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 ciphertext bytes.byte[]decryptBase64(String base64Ciphertext) Decrypt Base64 encoded ciphertext.decryptBase64ToString(String base64Ciphertext) Decrypt Base64 encoded ciphertext to string.voiddecryptFile(Path source, Path target) Decrypt a file.byte[]decryptHex(String hexCiphertext) Decrypt hexadecimal encoded ciphertext.decryptStream(InputStream input) Create a decrypting input stream.decryptToString(byte[] ciphertext) Decrypt and convert to string.byte[]encrypt(byte[] plaintext) Encrypt plaintext bytes.byte[]Encrypt plaintext string.encryptBase64(byte[] plaintext) Encrypt and encode as Base64.encryptBase64(String plaintext) Encrypt string and encode as Base64.voidencryptFile(Path source, Path target) Encrypt a file.encryptHex(byte[] plaintext) Encrypt and encode as hexadecimal.encryptStream(OutputStream output) Create an encrypting output stream.byte[]Generate a random initialization vector.byte[]Generate a random nonce.Get the algorithm name.intGet the IV/nonce length in bytes.setAad(byte[] aad) Set additional authenticated data (AAD).setIv(byte[] iv) Set the initialization vector.setKey(byte[] key) Set the secret key from bytes.Set the secret key.setNonce(byte[] nonce) Set the nonce (same as IV for most AEAD ciphers).setTagLength(int tagBits) Set authentication tag length in bits.
-
Method Details
-
setKey
Set the secret key. 设置密钥。- Parameters:
key- secret key / 密钥- Returns:
- this cipher instance / 当前加密实例
-
setKey
Set the secret key from bytes. 从字节数组设置密钥。- Parameters:
key- key bytes / 密钥字节- Returns:
- this cipher instance / 当前加密实例
-
setIv
Set the initialization vector. 设置初始化向量。- Parameters:
iv- initialization vector / 初始化向量- Returns:
- this cipher instance / 当前加密实例
-
setNonce
Set the nonce (same as IV for most AEAD ciphers). 设置随机数(对于大多数 AEAD 密码与 IV 相同)。- Parameters:
nonce- nonce bytes / 随机数字节- Returns:
- this cipher instance / 当前加密实例
-
setAad
Set additional authenticated data (AAD). 设置附加认证数据(AAD)。- Parameters:
aad- additional authenticated data / 附加认证数据- Returns:
- this cipher instance / 当前加密实例
-
setTagLength
Set authentication tag length in bits. 设置认证标签长度(比特)。- Parameters:
tagBits- tag length in bits / 标签长度(比特)- Returns:
- this cipher instance / 当前加密实例
-
encrypt
byte[] encrypt(byte[] plaintext) Encrypt plaintext bytes. 加密明文字节。- Parameters:
plaintext- plaintext bytes / 明文字节- Returns:
- ciphertext with authentication tag / 带认证标签的密文
-
encrypt
Encrypt plaintext string. 加密明文字符串。- Parameters:
plaintext- plaintext string / 明文字符串- Returns:
- ciphertext with authentication tag / 带认证标签的密文
-
encryptBase64
Encrypt and encode as Base64. 加密并编码为 Base64。- Parameters:
plaintext- plaintext bytes / 明文字节- Returns:
- Base64 encoded ciphertext / Base64 编码的密文
-
encryptBase64
-
encryptHex
Encrypt and encode as hexadecimal. 加密并编码为十六进制。- Parameters:
plaintext- plaintext bytes / 明文字节- Returns:
- hex encoded ciphertext / 十六进制编码的密文
-
encryptFile
-
encryptStream
Create an encrypting output stream. 创建加密输出流。- Parameters:
output- underlying output stream / 底层输出流- Returns:
- encrypting output stream / 加密输出流
-
decrypt
byte[] decrypt(byte[] ciphertext) Decrypt ciphertext bytes. 解密密文字节。- Parameters:
ciphertext- ciphertext with authentication tag / 带认证标签的密文- Returns:
- plaintext bytes / 明文字节
-
decryptToString
Decrypt and convert to string. 解密并转换为字符串。- Parameters:
ciphertext- ciphertext bytes / 密文字节- Returns:
- plaintext string / 明文字符串
-
decryptBase64
Decrypt Base64 encoded ciphertext. 解密 Base64 编码的密文。- Parameters:
base64Ciphertext- Base64 encoded ciphertext / Base64 编码的密文- Returns:
- plaintext bytes / 明文字节
-
decryptBase64ToString
-
decryptHex
Decrypt hexadecimal encoded ciphertext. 解密十六进制编码的密文。- Parameters:
hexCiphertext- hex encoded ciphertext / 十六进制编码的密文- Returns:
- plaintext bytes / 明文字节
-
decryptFile
-
decryptStream
Create a decrypting input stream. 创建解密输入流。- Parameters:
input- underlying input stream / 底层输入流- Returns:
- decrypting input stream / 解密输入流
-
generateIv
byte[] generateIv()Generate a random initialization vector. 生成随机初始化向量。- Returns:
- IV bytes / 初始化向量字节
-
generateNonce
byte[] generateNonce()Generate a random nonce. 生成随机随机数。- Returns:
- nonce bytes / 随机数字节
-
getIvLength
int getIvLength()Get the IV/nonce length in bytes. 获取初始化向量/随机数长度(字节)。- Returns:
- IV length / 初始化向量长度
-
getAlgorithm
-