Interface SymmetricCipher

All Known Implementing Classes:
AesCipher, Sm4Cipher

public interface SymmetricCipher
Interface for symmetric encryption algorithms (CBC, CTR modes). 对称加密算法接口(CBC、CTR 模式)。

Features | 主要功能:

  • Symmetric encryption and decryption - 对称加密和解密
  • Key and IV management - 密钥和 IV 管理

Usage Examples | 使用示例:

SymmetricCipher cipher = AesCipher.cbc();
cipher.setKey(secretKey);
byte[] encrypted = cipher.encrypt(data);

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

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

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

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

      SymmetricCipher setMode(CipherMode mode)
      Set the cipher mode. 设置加密模式。
      Parameters:
      mode - cipher mode / 加密模式
      Returns:
      this cipher instance / 当前加密实例
    • setPadding

      SymmetricCipher setPadding(Padding padding)
      Set the padding scheme. 设置填充方案。
      Parameters:
      padding - padding scheme / 填充方案
      Returns:
      this cipher instance / 当前加密实例
    • encrypt

      byte[] encrypt(byte[] plaintext)
      Encrypt plaintext bytes. 加密明文字节。
      Parameters:
      plaintext - plaintext bytes / 明文字节
      Returns:
      ciphertext bytes / 密文字节
    • encrypt

      byte[] encrypt(String plaintext)
      Encrypt plaintext string. 加密明文字符串。
      Parameters:
      plaintext - plaintext string / 明文字符串
      Returns:
      ciphertext bytes / 密文字节
    • encryptBase64

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

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

      byte[] decrypt(byte[] ciphertext)
      Decrypt ciphertext bytes. 解密密文字节。
      Parameters:
      ciphertext - ciphertext bytes / 密文字节
      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 / 明文字节
    • decryptHex

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

      byte[] generateIv()
      Generate a random initialization vector. 生成随机初始化向量。
      Returns:
      IV bytes / 初始化向量字节
    • getBlockSize

      int getBlockSize()
      Get the block size in bytes. 获取块大小(字节)。
      Returns:
      block size / 块大小
    • getAlgorithm

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

      int getIvLength()
      Get the IV length in bytes. 获取 IV 长度(字节)。
      Returns:
      IV length / IV 长度
    • generateKey

      SecretKey generateKey(int keySize)
      Generate a new secret key. 生成新密钥。
      Parameters:
      keySize - key size in bits / 密钥大小(比特)
      Returns:
      generated secret key / 生成的密钥