Class OpenSymmetric
java.lang.Object
cloud.opencode.base.crypto.OpenSymmetric
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 Summary
Modifier and TypeMethodDescriptionstatic OpenSymmetricaesCbc()Create AES-CBC cipher 创建 AES-CBC 加密static OpenSymmetricaesCtr()Create AES-CTR cipher 创建 AES-CTR 加密byte[]decrypt(byte[] ciphertext) Decrypt data 解密数据byte[]decryptBase64(String base64Ciphertext) Decrypt Base64-encoded ciphertext 解密 Base64 编码的密文decryptBase64ToString(String base64Ciphertext) Decrypt Base64-encoded ciphertext to string 解密 Base64 编码的密文为字符串byte[]decryptHex(String hexCiphertext) Decrypt hex-encoded ciphertext 解密十六进制编码的密文decryptHexToString(String hexCiphertext) Decrypt hex-encoded ciphertext to string 解密十六进制编码的密文为字符串decryptToString(byte[] ciphertext) Decrypt and return as string (UTF-8) 解密并返回字符串(UTF-8)byte[]encrypt(byte[] plaintext) Encrypt data 加密数据byte[]Encrypt string (UTF-8) 加密字符串(UTF-8)encryptBase64(byte[] plaintext) Encrypt and return as Base64 string 加密并返回 Base64 字符串encryptBase64(String plaintext) Encrypt string and return as Base64 string 加密字符串并返回 Base64 字符串encryptHex(byte[] plaintext) Encrypt and return as hex string 加密并返回十六进制字符串encryptHex(String plaintext) Encrypt string and return as hex string 加密字符串并返回十六进制字符串byte[]Generate random IV 生成随机 IVgenerateKey(int keySize) Generate random key 生成随机密钥Get algorithm name 获取算法名称intGet IV length in bytes 获取 IV 长度(字节)static OpenSymmetricof(SymmetricAlgorithm algorithm) Create cipher by algorithm enum 根据算法枚举创建加密器setIv(byte[] iv) Set initialization vector 设置初始化向量setKey(byte[] key) Set secret key from bytes 从字节设置密钥Set secret key 设置密钥
-
Method Details
-
aesCbc
Create AES-CBC cipher 创建 AES-CBC 加密- Returns:
- OpenSymmetric instance
-
aesCtr
Create AES-CTR cipher 创建 AES-CTR 加密- Returns:
- OpenSymmetric instance
-
of
Create cipher by algorithm enum 根据算法枚举创建加密器- Parameters:
algorithm- symmetric algorithm- Returns:
- OpenSymmetric instance
-
setKey
Set secret key 设置密钥- Parameters:
key- secret key- Returns:
- this instance for chaining
-
setKey
Set secret key from bytes 从字节设置密钥- Parameters:
key- key bytes- Returns:
- this instance for chaining
-
setIv
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
Encrypt string (UTF-8) 加密字符串(UTF-8)- Parameters:
plaintext- plaintext string- Returns:
- ciphertext bytes
-
encryptHex
Encrypt and return as hex string 加密并返回十六进制字符串- Parameters:
plaintext- plaintext bytes- Returns:
- hex ciphertext
-
encryptHex
-
encryptBase64
Encrypt and return as Base64 string 加密并返回 Base64 字符串- Parameters:
plaintext- plaintext bytes- Returns:
- Base64 ciphertext
-
encryptBase64
-
decrypt
public byte[] decrypt(byte[] ciphertext) Decrypt data 解密数据- Parameters:
ciphertext- ciphertext bytes- Returns:
- plaintext bytes
-
decryptToString
Decrypt and return as string (UTF-8) 解密并返回字符串(UTF-8)- Parameters:
ciphertext- ciphertext bytes- Returns:
- plaintext string
-
decryptHex
Decrypt hex-encoded ciphertext 解密十六进制编码的密文- Parameters:
hexCiphertext- hex-encoded ciphertext- Returns:
- plaintext bytes
-
decryptHexToString
-
decryptBase64
Decrypt Base64-encoded ciphertext 解密 Base64 编码的密文- Parameters:
base64Ciphertext- Base64-encoded ciphertext- Returns:
- plaintext bytes
-
decryptBase64ToString
-
generateIv
public byte[] generateIv()Generate random IV 生成随机 IV- Returns:
- IV bytes
-
generateKey
Generate random key 生成随机密钥- Parameters:
keySize- key size in bits- Returns:
- generated secret key
-
getAlgorithm
-
getIvLength
public int getIvLength()Get IV length in bytes 获取 IV 长度(字节)- Returns:
- IV length
-