依赖包:bcprov-jdk16-1.45.jar
加密key必须16位长度
具体实现如下:
=============================Sm4Util.java==================================
import java.io.UnsupportedEncodingException;
import java.security.Key;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class Sm4Util {
public static final String ALGORITHM_NAME = "SM4";
public static final String ALGORITHM_NAME_CBC_PADDING = "SM4/CBC/PKCS5Padding";
public static void main(String[] args) throws UnsupportedEncodingException {
String chartSet="UTF-8";
String key = "2E7A29B87B1E02C9";
System.out.println("===================SM4加密解密====================");
String str = "测试232所得税78&*&*";
String s = sm4Encode(str,key,chartSet);
System.out.println("加密结果:" + s);
System.out.println("解密结果:" +sm4Decode(s,key,chartSet));
}
//必须有本静态库,否则会报错
static{
try{
Security.addProvider(new BouncyCastleProvider());
}catch(Exception e){
e.printStackTrace();
}
}
/**
* CBC模式加密 ,自动填充
*
* @param key
* @param data 要加密的内容
* @return 加密后内容
*/
public static byte[] encryptCBC(byte[] key, byte[] data) {
try {
Cipher cipher = generateCBCCipher(ALGORITHM_NAME_CBC_PADDING, Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(data);
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* CBC模式解密
*
* @param key
* @param cipherText
* @return 解密后内容
*/
public static byte[] decryptCBC(byte[] key, byte[] cipherText) {
try {
Cipher cipher = generateCBCCipher(ALGORITHM_NAME_CBC_PADDING, Cipher.DECRYPT_MODE, key);
return cipher.doFinal(cipherText);
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static Cipher generateCBCCipher(String algorithmName, int mode, byte[] key) {
try {
Cipher cipher = Cipher.getInstance(algorithmName, BouncyCastleProvider.PROVIDER_NAME);
Key sm4Key = new SecretKeySpec(key, ALGORITHM_NAME);
IvParameterSpec ivParameterSpec = new IvParameterSpec(new byte[16]);
cipher.init(mode, sm4Key, ivParameterSpec);
return cipher;
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static final char HexCharArr[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
'E', 'F' };
public static String byteArrToHex(byte[] content) {
char[] strArr = new char[content.length * 2];
int i = 0;
for (byte bt : content) {
strArr[i++] = HexCharArr[bt >>> 4 & 0xf];
strArr[i++] = HexCharArr[bt & 0xf];
}
return new String(strArr);
}
/**
* 十六进制字符串转字节数组
* @param content 16进制字符串
* @return 字节数组
*/
public static byte[] hexStringToBytes(String hexString) {
if (hexString != null && !"".equals(hexString)) {
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; ++i) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
} else {
return null;
}
}
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
/**
* sm4解密
* @param content 要解密的明文
* @return
* @throws UnsupportedEncodingException
*/
public static String sm4Decode(String content,String key ,String chartSet ) throws UnsupportedEncodingException {
byte[] bytes2 = hexStringToBytes(content);
byte[] deBytes = decryptCBC(key.getBytes(chartSet),bytes2);
return new String(deBytes,chartSet);
}
/**
* sm4加密
* @param content 要加密的明文
* @return
* @throws UnsupportedEncodingException
*/
public static String sm4Encode(String content,String key,String chartSet ) throws UnsupportedEncodingException {
byte[] bytes = encryptCBC(key.getBytes(), content.getBytes(chartSet));
String s = byteArrToHex(bytes).toLowerCase();
return s;
}
}