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 Details

    • setKey

      AeadCipher setKey(SecretKey key)
      Set the secret key. 设置密钥。
      Parameters:
      key - secret key / 密钥
      Returns:
      this cipher instance / 当前加密实例
    • setKey

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

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

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

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

      AeadCipher setTagLength(int tagBits)
      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

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

      String encryptBase64(byte[] plaintext)
      Encrypt and encode as Base64. 加密并编码为 Base64。
      Parameters:
      plaintext - plaintext bytes / 明文字节
      Returns:
      Base64 encoded ciphertext / Base64 编码的密文
    • encryptBase64

      String encryptBase64(String plaintext)
      Encrypt string and encode as Base64. 加密字符串并编码为 Base64。
      Parameters:
      plaintext - plaintext string / 明文字符串
      Returns:
      Base64 encoded ciphertext / Base64 编码的密文
    • encryptHex

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

      void encryptFile(Path source, Path target)
      Encrypt a file. 加密文件。
      Parameters:
      source - source file path / 源文件路径
      target - target file path / 目标文件路径
    • encryptStream

      OutputStream encryptStream(OutputStream output)
      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

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

      byte[] decryptBase64(String base64Ciphertext)
      Decrypt Base64 encoded ciphertext. 解密 Base64 编码的密文。
      Parameters:
      base64Ciphertext - Base64 encoded ciphertext / Base64 编码的密文
      Returns:
      plaintext bytes / 明文字节
    • decryptBase64ToString

      String decryptBase64ToString(String base64Ciphertext)
      Decrypt Base64 encoded ciphertext to string. 解密 Base64 编码的密文为字符串。
      Parameters:
      base64Ciphertext - Base64 encoded ciphertext / Base64 编码的密文
      Returns:
      plaintext string / 明文字符串
    • decryptHex

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

      void decryptFile(Path source, Path target)
      Decrypt a file. 解密文件。
      Parameters:
      source - source file path / 源文件路径
      target - target file path / 目标文件路径
    • decryptStream

      InputStream decryptStream(InputStream input)
      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

      String getAlgorithm()
      Get the algorithm name. 获取算法名称。
      Returns:
      algorithm name / 算法名称