[+]增加国密SM4接口加解密
This commit is contained in:
@@ -163,6 +163,13 @@
|
|||||||
<artifactId>mockito-core</artifactId>
|
<artifactId>mockito-core</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.bouncycastle</groupId>
|
||||||
|
<artifactId>bcprov-jdk18on</artifactId>
|
||||||
|
<version>1.78.1</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
package com.zt.plat.framework.common.util.security;
|
package com.zt.plat.framework.common.util.security;
|
||||||
|
|
||||||
import cn.hutool.crypto.SecureUtil;
|
import cn.hutool.crypto.SecureUtil;
|
||||||
|
import cn.hutool.crypto.symmetric.SM4;
|
||||||
import com.zt.plat.framework.common.util.json.JsonUtils;
|
import com.zt.plat.framework.common.util.json.JsonUtils;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||||
import javax.crypto.Cipher;
|
import javax.crypto.Cipher;
|
||||||
import javax.crypto.KeyGenerator;
|
import javax.crypto.KeyGenerator;
|
||||||
import javax.crypto.SecretKey;
|
import javax.crypto.SecretKey;
|
||||||
@@ -10,11 +12,13 @@ import javax.crypto.spec.SecretKeySpec;
|
|||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
|
import java.security.Security;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通用的签名、加解密工具类
|
* 通用的签名、加解密工具类
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
public final class CryptoSignatureUtils {
|
public final class CryptoSignatureUtils {
|
||||||
|
|
||||||
public static final String ENCRYPT_TYPE_AES = "AES";
|
public static final String ENCRYPT_TYPE_AES = "AES";
|
||||||
@@ -25,6 +29,15 @@ public final class CryptoSignatureUtils {
|
|||||||
private static final String AES_TRANSFORMATION = "AES/ECB/PKCS5Padding";
|
private static final String AES_TRANSFORMATION = "AES/ECB/PKCS5Padding";
|
||||||
public static final String SIGNATURE_FIELD = "signature";
|
public static final String SIGNATURE_FIELD = "signature";
|
||||||
|
|
||||||
|
private static final String CHARSET = "UTF-8";
|
||||||
|
|
||||||
|
//@Value("${sa.encrypt.sm4.key}")
|
||||||
|
private static String SM4_KEY = "1234567890123456";
|
||||||
|
|
||||||
|
static {
|
||||||
|
Security.addProvider(new BouncyCastleProvider());
|
||||||
|
}
|
||||||
|
|
||||||
private CryptoSignatureUtils() {
|
private CryptoSignatureUtils() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,4 +228,92 @@ public final class CryptoSignatureUtils {
|
|||||||
}
|
}
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//-------------------------------- 国密方式2 -------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加密
|
||||||
|
*/
|
||||||
|
public static String enCode(String data) {
|
||||||
|
try {
|
||||||
|
// 第一步: SM4 加密
|
||||||
|
SM4 sm4 = new SM4(hexToBytes(stringToHex(SM4_KEY)));
|
||||||
|
String encryptHex = sm4.encryptHex(data);
|
||||||
|
|
||||||
|
// 第二步: Base64 编码
|
||||||
|
return new String(Base64.getEncoder().encode(encryptHex.getBytes(CHARSET)), CHARSET);
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("国密加密失败{}",e.getMessage(),e);
|
||||||
|
System.out.println("加密失败"+e);
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解密
|
||||||
|
*/
|
||||||
|
public static String deCode(String data) {
|
||||||
|
try {
|
||||||
|
|
||||||
|
// 第一步: Base64 解码
|
||||||
|
byte[] base64Decode = Base64.getDecoder().decode(data);
|
||||||
|
|
||||||
|
// 第二步: SM4 解密
|
||||||
|
SM4 sm4 = new SM4(hexToBytes(stringToHex(SM4_KEY)));
|
||||||
|
return sm4.decryptStr(new String(base64Decode));
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("国密解密失败{}",e.getMessage(),e);
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static String stringToHex(String input) {
|
||||||
|
char[] chars = input.toCharArray();
|
||||||
|
StringBuilder hex = new StringBuilder();
|
||||||
|
for (char c : chars) {
|
||||||
|
hex.append(Integer.toHexString((int) c));
|
||||||
|
}
|
||||||
|
return hex.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 16 进制串转字节数组
|
||||||
|
*
|
||||||
|
* @param hex 16进制字符串
|
||||||
|
* @return byte数组
|
||||||
|
*/
|
||||||
|
public static byte[] hexToBytes(String hex) {
|
||||||
|
int length = hex.length();
|
||||||
|
byte[] result;
|
||||||
|
if (length % 2 == 1) {
|
||||||
|
length++;
|
||||||
|
result = new byte[(length / 2)];
|
||||||
|
hex = "0" + hex;
|
||||||
|
} else {
|
||||||
|
result = new byte[(length / 2)];
|
||||||
|
}
|
||||||
|
int j = 0;
|
||||||
|
for (int i = 0; i < length; i += 2) {
|
||||||
|
result[j] = hexToByte(hex.substring(i, i + 2));
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 16 进制字符转字节
|
||||||
|
*
|
||||||
|
* @param hex 16进制字符 0x00到0xFF
|
||||||
|
* @return byte
|
||||||
|
*/
|
||||||
|
private static byte hexToByte(String hex) {
|
||||||
|
return (byte) Integer.parseInt(hex, 16);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user