mirror of
https://gitee.com/lab1024/smart-admin.git
synced 2025-11-16 07:33:49 +08:00
v3.7.0 (2024-09-28) Java17+SpringBoot3重磅更新,【新增】支持Java17;【新增】支持SpringBoot3;【优化】优化AES和SM4加密;【优化】优化三级等保文档;
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
package net.lab1024.sa.base.module.support.apiencrypt.service;
|
||||
|
||||
import cn.hutool.crypto.symmetric.AES;
|
||||
import cn.hutool.crypto.symmetric.SM4;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.lab1024.sa.base.common.constant.StringConst;
|
||||
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.security.Security;
|
||||
@@ -12,6 +14,10 @@ import java.util.Base64;
|
||||
|
||||
/**
|
||||
* AES 加密和解密
|
||||
* 1、AES加密算法支持三种密钥长度:128位、192位和256位,这里选择128位
|
||||
* 2、AES 要求秘钥为 128bit,转化字节为 16个字节;
|
||||
* 3、js前端使用 UCS-2 或者 UTF-16 编码,字母、数字、特殊符号等 占用1个字节;
|
||||
* 4、所以:秘钥Key 组成为:字母、数字、特殊符号 一共16个即可
|
||||
*
|
||||
* @Author 1024创新实验室-主任:卓大
|
||||
* @Date 2023/10/21 11:41:46
|
||||
@@ -20,14 +26,12 @@ import java.util.Base64;
|
||||
* @Copyright <a href="https://1024lab.net">1024创新实验室</a>
|
||||
*/
|
||||
|
||||
|
||||
@Slf4j
|
||||
//@Service
|
||||
public class ApiEncryptServiceAesImpl implements ApiEncryptService {
|
||||
|
||||
private static final String CHARSET = "UTF-8";
|
||||
|
||||
private static final String AES_KEY = "1024abcd1024abcd1024abcd1024abcd";
|
||||
private static final String AES_KEY = "1024lab__1024lab";
|
||||
|
||||
static {
|
||||
Security.addProvider(new BouncyCastleProvider());
|
||||
@@ -36,9 +40,8 @@ public class ApiEncryptServiceAesImpl implements ApiEncryptService {
|
||||
@Override
|
||||
public String encrypt(String data) {
|
||||
try {
|
||||
|
||||
// AES 加密 并转为 base64
|
||||
AES aes = new AES(AES_KEY.getBytes(CHARSET));
|
||||
AES aes = new AES(hexToBytes(stringToHex(AES_KEY)));
|
||||
return aes.encryptBase64(data);
|
||||
|
||||
|
||||
@@ -55,7 +58,7 @@ public class ApiEncryptServiceAesImpl implements ApiEncryptService {
|
||||
byte[] base64Decode = Base64.getDecoder().decode(data);
|
||||
|
||||
// 第二步: AES 解密
|
||||
AES aes = new AES(AES_KEY.getBytes(CHARSET));
|
||||
AES aes = new AES(hexToBytes(stringToHex(AES_KEY)));
|
||||
byte[] decryptedBytes = aes.decrypt(base64Decode);
|
||||
return new String(decryptedBytes, CHARSET);
|
||||
|
||||
@@ -65,11 +68,47 @@ public class ApiEncryptServiceAesImpl implements ApiEncryptService {
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws UnsupportedEncodingException {
|
||||
String s = "{\"age\":100,\"name\":\"卓大\"}";
|
||||
String jsonString = JSON.toJSONString(s);
|
||||
AES aes = new AES(AES_KEY.getBytes(CHARSET));
|
||||
System.out.println(new String(aes.encryptBase64(jsonString)));
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
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进制字符 0x00到0xFF
|
||||
* @return byte
|
||||
*/
|
||||
private static byte hexToByte(String hex) {
|
||||
return (byte) Integer.parseInt(hex, 16);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -11,6 +11,10 @@ import java.util.Base64;
|
||||
|
||||
/**
|
||||
* 国产 SM4 加密 和 解密
|
||||
* 1、国密SM4 要求秘钥为 128bit,转化字节为 16个字节;
|
||||
* 2、js前端使用 UCS-2 或者 UTF-16 编码,字母、数字、特殊符号等 占用1个字节;
|
||||
* 3、java中 每个 字母数字 也是占用1个字节;
|
||||
* 4、所以:前端和后端的 秘钥Key 组成为:字母、数字、特殊符号 一共16个即可
|
||||
*
|
||||
* @Author 1024创新实验室-主任:卓大
|
||||
* @Date 2023/10/21 11:41:46
|
||||
@@ -24,7 +28,7 @@ import java.util.Base64;
|
||||
public class ApiEncryptServiceSmImpl implements ApiEncryptService {
|
||||
|
||||
private static final String CHARSET = "UTF-8";
|
||||
private static final String SM4_KEY = "1024abcd1024abcd1024abcd1024abcd";
|
||||
private static final String SM4_KEY = "1024lab__1024lab";
|
||||
|
||||
static {
|
||||
Security.addProvider(new BouncyCastleProvider());
|
||||
@@ -36,7 +40,7 @@ public class ApiEncryptServiceSmImpl implements ApiEncryptService {
|
||||
try {
|
||||
|
||||
// 第一步: SM4 加密
|
||||
SM4 sm4 = new SM4(hexToBytes(SM4_KEY));
|
||||
SM4 sm4 = new SM4(hexToBytes(stringToHex(SM4_KEY)));
|
||||
String encryptHex = sm4.encryptHex(data);
|
||||
|
||||
// 第二步: Base64 编码
|
||||
@@ -57,7 +61,7 @@ public class ApiEncryptServiceSmImpl implements ApiEncryptService {
|
||||
byte[] base64Decode = Base64.getDecoder().decode(data);
|
||||
|
||||
// 第二步: SM4 解密
|
||||
SM4 sm4 = new SM4(hexToBytes(SM4_KEY));
|
||||
SM4 sm4 = new SM4(hexToBytes(stringToHex(SM4_KEY)));
|
||||
return sm4.decryptStr(new String(base64Decode));
|
||||
|
||||
} catch (Exception e) {
|
||||
@@ -67,6 +71,16 @@ public class ApiEncryptServiceSmImpl implements ApiEncryptService {
|
||||
}
|
||||
|
||||
|
||||
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 进制串转字节数组
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user