Class AesCipher
java.lang.Object
cloud.opencode.base.crypto.symmetric.AesCipher
- All Implemented Interfaces:
SymmetricCipher
AES cipher implementation supporting CBC and CTR modes.
AES 加密实现,支持 CBC 和 CTR 模式。
Features | 主要功能:
- AES-CBC and AES-CTR modes - AES-CBC 和 AES-CTR 模式
- 128/192/256-bit key support - 128/192/256 位密钥支持
- Automatic IV generation and prepending - 自动 IV 生成和前置
- Raw mode for protocol-specific use (fixed IV, no IV prepending) - 协议专用裸模式(固定 IV,不前置 IV)
Usage Examples | 使用示例:
AesCipher cipher = AesCipher.cbc();
cipher.setKey(secretKey);
byte[] encrypted = cipher.encrypt(plaintext);
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:
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic AesCipheraes128()Create AES-128 cipher.static AesCipheraes256()Create AES-256 cipher (recommended).static AesCipher.Builderbuilder()Create a builder for AES cipher.static AesCiphercbc()Create AES cipher in CBC mode.static AesCipherctr()Create AES cipher in CTR mode.byte[]decrypt(byte[] ciphertext) Decrypt ciphertext bytes.byte[]decryptBase64(String base64Ciphertext) Decrypt Base64 encoded ciphertext.byte[]decryptHex(String hexCiphertext) Decrypt hexadecimal encoded ciphertext.byte[]decryptRaw(byte[] ciphertext) Decrypt using the pre-set IV.decryptToString(byte[] ciphertext) Decrypt and convert to string.byte[]encrypt(byte[] plaintext) Encrypt plaintext bytes.byte[]Encrypt plaintext string.encryptBase64(byte[] plaintext) Encrypt and encode as Base64.encryptHex(byte[] plaintext) Encrypt and encode as hexadecimal.byte[]encryptRaw(byte[] plaintext) Encrypt using the pre-set IV without generating a new one and without prepending the IV to the output.byte[]Generate a random initialization vector.generateKey(int keySize) Generate a new secret key.Get the algorithm name.intGet the block size in bytes.byte[]getIv()Get current IV (returns a defensive copy).intGet the IV length in bytes.getKey()Get current key.setIv(byte[] iv) Set the initialization vector.setKey(byte[] key) Set the secret key from bytes.Set the secret key.setMode(CipherMode mode) Set the cipher mode.setPadding(Padding padding) Set the padding scheme.
-
Method Details
-
aes128
Create AES-128 cipher. 创建 AES-128 加密器。- Returns:
- AES-128 cipher instance / AES-128 加密实例
-
aes256
Create AES-256 cipher (recommended). 创建 AES-256 加密器(推荐)。- Returns:
- AES-256 cipher instance / AES-256 加密实例
-
cbc
Create AES cipher in CBC mode. 创建 CBC 模式的 AES 加密器。- Returns:
- AES-CBC cipher instance / AES-CBC 加密实例
-
ctr
Create AES cipher in CTR mode. 创建 CTR 模式的 AES 加密器。- Returns:
- AES-CTR cipher instance / AES-CTR 加密实例
-
builder
Create a builder for AES cipher. 创建 AES 加密器构建器。- Returns:
- builder instance / 构建器实例
-
setKey
Description copied from interface:SymmetricCipherSet the secret key. 设置密钥。- Specified by:
setKeyin interfaceSymmetricCipher- Parameters:
key- secret key / 密钥- Returns:
- this cipher instance / 当前加密实例
-
setKey
Description copied from interface:SymmetricCipherSet the secret key from bytes. 从字节数组设置密钥。- Specified by:
setKeyin interfaceSymmetricCipher- Parameters:
key- key bytes / 密钥字节- Returns:
- this cipher instance / 当前加密实例
-
setIv
Description copied from interface:SymmetricCipherSet the initialization vector. 设置初始化向量。- Specified by:
setIvin interfaceSymmetricCipher- Parameters:
iv- initialization vector / 初始化向量- Returns:
- this cipher instance / 当前加密实例
-
setMode
Description copied from interface:SymmetricCipherSet the cipher mode. 设置加密模式。- Specified by:
setModein interfaceSymmetricCipher- Parameters:
mode- cipher mode / 加密模式- Returns:
- this cipher instance / 当前加密实例
-
setPadding
Description copied from interface:SymmetricCipherSet the padding scheme. 设置填充方案。- Specified by:
setPaddingin interfaceSymmetricCipher- Parameters:
padding- padding scheme / 填充方案- Returns:
- this cipher instance / 当前加密实例
-
encrypt
public byte[] encrypt(byte[] plaintext) Description copied from interface:SymmetricCipherEncrypt plaintext bytes. 加密明文字节。- Specified by:
encryptin interfaceSymmetricCipher- Parameters:
plaintext- plaintext bytes / 明文字节- Returns:
- ciphertext bytes / 密文字节
-
encrypt
Description copied from interface:SymmetricCipherEncrypt plaintext string. 加密明文字符串。- Specified by:
encryptin interfaceSymmetricCipher- Parameters:
plaintext- plaintext string / 明文字符串- Returns:
- ciphertext bytes / 密文字节
-
encryptBase64
Description copied from interface:SymmetricCipherEncrypt and encode as Base64. 加密并编码为 Base64。- Specified by:
encryptBase64in interfaceSymmetricCipher- Parameters:
plaintext- plaintext bytes / 明文字节- Returns:
- Base64 encoded ciphertext / Base64 编码的密文
-
encryptHex
Description copied from interface:SymmetricCipherEncrypt and encode as hexadecimal. 加密并编码为十六进制。- Specified by:
encryptHexin interfaceSymmetricCipher- Parameters:
plaintext- plaintext bytes / 明文字节- Returns:
- hex encoded ciphertext / 十六进制编码的密文
-
encryptRaw
public byte[] encryptRaw(byte[] plaintext) Encrypt using the pre-set IV without generating a new one and without prepending the IV to the output. The caller must have calledsetIv(byte[])first. 使用预设 IV 加密,不生成新 IV,不将 IV 前置到输出。调用方必须先调用setIv(byte[])。This method is intended for protocol-specific scenarios (e.g. WeCom, Feishu) where the IV is derived externally (e.g. from the key) and must not be included in the ciphertext. For general-purpose encryption, prefer
encrypt(byte[])which generates a random IV and prepends it for safe transport.此方法适用于协议特定场景(如企业微信、飞书),其中 IV 由外部派生(如从密钥中取), 且不能包含在密文中。通用加密请使用
encrypt(byte[]),它会生成随机 IV 并前置。- Parameters:
plaintext- plaintext bytes / 明文字节- Returns:
- ciphertext bytes (IV not included) / 密文字节(不含 IV)
- Throws:
OpenCryptoException- if IV is not set or encryption fails / 如果未设置 IV 或加密失败
-
decryptRaw
public byte[] decryptRaw(byte[] ciphertext) Decrypt using the pre-set IV. The input must be pure ciphertext without a prepended IV. The caller must have calledsetIv(byte[])first. 使用预设 IV 解密。输入必须是纯密文,不含前置 IV。调用方必须先调用setIv(byte[])。This is the counterpart of
encryptRaw(byte[])for protocol-specific scenarios where the IV is managed externally.此方法是
encryptRaw(byte[])的对应解密方法,适用于 IV 由外部管理的协议场景。- Parameters:
ciphertext- ciphertext bytes (IV not included) / 密文字节(不含 IV)- Returns:
- plaintext bytes / 明文字节
- Throws:
OpenCryptoException- if IV is not set or decryption fails / 如果未设置 IV 或解密失败
-
decrypt
public byte[] decrypt(byte[] ciphertext) Description copied from interface:SymmetricCipherDecrypt ciphertext bytes. 解密密文字节。- Specified by:
decryptin interfaceSymmetricCipher- Parameters:
ciphertext- ciphertext bytes / 密文字节- Returns:
- plaintext bytes / 明文字节
-
decryptToString
Description copied from interface:SymmetricCipherDecrypt and convert to string. 解密并转换为字符串。- Specified by:
decryptToStringin interfaceSymmetricCipher- Parameters:
ciphertext- ciphertext bytes / 密文字节- Returns:
- plaintext string / 明文字符串
-
decryptBase64
Description copied from interface:SymmetricCipherDecrypt Base64 encoded ciphertext. 解密 Base64 编码的密文。- Specified by:
decryptBase64in interfaceSymmetricCipher- Parameters:
base64Ciphertext- Base64 encoded ciphertext / Base64 编码的密文- Returns:
- plaintext bytes / 明文字节
-
decryptHex
Description copied from interface:SymmetricCipherDecrypt hexadecimal encoded ciphertext. 解密十六进制编码的密文。- Specified by:
decryptHexin interfaceSymmetricCipher- Parameters:
hexCiphertext- hex encoded ciphertext / 十六进制编码的密文- Returns:
- plaintext bytes / 明文字节
-
generateIv
public byte[] generateIv()Description copied from interface:SymmetricCipherGenerate a random initialization vector. 生成随机初始化向量。- Specified by:
generateIvin interfaceSymmetricCipher- Returns:
- IV bytes / 初始化向量字节
-
getBlockSize
public int getBlockSize()Description copied from interface:SymmetricCipherGet the block size in bytes. 获取块大小(字节)。- Specified by:
getBlockSizein interfaceSymmetricCipher- Returns:
- block size / 块大小
-
getAlgorithm
Description copied from interface:SymmetricCipherGet the algorithm name. 获取算法名称。- Specified by:
getAlgorithmin interfaceSymmetricCipher- Returns:
- algorithm name / 算法名称
-
getIvLength
public int getIvLength()Description copied from interface:SymmetricCipherGet the IV length in bytes. 获取 IV 长度(字节)。- Specified by:
getIvLengthin interfaceSymmetricCipher- Returns:
- IV length / IV 长度
-
generateKey
Description copied from interface:SymmetricCipherGenerate a new secret key. 生成新密钥。- Specified by:
generateKeyin interfaceSymmetricCipher- Parameters:
keySize- key size in bits / 密钥大小(比特)- Returns:
- generated secret key / 生成的密钥
-
getIv
public byte[] getIv()Get current IV (returns a defensive copy). 获取当前初始化向量(返回防御性副本)。- Returns:
- IV bytes / 初始化向量字节
-
getKey
-