Class OpenSymmetric

java.lang.Object
cloud.opencode.base.crypto.OpenSymmetric

public final class OpenSymmetric extends Object
Symmetric encryption facade for encrypt/decrypt operations - Provides convenient API for various symmetric algorithms 对称加密门面类 - 为各种对称加密算法提供便捷的 API

Features | 主要功能:

  • AES-CBC and AES-CTR encryption - AES-CBC 和 AES-CTR 加密
  • SM4-CBC encryption support - SM4-CBC 加密支持
  • Hex and Base64 output encoding - 十六进制和 Base64 输出编码
  • Key and IV generation - 密钥和 IV 生成

Usage Examples | 使用示例:

OpenSymmetric cipher = OpenSymmetric.aesCbc();
SecretKey key = cipher.generateKey(256);
cipher.setKey(key);
byte[] iv = cipher.generateIv();
cipher.setIv(iv);
String encrypted = cipher.encryptBase64("Hello, World!");
String decrypted = cipher.decryptBase64ToString(encrypted);

Security | 安全性:

  • Thread-safe: No - 线程安全: 否
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-crypto V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • aesCbc

      public static OpenSymmetric aesCbc()
      Create AES-CBC cipher 创建 AES-CBC 加密
      Returns:
      OpenSymmetric instance
    • aesCtr

      public static OpenSymmetric aesCtr()
      Create AES-CTR cipher 创建 AES-CTR 加密
      Returns:
      OpenSymmetric instance
    • of

      public static OpenSymmetric of(SymmetricAlgorithm algorithm)
      Create cipher by algorithm enum 根据算法枚举创建加密器
      Parameters:
      algorithm - symmetric algorithm
      Returns:
      OpenSymmetric instance
    • setKey

      public OpenSymmetric setKey(SecretKey key)
      Set secret key 设置密钥
      Parameters:
      key - secret key
      Returns:
      this instance for chaining
    • setKey

      public OpenSymmetric setKey(byte[] key)
      Set secret key from bytes 从字节设置密钥
      Parameters:
      key - key bytes
      Returns:
      this instance for chaining
    • setIv

      public OpenSymmetric setIv(byte[] iv)
      Set initialization vector 设置初始化向量
      Parameters:
      iv - IV bytes
      Returns:
      this instance for chaining
    • encrypt

      public byte[] encrypt(byte[] plaintext)
      Encrypt data 加密数据
      Parameters:
      plaintext - plaintext bytes
      Returns:
      ciphertext bytes
    • encrypt

      public byte[] encrypt(String plaintext)
      Encrypt string (UTF-8) 加密字符串(UTF-8)
      Parameters:
      plaintext - plaintext string
      Returns:
      ciphertext bytes
    • encryptHex

      public String encryptHex(byte[] plaintext)
      Encrypt and return as hex string 加密并返回十六进制字符串
      Parameters:
      plaintext - plaintext bytes
      Returns:
      hex ciphertext
    • encryptHex

      public String encryptHex(String plaintext)
      Encrypt string and return as hex string 加密字符串并返回十六进制字符串
      Parameters:
      plaintext - plaintext string
      Returns:
      hex ciphertext
    • encryptBase64

      public String encryptBase64(byte[] plaintext)
      Encrypt and return as Base64 string 加密并返回 Base64 字符串
      Parameters:
      plaintext - plaintext bytes
      Returns:
      Base64 ciphertext
    • encryptBase64

      public String encryptBase64(String plaintext)
      Encrypt string and return as Base64 string 加密字符串并返回 Base64 字符串
      Parameters:
      plaintext - plaintext string
      Returns:
      Base64 ciphertext
    • decrypt

      public byte[] decrypt(byte[] ciphertext)
      Decrypt data 解密数据
      Parameters:
      ciphertext - ciphertext bytes
      Returns:
      plaintext bytes
    • decryptToString

      public String decryptToString(byte[] ciphertext)
      Decrypt and return as string (UTF-8) 解密并返回字符串(UTF-8)
      Parameters:
      ciphertext - ciphertext bytes
      Returns:
      plaintext string
    • decryptHex

      public byte[] decryptHex(String hexCiphertext)
      Decrypt hex-encoded ciphertext 解密十六进制编码的密文
      Parameters:
      hexCiphertext - hex-encoded ciphertext
      Returns:
      plaintext bytes
    • decryptHexToString

      public String decryptHexToString(String hexCiphertext)
      Decrypt hex-encoded ciphertext to string 解密十六进制编码的密文为字符串
      Parameters:
      hexCiphertext - hex-encoded ciphertext
      Returns:
      plaintext string
    • decryptBase64

      public byte[] decryptBase64(String base64Ciphertext)
      Decrypt Base64-encoded ciphertext 解密 Base64 编码的密文
      Parameters:
      base64Ciphertext - Base64-encoded ciphertext
      Returns:
      plaintext bytes
    • decryptBase64ToString

      public String decryptBase64ToString(String base64Ciphertext)
      Decrypt Base64-encoded ciphertext to string 解密 Base64 编码的密文为字符串
      Parameters:
      base64Ciphertext - Base64-encoded ciphertext
      Returns:
      plaintext string
    • generateIv

      public byte[] generateIv()
      Generate random IV 生成随机 IV
      Returns:
      IV bytes
    • generateKey

      public SecretKey generateKey(int keySize)
      Generate random key 生成随机密钥
      Parameters:
      keySize - key size in bits
      Returns:
      generated secret key
    • getAlgorithm

      public String getAlgorithm()
      Get algorithm name 获取算法名称
      Returns:
      algorithm name
    • getIvLength

      public int getIvLength()
      Get IV length in bytes 获取 IV 长度(字节)
      Returns:
      IV length