java完整的SM4Utils工具类代码
时间: 2024-05-17 22:16:56 浏览: 13
以下是一个Java完整的SM4Utils工具类代码,可以用于加密和解密数据:
```java
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.Security;
public class SM4Utils {
static {
Security.addProvider(new BouncyCastleProvider());
}
private static final String ALGORITHM_NAME = "SM4";
private static final String ALGORITHM_NAME_ECB_PADDING = "SM4/ECB/PKCS7Padding";
private static final String ALGORITHM_NAME_CBC_PADDING = "SM4/CBC/PKCS7Padding";
private static final int DEFAULT_KEY_SIZE = 128;
/**
* ECB加密
*
* @param data 待加密数据
* @param secretKey 密钥
* @return 加密后的数据
* @throws Exception 加密异常
*/
public static String encryptECB(String data, String secretKey) throws Exception {
byte[] keyBytes = secretKey.getBytes(StandardCharsets.UTF_8);
byte[] dataBytes = data.getBytes(StandardCharsets.UTF_8);
// 加密模式
Cipher cipher = Cipher.getInstance(ALGORITHM_NAME_ECB_PADDING);
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyBytes, ALGORITHM_NAME));
byte[] encrypted = cipher.doFinal(dataBytes);
return byte2Hex(encrypted);
}
/**
* ECB解密
*
* @param encryptedData 待解密数据
* @param secretKey 密钥
* @return 解密后的数据
* @throws Exception 解密异常
*/
public static String decryptECB(String encryptedData, String secretKey) throws Exception {
byte[] keyBytes = secretKey.getBytes(StandardCharsets.UTF_8);
byte[] encryptedBytes = hex2Byte(encryptedData);
// 解密模式
Cipher cipher = Cipher.getInstance(ALGORITHM_NAME_ECB_PADDING);
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyBytes, ALGORITHM_NAME));
byte[] decrypted = cipher.doFinal(encryptedBytes);
return new String(decrypted, StandardCharsets.UTF_8);
}
/**
* CBC加密
*
* @param data 待加密数据
* @param secretKey 密钥
* @param iv 初始化向量
* @return 加密后的数据
* @throws Exception 加密异常
*/
public static String encryptCBC(String data, String secretKey, String iv) throws Exception {
byte[] keyBytes = secretKey.getBytes(StandardCharsets.UTF_8);
byte[] dataBytes = data.getBytes(StandardCharsets.UTF_8);
byte[] ivBytes = iv.getBytes(StandardCharsets.UTF_8);
// 加密模式
Cipher cipher = Cipher.getInstance(ALGORITHM_NAME_CBC_PADDING);
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyBytes, ALGORITHM_NAME), new IvParameterSpec(ivBytes));
byte[] encrypted = cipher.doFinal(dataBytes);
return byte2Hex(encrypted);
}
/**
* CBC解密
*
* @param encryptedData 待解密数据
* @param secretKey 密钥
* @param iv 初始化向量
* @return 解密后的数据
* @throws Exception 解密异常
*/
public static String decryptCBC(String encryptedData, String secretKey, String iv) throws Exception {
byte[] keyBytes = secretKey.getBytes(StandardCharsets.UTF_8);
byte[] encryptedBytes = hex2Byte(encryptedData);
byte[] ivBytes = iv.getBytes(StandardCharsets.UTF_8);
// 解密模式
Cipher cipher = Cipher.getInstance(ALGORITHM_NAME_CBC_PADDING);
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyBytes, ALGORITHM_NAME), new IvParameterSpec(ivBytes));
byte[] decrypted = cipher.doFinal(encryptedBytes);
return new String(decrypted, StandardCharsets.UTF_8);
}
/**
* 字节转十六进制字符串
*
* @param bytes 字节数组
* @return 十六进制字符串
*/
private static String byte2Hex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(String.format("%02X", b));
}
return sb.toString();
}
/**
* 十六进制字符串转字节数组
*
* @param hex 十六进制字符串
* @return 字节数组
*/
private static byte[] hex2Byte(String hex) {
int len = hex.length() / 2;
byte[] result = new byte[len];
for (int i = 0; i < len; i++) {
result[i] = (byte) Integer.parseInt(hex.substring(i * 2, i * 2 + 2), 16);
}
return result;
}
}
```
这个工具类使用了Bouncy Castle密码库来实现SM4算法的加密和解密功能。在使用前,需要先将Bouncy Castle密码库添加到Java加密环境中,这里使用了static块来进行静态初始化。
其中,ECB模式和CBC模式各提供了加密和解密的方法,需要传入待加密/解密的数据、密钥和初始化向量(CBC模式需要)。在加密过程中,需要使用Cipher类来进行加密/解密操作,而密钥和初始化向量则需要使用SecretKeySpec和IvParameterSpec类来进行封装。最后,将加密/解密后的结果转换成十六进制字符串进行返回即可。
另外,这个工具类还提供了将字节数组转换成十六进制字符串和将十六进制字符串转换成字节数组的两个辅助方法,用于在加密/解密过程中进行数据的转换。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)