mirror of
https://gitee.com/lab1024/smart-admin.git
synced 2025-11-12 05:33:48 +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 进制串转字节数组
|
||||
*
|
||||
|
||||
@@ -42,7 +42,7 @@
|
||||
#end
|
||||
#end
|
||||
<a-form-item class="smart-query-form-item">
|
||||
<a-button type="primary" @click="queryData">
|
||||
<a-button type="primary" @click="onSearch">
|
||||
<template #icon>
|
||||
<SearchOutlined />
|
||||
</template>
|
||||
@@ -219,6 +219,12 @@
|
||||
queryData();
|
||||
}
|
||||
|
||||
// 搜索
|
||||
function onSearch(){
|
||||
queryForm.pageNum = 1;
|
||||
queryData();
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
async function queryData() {
|
||||
tableLoading.value = true;
|
||||
|
||||
@@ -39,7 +39,7 @@ spring:
|
||||
host: smtp.163.com
|
||||
port: 465
|
||||
username: lab1024@163.com
|
||||
password: ROIASDFJKHQFTA
|
||||
password: 1
|
||||
properties:
|
||||
mail:
|
||||
smtp:
|
||||
|
||||
@@ -39,7 +39,7 @@ spring:
|
||||
host: smtp.163.com
|
||||
port: 465
|
||||
username: lab1024@163.com
|
||||
password: ROIASDFJKHQFTA
|
||||
password: 1
|
||||
properties:
|
||||
mail:
|
||||
smtp:
|
||||
@@ -104,7 +104,7 @@ springdoc:
|
||||
knife4j:
|
||||
enable: true
|
||||
basic:
|
||||
enable: true
|
||||
enable: false
|
||||
username: api # Basic认证用户名
|
||||
password: 1024 # Basic认证密码
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
spring:
|
||||
# 数据库连接信息
|
||||
datasource:
|
||||
url: jdbc:mysql://127.0.0.1:3306/smart_admin_v3_dev?autoReconnect=true&useServerPreparedStmts=false&rewriteBatchedStatements=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai
|
||||
url: jdbc:mysql://127.0.0.1:3306/smart_admin_v3?autoReconnect=true&useServerPreparedStmts=false&rewriteBatchedStatements=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai
|
||||
username: root
|
||||
password: SmartAdmin666
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
@@ -38,7 +38,7 @@ spring:
|
||||
host: smtp.163.com
|
||||
port: 465
|
||||
username: lab1024@163.com
|
||||
password: ROIASDFJKHQFTA
|
||||
password: 1024lab
|
||||
properties:
|
||||
mail:
|
||||
smtp:
|
||||
|
||||
@@ -39,7 +39,7 @@ spring:
|
||||
host: smtp.163.com
|
||||
port: 465
|
||||
username: lab1024@163.com
|
||||
password: ROIASDFJKHQFTA
|
||||
password: 1
|
||||
properties:
|
||||
mail:
|
||||
smtp:
|
||||
|
||||
Reference in New Issue
Block a user