Class ChaChaCipher

java.lang.Object
cloud.opencode.base.crypto.symmetric.ChaChaCipher
All Implemented Interfaces:
AeadCipher

public final class ChaChaCipher extends Object implements 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:
  • Method Details

    • create

      public static ChaChaCipher create()
      Create ChaCha20-Poly1305 cipher. 创建 ChaCha20-Poly1305 加密器。
      Returns:
      ChaCha20-Poly1305 cipher instance / ChaCha20-Poly1305 加密实例
    • builder

      public static ChaChaCipher.Builder builder()
      Create a builder for ChaCha20-Poly1305 cipher. 创建 ChaCha20-Poly1305 加密器构建器。
      Returns:
      builder instance / 构建器实例
    • setKey

      public ChaChaCipher setKey(SecretKey key)
      Description copied from interface: AeadCipher
      Set the secret key. 设置密钥。
      Specified by:
      setKey in interface AeadCipher
      Parameters:
      key - secret key / 密钥
      Returns:
      this cipher instance / 当前加密实例
    • setKey

      public ChaChaCipher setKey(byte[] key)
      Description copied from interface: AeadCipher
      Set the secret key from bytes. 从字节数组设置密钥。
      Specified by:
      setKey in interface AeadCipher
      Parameters:
      key - key bytes / 密钥字节
      Returns:
      this cipher instance / 当前加密实例
    • setIv

      public ChaChaCipher setIv(byte[] iv)
      Description copied from interface: AeadCipher
      Set the initialization vector. 设置初始化向量。
      Specified by:
      setIv in interface AeadCipher
      Parameters:
      iv - initialization vector / 初始化向量
      Returns:
      this cipher instance / 当前加密实例
    • setNonce

      public ChaChaCipher setNonce(byte[] nonce)
      Description copied from interface: AeadCipher
      Set the nonce (same as IV for most AEAD ciphers). 设置随机数(对于大多数 AEAD 密码与 IV 相同)。
      Specified by:
      setNonce in interface AeadCipher
      Parameters:
      nonce - nonce bytes / 随机数字节
      Returns:
      this cipher instance / 当前加密实例
    • setAad

      public ChaChaCipher setAad(byte[] aad)
      Description copied from interface: AeadCipher
      Set additional authenticated data (AAD). 设置附加认证数据(AAD)。
      Specified by:
      setAad in interface AeadCipher
      Parameters:
      aad - additional authenticated data / 附加认证数据
      Returns:
      this cipher instance / 当前加密实例
    • setTagLength

      public ChaChaCipher setTagLength(int tagBits)
      Description copied from interface: AeadCipher
      Set authentication tag length in bits. 设置认证标签长度(比特)。
      Specified by:
      setTagLength in interface AeadCipher
      Parameters:
      tagBits - tag length in bits / 标签长度(比特)
      Returns:
      this cipher instance / 当前加密实例
    • encrypt

      public byte[] encrypt(byte[] plaintext)
      Description copied from interface: AeadCipher
      Encrypt plaintext bytes. 加密明文字节。
      Specified by:
      encrypt in interface AeadCipher
      Parameters:
      plaintext - plaintext bytes / 明文字节
      Returns:
      ciphertext with authentication tag / 带认证标签的密文
    • encrypt

      public byte[] encrypt(String plaintext)
      Description copied from interface: AeadCipher
      Encrypt plaintext string. 加密明文字符串。
      Specified by:
      encrypt in interface AeadCipher
      Parameters:
      plaintext - plaintext string / 明文字符串
      Returns:
      ciphertext with authentication tag / 带认证标签的密文
    • encryptBase64

      public String encryptBase64(byte[] plaintext)
      Description copied from interface: AeadCipher
      Encrypt and encode as Base64. 加密并编码为 Base64。
      Specified by:
      encryptBase64 in interface AeadCipher
      Parameters:
      plaintext - plaintext bytes / 明文字节
      Returns:
      Base64 encoded ciphertext / Base64 编码的密文
    • encryptBase64

      public String encryptBase64(String plaintext)
      Description copied from interface: AeadCipher
      Encrypt string and encode as Base64. 加密字符串并编码为 Base64。
      Specified by:
      encryptBase64 in interface AeadCipher
      Parameters:
      plaintext - plaintext string / 明文字符串
      Returns:
      Base64 encoded ciphertext / Base64 编码的密文
    • encryptHex

      public String encryptHex(byte[] plaintext)
      Description copied from interface: AeadCipher
      Encrypt and encode as hexadecimal. 加密并编码为十六进制。
      Specified by:
      encryptHex in interface AeadCipher
      Parameters:
      plaintext - plaintext bytes / 明文字节
      Returns:
      hex encoded ciphertext / 十六进制编码的密文
    • encryptFile

      public void encryptFile(Path source, Path target)
      Description copied from interface: AeadCipher
      Encrypt a file. 加密文件。
      Specified by:
      encryptFile in interface AeadCipher
      Parameters:
      source - source file path / 源文件路径
      target - target file path / 目标文件路径
    • encryptStream

      public OutputStream encryptStream(OutputStream output)
      Description copied from interface: AeadCipher
      Create an encrypting output stream. 创建加密输出流。
      Specified by:
      encryptStream in interface AeadCipher
      Parameters:
      output - underlying output stream / 底层输出流
      Returns:
      encrypting output stream / 加密输出流
    • decrypt

      public byte[] decrypt(byte[] ciphertext)
      Description copied from interface: AeadCipher
      Decrypt ciphertext bytes. 解密密文字节。
      Specified by:
      decrypt in interface AeadCipher
      Parameters:
      ciphertext - ciphertext with authentication tag / 带认证标签的密文
      Returns:
      plaintext bytes / 明文字节
    • decryptToString

      public String decryptToString(byte[] ciphertext)
      Description copied from interface: AeadCipher
      Decrypt and convert to string. 解密并转换为字符串。
      Specified by:
      decryptToString in interface AeadCipher
      Parameters:
      ciphertext - ciphertext bytes / 密文字节
      Returns:
      plaintext string / 明文字符串
    • decryptBase64

      public byte[] decryptBase64(String base64Ciphertext)
      Description copied from interface: AeadCipher
      Decrypt Base64 encoded ciphertext. 解密 Base64 编码的密文。
      Specified by:
      decryptBase64 in interface AeadCipher
      Parameters:
      base64Ciphertext - Base64 encoded ciphertext / Base64 编码的密文
      Returns:
      plaintext bytes / 明文字节
    • decryptBase64ToString

      public String decryptBase64ToString(String base64Ciphertext)
      Description copied from interface: AeadCipher
      Decrypt Base64 encoded ciphertext to string. 解密 Base64 编码的密文为字符串。
      Specified by:
      decryptBase64ToString in interface AeadCipher
      Parameters:
      base64Ciphertext - Base64 encoded ciphertext / Base64 编码的密文
      Returns:
      plaintext string / 明文字符串
    • decryptHex

      public byte[] decryptHex(String hexCiphertext)
      Description copied from interface: AeadCipher
      Decrypt hexadecimal encoded ciphertext. 解密十六进制编码的密文。
      Specified by:
      decryptHex in interface AeadCipher
      Parameters:
      hexCiphertext - hex encoded ciphertext / 十六进制编码的密文
      Returns:
      plaintext bytes / 明文字节
    • decryptFile

      public void decryptFile(Path source, Path target)
      Description copied from interface: AeadCipher
      Decrypt a file. 解密文件。
      Specified by:
      decryptFile in interface AeadCipher
      Parameters:
      source - source file path / 源文件路径
      target - target file path / 目标文件路径
    • decryptStream

      public InputStream decryptStream(InputStream input)
      Description copied from interface: AeadCipher
      Create a decrypting input stream. 创建解密输入流。
      Specified by:
      decryptStream in interface AeadCipher
      Parameters:
      input - underlying input stream / 底层输入流
      Returns:
      decrypting input stream / 解密输入流
    • generateIv

      public byte[] generateIv()
      Description copied from interface: AeadCipher
      Generate a random initialization vector. 生成随机初始化向量。
      Specified by:
      generateIv in interface AeadCipher
      Returns:
      IV bytes / 初始化向量字节
    • generateNonce

      public byte[] generateNonce()
      Description copied from interface: AeadCipher
      Generate a random nonce. 生成随机随机数。
      Specified by:
      generateNonce in interface AeadCipher
      Returns:
      nonce bytes / 随机数字节
    • getIvLength

      public int getIvLength()
      Description copied from interface: AeadCipher
      Get the IV/nonce length in bytes. 获取初始化向量/随机数长度(字节)。
      Specified by:
      getIvLength in interface AeadCipher
      Returns:
      IV length / 初始化向量长度
    • getAlgorithm

      public String getAlgorithm()
      Description copied from interface: AeadCipher
      Get the algorithm name. 获取算法名称。
      Specified by:
      getAlgorithm in interface AeadCipher
      Returns:
      algorithm name / 算法名称
    • getNonce

      public byte[] getNonce()
      Get current nonce (returns a defensive copy). 获取当前随机数(返回防御性副本)。
      Returns:
      nonce bytes / 随机数字节
    • getKey

      public SecretKey getKey()
      Get current key. 获取当前密钥。
      Returns:
      secret key / 密钥
    • 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 / 标签长度