Class Sm4Cipher
java.lang.Object
cloud.opencode.base.crypto.symmetric.Sm4Cipher
- All Implemented Interfaces:
AeadCipher, SymmetricCipher
SM4 cipher implementation (Chinese national cryptographic standard).
SM4 加密实现(中国国家密码标准)。
Requires Bouncy Castle provider for SM4 support. 需要 Bouncy Castle 提供商支持 SM4。
Features | 主要功能:
- SM4-GCM and SM4-CBC modes - SM4-GCM 和 SM4-CBC 模式
- Chinese national standard (GB/T 32907-2016) - 中国国密标准(GB/T 32907-2016)
- Requires Bouncy Castle provider - 需要 Bouncy Castle 提供者
Usage Examples | 使用示例:
AeadCipher sm4 = Sm4Cipher.gcm();
byte[] encrypted = sm4.encrypt(plaintext, key);
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:
-
Method Summary
Modifier and TypeMethodDescriptionstatic Sm4Ciphercbc()Create SM4 cipher in CBC mode.byte[]decrypt(byte[] ciphertext) Decrypt ciphertext bytes.byte[]decryptBase64(String base64Ciphertext) Decrypt Base64 encoded ciphertext.decryptBase64ToString(String base64Ciphertext) Decrypt Base64 encoded ciphertext to string.voiddecryptFile(Path source, Path target) Decrypt a file.byte[]decryptHex(String hexCiphertext) Decrypt hexadecimal encoded ciphertext.decryptStream(InputStream input) Create a decrypting input stream.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.encryptBase64(String plaintext) Encrypt string and encode as Base64.voidencryptFile(Path source, Path target) Encrypt a file.encryptHex(byte[] plaintext) Encrypt and encode as hexadecimal.encryptStream(OutputStream output) Create an encrypting output stream.static Sm4Ciphergcm()Create SM4 cipher in GCM mode (AEAD).byte[]Generate a random initialization vector.generateKey(int keySize) Generate a new secret key.byte[]Generate a random nonce.Get the algorithm name.intGet the block size in bytes.byte[]getIv()Get current IV.intGet the IV length in bytes.getKey()Get current key.static booleanCheck if Bouncy Castle provider is available.setAad(byte[] aad) Set additional authenticated data (AAD).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.setNonce(byte[] nonce) Set the nonce (same as IV for most AEAD ciphers).setPadding(Padding padding) Set the padding scheme.setTagLength(int tagBits) Set authentication tag length in bits.
-
Method Details
-
cbc
Create SM4 cipher in CBC mode. 创建 CBC 模式的 SM4 加密器。- Returns:
- SM4-CBC cipher instance / SM4-CBC 加密实例
-
gcm
Create SM4 cipher in GCM mode (AEAD). 创建 GCM 模式的 SM4 加密器(AEAD)。- Returns:
- SM4-GCM cipher instance / SM4-GCM 加密实例
-
isBouncyCastleAvailable
public static boolean isBouncyCastleAvailable()Check if Bouncy Castle provider is available. 检查 Bouncy Castle 提供商是否可用。- Returns:
- true if available / 如果可用则返回 true
-
setKey
Description copied from interface:SymmetricCipherSet the secret key. 设置密钥。- Specified by:
setKeyin interfaceAeadCipher- 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 interfaceAeadCipher- Specified by:
setKeyin interfaceSymmetricCipher- Parameters:
key- key bytes / 密钥字节- Returns:
- this cipher instance / 当前加密实例
-
setIv
Description copied from interface:SymmetricCipherSet the initialization vector. 设置初始化向量。- Specified by:
setIvin interfaceAeadCipher- Specified by:
setIvin interfaceSymmetricCipher- Parameters:
iv- initialization vector / 初始化向量- Returns:
- this cipher instance / 当前加密实例
-
setNonce
Description copied from interface:AeadCipherSet the nonce (same as IV for most AEAD ciphers). 设置随机数(对于大多数 AEAD 密码与 IV 相同)。- Specified by:
setNoncein interfaceAeadCipher- Parameters:
nonce- nonce bytes / 随机数字节- Returns:
- this cipher instance / 当前加密实例
-
setAad
Description copied from interface:AeadCipherSet additional authenticated data (AAD). 设置附加认证数据(AAD)。- Specified by:
setAadin interfaceAeadCipher- Parameters:
aad- additional authenticated data / 附加认证数据- 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 / 当前加密实例
-
setTagLength
Description copied from interface:AeadCipherSet authentication tag length in bits. 设置认证标签长度(比特)。- Specified by:
setTagLengthin interfaceAeadCipher- Parameters:
tagBits- tag length in bits / 标签长度(比特)- Returns:
- this cipher instance / 当前加密实例
-
encrypt
public byte[] encrypt(byte[] plaintext) Description copied from interface:SymmetricCipherEncrypt plaintext bytes. 加密明文字节。- Specified by:
encryptin interfaceAeadCipher- Specified by:
encryptin interfaceSymmetricCipher- Parameters:
plaintext- plaintext bytes / 明文字节- Returns:
- ciphertext bytes / 密文字节
-
encrypt
Description copied from interface:SymmetricCipherEncrypt plaintext string. 加密明文字符串。- Specified by:
encryptin interfaceAeadCipher- 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 interfaceAeadCipher- Specified by:
encryptBase64in interfaceSymmetricCipher- Parameters:
plaintext- plaintext bytes / 明文字节- Returns:
- Base64 encoded ciphertext / Base64 编码的密文
-
encryptBase64
Description copied from interface:AeadCipherEncrypt string and encode as Base64. 加密字符串并编码为 Base64。- Specified by:
encryptBase64in interfaceAeadCipher- Parameters:
plaintext- plaintext string / 明文字符串- Returns:
- Base64 encoded ciphertext / Base64 编码的密文
-
encryptHex
Description copied from interface:SymmetricCipherEncrypt and encode as hexadecimal. 加密并编码为十六进制。- Specified by:
encryptHexin interfaceAeadCipher- Specified by:
encryptHexin interfaceSymmetricCipher- Parameters:
plaintext- plaintext bytes / 明文字节- Returns:
- hex encoded ciphertext / 十六进制编码的密文
-
encryptFile
Description copied from interface:AeadCipherEncrypt a file. 加密文件。- Specified by:
encryptFilein interfaceAeadCipher- Parameters:
source- source file path / 源文件路径target- target file path / 目标文件路径
-
encryptStream
Description copied from interface:AeadCipherCreate an encrypting output stream. 创建加密输出流。- Specified by:
encryptStreamin interfaceAeadCipher- Parameters:
output- underlying output stream / 底层输出流- Returns:
- encrypting output stream / 加密输出流
-
decrypt
public byte[] decrypt(byte[] ciphertext) Description copied from interface:SymmetricCipherDecrypt ciphertext bytes. 解密密文字节。- Specified by:
decryptin interfaceAeadCipher- Specified by:
decryptin interfaceSymmetricCipher- Parameters:
ciphertext- ciphertext bytes / 密文字节- Returns:
- plaintext bytes / 明文字节
-
decryptToString
Description copied from interface:SymmetricCipherDecrypt and convert to string. 解密并转换为字符串。- Specified by:
decryptToStringin interfaceAeadCipher- Specified by:
decryptToStringin interfaceSymmetricCipher- Parameters:
ciphertext- ciphertext bytes / 密文字节- Returns:
- plaintext string / 明文字符串
-
decryptBase64
Description copied from interface:SymmetricCipherDecrypt Base64 encoded ciphertext. 解密 Base64 编码的密文。- Specified by:
decryptBase64in interfaceAeadCipher- Specified by:
decryptBase64in interfaceSymmetricCipher- Parameters:
base64Ciphertext- Base64 encoded ciphertext / Base64 编码的密文- Returns:
- plaintext bytes / 明文字节
-
decryptBase64ToString
Description copied from interface:AeadCipherDecrypt Base64 encoded ciphertext to string. 解密 Base64 编码的密文为字符串。- Specified by:
decryptBase64ToStringin interfaceAeadCipher- Parameters:
base64Ciphertext- Base64 encoded ciphertext / Base64 编码的密文- Returns:
- plaintext string / 明文字符串
-
decryptHex
Description copied from interface:SymmetricCipherDecrypt hexadecimal encoded ciphertext. 解密十六进制编码的密文。- Specified by:
decryptHexin interfaceAeadCipher- Specified by:
decryptHexin interfaceSymmetricCipher- Parameters:
hexCiphertext- hex encoded ciphertext / 十六进制编码的密文- Returns:
- plaintext bytes / 明文字节
-
decryptFile
Description copied from interface:AeadCipherDecrypt a file. 解密文件。- Specified by:
decryptFilein interfaceAeadCipher- Parameters:
source- source file path / 源文件路径target- target file path / 目标文件路径
-
decryptStream
Description copied from interface:AeadCipherCreate a decrypting input stream. 创建解密输入流。- Specified by:
decryptStreamin interfaceAeadCipher- Parameters:
input- underlying input stream / 底层输入流- Returns:
- decrypting input stream / 解密输入流
-
generateIv
public byte[] generateIv()Description copied from interface:SymmetricCipherGenerate a random initialization vector. 生成随机初始化向量。- Specified by:
generateIvin interfaceAeadCipher- Specified by:
generateIvin interfaceSymmetricCipher- Returns:
- IV bytes / 初始化向量字节
-
generateNonce
public byte[] generateNonce()Description copied from interface:AeadCipherGenerate a random nonce. 生成随机随机数。- Specified by:
generateNoncein interfaceAeadCipher- Returns:
- nonce bytes / 随机数字节
-
getBlockSize
public int getBlockSize()Description copied from interface:SymmetricCipherGet the block size in bytes. 获取块大小(字节)。- Specified by:
getBlockSizein interfaceSymmetricCipher- Returns:
- block size / 块大小
-
getIvLength
public int getIvLength()Description copied from interface:SymmetricCipherGet the IV length in bytes. 获取 IV 长度(字节)。- Specified by:
getIvLengthin interfaceAeadCipher- Specified by:
getIvLengthin interfaceSymmetricCipher- Returns:
- IV length / IV 长度
-
getAlgorithm
Description copied from interface:SymmetricCipherGet the algorithm name. 获取算法名称。- Specified by:
getAlgorithmin interfaceAeadCipher- Specified by:
getAlgorithmin interfaceSymmetricCipher- Returns:
- algorithm name / 算法名称
-
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:
- IV bytes / 初始化向量字节
-
getKey
-