Class ChaChaCipher
java.lang.Object
cloud.opencode.base.crypto.symmetric.ChaChaCipher
- All Implemented Interfaces:
AeadCipher
ChaCha20-Poly1305 cipher implementation - Modern AEAD cipher
ChaCha20-Poly1305 加密实现 - 现代 AEAD 加密
Features | 主要功能:
- ChaCha20-Poly1305 authenticated encryption - ChaCha20-Poly1305 认证加密
- 256-bit key, 96-bit nonce - 256 位密钥,96 位随机数
Usage Examples | 使用示例:
AeadCipher chacha = ChaChaCipher.create();
byte[] encrypted = chacha.encrypt(plaintext, key);
Security | 安全性:
- Thread-safe: No - 线程安全: 否
- 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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder class for ChaCha20-Poly1305 cipher. -
Method Summary
Modifier and TypeMethodDescriptionstatic ChaChaCipher.Builderbuilder()Create a builder for ChaCha20-Poly1305 cipher.static ChaChaCiphercreate()Create ChaCha20-Poly1305 cipher.byte[]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.getKey()Get current key.byte[]getNonce()Get current nonce (returns a defensive copy).intGet nonce length in bytes.intGet authentication tag length in bits.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
-
create
Create ChaCha20-Poly1305 cipher. 创建 ChaCha20-Poly1305 加密器。- Returns:
- ChaCha20-Poly1305 cipher instance / ChaCha20-Poly1305 加密实例
-
builder
Create a builder for ChaCha20-Poly1305 cipher. 创建 ChaCha20-Poly1305 加密器构建器。- Returns:
- builder instance / 构建器实例
-
setKey
Description copied from interface:AeadCipherSet the secret key. 设置密钥。- Specified by:
setKeyin interfaceAeadCipher- Parameters:
key- secret key / 密钥- Returns:
- this cipher instance / 当前加密实例
-
setKey
Description copied from interface:AeadCipherSet the secret key from bytes. 从字节数组设置密钥。- Specified by:
setKeyin interfaceAeadCipher- Parameters:
key- key bytes / 密钥字节- Returns:
- this cipher instance / 当前加密实例
-
setIv
Description copied from interface:AeadCipherSet the initialization vector. 设置初始化向量。- Specified by:
setIvin interfaceAeadCipher- Parameters:
iv- initialization vector / 初始化向量- Returns:
- this cipher instance / 当前加密实例
-
setNonce
Description copied from interface:AeadCipherSet the nonce (same as IV for most AEAD ciphers). 设置随机数(对于大多数 AEAD 密码与 IV 相同)。- Specified by:
setNoncein interfaceAeadCipher- Parameters:
nonce- nonce bytes / 随机数字节- Returns:
- this cipher instance / 当前加密实例
-
setAad
Description copied from interface:AeadCipherSet additional authenticated data (AAD). 设置附加认证数据(AAD)。- Specified by:
setAadin interfaceAeadCipher- Parameters:
aad- additional authenticated data / 附加认证数据- Returns:
- this cipher instance / 当前加密实例
-
setTagLength
Description copied from interface:AeadCipherSet authentication tag length in bits. 设置认证标签长度(比特)。- Specified by:
setTagLengthin interfaceAeadCipher- Parameters:
tagBits- tag length in bits / 标签长度(比特)- Returns:
- this cipher instance / 当前加密实例
-
encrypt
public byte[] encrypt(byte[] plaintext) Description copied from interface:AeadCipherEncrypt plaintext bytes. 加密明文字节。- Specified by:
encryptin interfaceAeadCipher- Parameters:
plaintext- plaintext bytes / 明文字节- Returns:
- ciphertext with authentication tag / 带认证标签的密文
-
encrypt
Description copied from interface:AeadCipherEncrypt plaintext string. 加密明文字符串。- Specified by:
encryptin interfaceAeadCipher- Parameters:
plaintext- plaintext string / 明文字符串- Returns:
- ciphertext with authentication tag / 带认证标签的密文
-
encryptBase64
Description copied from interface:AeadCipherEncrypt and encode as Base64. 加密并编码为 Base64。- Specified by:
encryptBase64in interfaceAeadCipher- Parameters:
plaintext- plaintext bytes / 明文字节- Returns:
- Base64 encoded ciphertext / Base64 编码的密文
-
encryptBase64
Description copied from interface:AeadCipherEncrypt string and encode as Base64. 加密字符串并编码为 Base64。- Specified by:
encryptBase64in interfaceAeadCipher- Parameters:
plaintext- plaintext string / 明文字符串- Returns:
- Base64 encoded ciphertext / Base64 编码的密文
-
encryptHex
Description copied from interface:AeadCipherEncrypt and encode as hexadecimal. 加密并编码为十六进制。- Specified by:
encryptHexin interfaceAeadCipher- Parameters:
plaintext- plaintext bytes / 明文字节- Returns:
- hex encoded ciphertext / 十六进制编码的密文
-
encryptFile
Description copied from interface:AeadCipherEncrypt a file. 加密文件。- Specified by:
encryptFilein interfaceAeadCipher- Parameters:
source- source file path / 源文件路径target- target file path / 目标文件路径
-
encryptStream
Description copied from interface:AeadCipherCreate an encrypting output stream. 创建加密输出流。- Specified by:
encryptStreamin interfaceAeadCipher- Parameters:
output- underlying output stream / 底层输出流- Returns:
- encrypting output stream / 加密输出流
-
decrypt
public byte[] decrypt(byte[] ciphertext) Description copied from interface:AeadCipherDecrypt ciphertext bytes. 解密密文字节。- Specified by:
decryptin interfaceAeadCipher- Parameters:
ciphertext- ciphertext with authentication tag / 带认证标签的密文- Returns:
- plaintext bytes / 明文字节
-
decryptToString
Description copied from interface:AeadCipherDecrypt and convert to string. 解密并转换为字符串。- Specified by:
decryptToStringin interfaceAeadCipher- Parameters:
ciphertext- ciphertext bytes / 密文字节- Returns:
- plaintext string / 明文字符串
-
decryptBase64
Description copied from interface:AeadCipherDecrypt Base64 encoded ciphertext. 解密 Base64 编码的密文。- Specified by:
decryptBase64in interfaceAeadCipher- Parameters:
base64Ciphertext- Base64 encoded ciphertext / Base64 编码的密文- Returns:
- plaintext bytes / 明文字节
-
decryptBase64ToString
Description copied from interface:AeadCipherDecrypt Base64 encoded ciphertext to string. 解密 Base64 编码的密文为字符串。- Specified by:
decryptBase64ToStringin interfaceAeadCipher- Parameters:
base64Ciphertext- Base64 encoded ciphertext / Base64 编码的密文- Returns:
- plaintext string / 明文字符串
-
decryptHex
Description copied from interface:AeadCipherDecrypt hexadecimal encoded ciphertext. 解密十六进制编码的密文。- Specified by:
decryptHexin interfaceAeadCipher- Parameters:
hexCiphertext- hex encoded ciphertext / 十六进制编码的密文- Returns:
- plaintext bytes / 明文字节
-
decryptFile
Description copied from interface:AeadCipherDecrypt a file. 解密文件。- Specified by:
decryptFilein interfaceAeadCipher- Parameters:
source- source file path / 源文件路径target- target file path / 目标文件路径
-
decryptStream
Description copied from interface:AeadCipherCreate a decrypting input stream. 创建解密输入流。- Specified by:
decryptStreamin interfaceAeadCipher- Parameters:
input- underlying input stream / 底层输入流- Returns:
- decrypting input stream / 解密输入流
-
generateIv
public byte[] generateIv()Description copied from interface:AeadCipherGenerate a random initialization vector. 生成随机初始化向量。- Specified by:
generateIvin interfaceAeadCipher- Returns:
- IV bytes / 初始化向量字节
-
generateNonce
public byte[] generateNonce()Description copied from interface:AeadCipherGenerate a random nonce. 生成随机随机数。- Specified by:
generateNoncein interfaceAeadCipher- Returns:
- nonce bytes / 随机数字节
-
getIvLength
public int getIvLength()Description copied from interface:AeadCipherGet the IV/nonce length in bytes. 获取初始化向量/随机数长度(字节)。- Specified by:
getIvLengthin interfaceAeadCipher- Returns:
- IV length / 初始化向量长度
-
getAlgorithm
Description copied from interface:AeadCipherGet the algorithm name. 获取算法名称。- Specified by:
getAlgorithmin interfaceAeadCipher- Returns:
- algorithm name / 算法名称
-
getNonce
public byte[] getNonce()Get current nonce (returns a defensive copy). 获取当前随机数(返回防御性副本)。- Returns:
- nonce bytes / 随机数字节
-
getKey
-
getNonceLength
public int getNonceLength()Get nonce length in bytes. 获取随机数长度(字节)。- Returns:
- nonce length / 随机数长度
-
getTagLength
public int getTagLength()Get authentication tag length in bits. 获取认证标签长度(比特)。- Returns:
- tag length / 标签长度
-