1、AES密钥优化

Former-commit-id: e2b6bfe1aa611bca00ba39eecd3d8ad01d4a1cbd
master
wangbing 4 years ago
parent 14e733c2f1
commit 26a41111db

@ -7,6 +7,7 @@ import xyz.wbsite.dbtool.javafx.tool.EntityReader;
import xyz.wbsite.dbtool.javafx.tool.RequestReader;
import xyz.wbsite.dbtool.javafx.tool.ResponseReader;
import xyz.wbsite.dbtool.javafx.tool.Tool;
import xyz.wbsite.dbtool.web.frame.utils.ResourceUtil;
import java.io.File;
import java.util.ArrayList;
@ -98,83 +99,12 @@ public class ApiCallable implements Callable {
System.out.println("生成基础类");
HashMap<String, Object> ctx = new HashMap<String, Object>();
ctx.put("domain", String.join(".", domainList));
{
File file = new File(apiFrame, "ApiEntity.java");
freeMarkerManager.outputTemp(file, "/Java_api/frame/ApiEntity.java", ctx);
}
{
File file = new File(apiFrame, "AESUtil.java");
freeMarkerManager.outputTemp(file, "/Java_api/frame/AESUtil.java", ctx);
}
{
File file = new File(apiFrame, "ApiRequest.java");
freeMarkerManager.outputTemp(file, "/Java_api/frame/ApiRequest.java", ctx);
}
{
File file = new File(apiFrame, "ApiFindRequest.java");
freeMarkerManager.outputTemp(file, "/Java_api/frame/ApiFindRequest.java", ctx);
}
{
File file = new File(apiFrame, "ApiSearchRequest.java");
freeMarkerManager.outputTemp(file, "/Java_api/frame/ApiSearchRequest.java", ctx);
}
{
File file = new File(apiFrame, "ApiResponse.java");
freeMarkerManager.outputTemp(file, "/Java_api/frame/ApiResponse.java", ctx);
}
{
File file = new File(apiFrame, "ApiFindResponse.java");
freeMarkerManager.outputTemp(file, "/Java_api/frame/ApiFindResponse.java", ctx);
}
{
File file = new File(apiFrame, "Base64Util.java");
freeMarkerManager.outputTemp(file, "/Java_api/frame/Base64Util.java", ctx);
}
{
File file = new File(apiFrame, "DownloadUtil.java");
freeMarkerManager.outputTemp(file, "/Java_api/frame/DownloadUtil.java", ctx);
}
{
File file = new File(apiFrame, "Error.java");
freeMarkerManager.outputTemp(file, "/Java_api/frame/Error.java", ctx);
}
{
File file = new File(apiFrame, "ErrorType.java");
freeMarkerManager.outputTemp(file, "/Java_api/frame/ErrorType.java", ctx);
}
{
File file = new File(apiFrame, "MapperUtil.java");
freeMarkerManager.outputTemp(file, "/Java_api/frame/MapperUtil.java", ctx);
}
{
File file = new File(apiFrame, "MD5Util.java");
freeMarkerManager.outputTemp(file, "/Java_api/frame/MD5Util.java", ctx);
}
{
File file = new File(apiFrame, "RSAUtil.java");
freeMarkerManager.outputTemp(file, "/Java_api/frame/RSAUtil.java", ctx);
}
{
File file = new File(apiFrame, "ProgressRequestBody.java");
freeMarkerManager.outputTemp(file, "/Java_api/frame/ProgressRequestBody.java", ctx);
}
{
File file = new File(apiFrame, "SortType.java");
freeMarkerManager.outputTemp(file, "/Java_api/frame/SortType.java", ctx);
}
{
File file = new File(apiFrame, "StringUtils.java");
freeMarkerManager.outputTemp(file, "/Java_api/frame/StringUtils.java", ctx);
for (String name : ResourceUtil.getResourceFiles("/modules/Java_api/frame/")) {
if (name.equals("ValidationUtil.java")){//去掉验证 减少体积
continue;
}
freeMarkerManager.outputTemp(Tool.createFile(apiFrame.getAbsolutePath(), name), "Java_api/frame/" + name, ctx);
}
//4.0.1 去掉验证 减少体积
// {
// File file = new File(frameWork , "ValidationUtil.java");
// freeMarkerManager.outputTemp(file, "/Java_api/frame/ValidationUtil.java", ctx);
// }
}
generateTest();

@ -32,7 +32,7 @@ public class ${className} {
@Before
public void before() {
//实例化API请求客户端
ApiClient.init("http://localhost:8080/api", "app_key", "1234567890123456");
ApiClient.init("http://localhost:8080/api", "app_key", "01234567890123456789012345678901");
apiClient = ApiClient.getInstance();
ApiClient client = ApiClient.getInstance();
client.setDebug(true);

@ -33,7 +33,7 @@ public class AESUtil {
if (secret.length() != 16) {
throw new IllegalArgumentException("secret's length is not 16");
}
SecretKeySpec key = new SecretKeySpec(secret.getBytes(), ALGORITHM);
SecretKeySpec key = new SecretKeySpec(BytesUtil.hex2Bytes(secret), ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM_STR); // 创建密码器
cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
return cipher.doFinal(data);// 加密
@ -75,7 +75,7 @@ public class AESUtil {
if (secret.length() != 16) {
throw new IllegalArgumentException("secret's length is not 16");
}
SecretKeySpec key = new SecretKeySpec(secret.getBytes(), ALGORITHM);
SecretKeySpec key = new SecretKeySpec(BytesUtil.hex2Bytes(secret), ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM_STR);
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(data);

@ -0,0 +1,54 @@
package ${domain}.frame;
/**
* BytesUtil -
*
* @author wangbing
* @version 0.0.1
* @since 2017-01-01
*/
public class BytesUtil {
private static final char[] HEXES = {
'0', '1', '2', '3',
'4', '5', '6', '7',
'8', '9', 'a', 'b',
'c', 'd', 'e', 'f'
};
/**
* byte16
*/
public static String bytes2Hex(byte[] bytes) {
if (bytes == null || bytes.length == 0) {
return null;
}
StringBuilder hex = new StringBuilder();
for (byte b : bytes) {
hex.append(HEXES[(b >> 4) & 0x0F]);
hex.append(HEXES[b & 0x0F]);
}
return hex.toString();
}
/**
* 16byte
*/
public static byte[] hex2Bytes(String hex) {
if (hex == null || hex.length() == 0) {
return null;
}
char[] hexChars = hex.toCharArray();
byte[] bytes = new byte[hexChars.length / 2];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) Integer.parseInt("" + hexChars[i * 2] + hexChars[i * 2 + 1], 16);
}
return bytes;
}
}

@ -335,7 +335,7 @@ public class GlobalController implements ErrorController {
}
String data = null;
String appSecret = "1234567890123456";
String appSecret = "01234567890123456789012345678901";
// 解码
try {
data = AESUtil.decrypt2String(encryptData, appSecret);

@ -3,7 +3,9 @@ package ${basePackage}.frame.utils;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
@ -21,6 +23,22 @@ public class AESUtil {
private static final String ALGORITHM = "AES";
private static final String ALGORITHM_STR = "AES/ECB/PKCS5Padding";
public static String generateDesKey() {
try {
//实例化
KeyGenerator kgen = null;
kgen = KeyGenerator.getInstance("AES");
//设置密钥长度
kgen.init(128);
//生成密钥
SecretKey skey = kgen.generateKey();
//返回密钥的二进制编码
return BytesUtil.bytes2Hex(skey.getEncoded());
} catch (NoSuchAlgorithmException e) {
return "";
}
}
/**
*
*
@ -30,10 +48,10 @@ public class AESUtil {
*/
public static byte[] encrypt(byte[] data, String secret) {
try {
if (secret.length() != 16) {
throw new IllegalArgumentException("secret's length is not 16");
if (secret.length() != 32) {
throw new IllegalArgumentException("Hex secret's length must be 32");
}
SecretKeySpec key = new SecretKeySpec(secret.getBytes(), ALGORITHM);
SecretKeySpec key = new SecretKeySpec(BytesUtil.hex2Bytes(secret), ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM_STR); // 创建密码器
cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
return cipher.doFinal(data);// 加密
@ -72,10 +90,10 @@ public class AESUtil {
*/
public static byte[] decrypt(byte[] data, String secret) {
try {
if (secret.length() != 16) {
throw new IllegalArgumentException("secret's length is not 16");
if (secret.length() != 32) {
throw new IllegalArgumentException("Hex secret's length must be 32");
}
SecretKeySpec key = new SecretKeySpec(secret.getBytes(), ALGORITHM);
SecretKeySpec key = new SecretKeySpec(BytesUtil.hex2Bytes(secret), ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM_STR);
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(data);

@ -0,0 +1,54 @@
package ${basePackage}.frame.utils;
/**
* BytesUtil -
*
* @author wangbing
* @version 0.0.1
* @since 2017-01-01
*/
public class BytesUtil {
private static final char[] HEXES = {
'0', '1', '2', '3',
'4', '5', '6', '7',
'8', '9', 'a', 'b',
'c', 'd', 'e', 'f'
};
/**
* byte16
*/
public static String bytes2Hex(byte[] bytes) {
if (bytes == null || bytes.length == 0) {
return null;
}
StringBuilder hex = new StringBuilder();
for (byte b : bytes) {
hex.append(HEXES[(b >> 4) & 0x0F]);
hex.append(HEXES[b & 0x0F]);
}
return hex.toString();
}
/**
* 16byte
*/
public static byte[] hex2Bytes(String hex) {
if (hex == null || hex.length() == 0) {
return null;
}
char[] hexChars = hex.toCharArray();
byte[] bytes = new byte[hexChars.length / 2];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) Integer.parseInt("" + hexChars[i * 2] + hexChars[i * 2 + 1], 16);
}
return bytes;
}
}

@ -47,7 +47,7 @@ public class MD5Util {
try {
MessageDigest md = MessageDigest.getInstance("md5");
byte[] e = md.digest(value.getBytes());
return toHexString(e);
return BytesUtil.bytes2Hex(e);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return value;
@ -64,28 +64,10 @@ public class MD5Util {
try {
MessageDigest md = MessageDigest.getInstance("md5");
byte[] e = md.digest(bytes);
return toHexString(e);
return BytesUtil.bytes2Hex(e);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return "";
}
}
/**
* @param bytes
* @return Hex
*/
private static String toHexString(byte bytes[]) {
StringBuilder hs = new StringBuilder();
String stmp = "";
for (int n = 0; n < bytes.length; n++) {
stmp = Integer.toHexString(bytes[n] & 0xff);
if (stmp.length() == 1)
hs.append("0").append(stmp);
else
hs.append(stmp);
}
return hs.toString();
}
}

@ -1,7 +1,51 @@
package ${basePackage}.frame.utils;
import java.util.Arrays;
public class StringUtil {
/**
*
*
* @param text
* @param length
* @param c
* @return
*/
public static String padRight(String text, int length, char c) {
if (text == null) {
throw new IllegalArgumentException("text can not be null!");
}
if (text.length() > length) {
throw new IllegalArgumentException("text's length " + text.length() + " is too long!");
}
char[] array = new char[length];
Arrays.fill(array, text.length(), length, c);
System.arraycopy(text.toCharArray(), 0, array, 0, text.length());
return new String(array);
}
/**
*
*
* @param text
* @param length
* @param c
* @return
*/
public static String padLeft(String text, int length, char c) {
if (text == null) {
throw new IllegalArgumentException("text can not be null!");
}
if (text.length() > length) {
throw new IllegalArgumentException("text's length " + text.length() + " is too long!");
}
char[] array = new char[length];
Arrays.fill(array, 0, length - text.length(), c);
System.arraycopy(text.toCharArray(), 0, array, length - text.length(), text.length());
return new String(array);
}
public static int getByteLength(String str) {
int length = str.replaceAll("[^\\x00-\\xff]", "**").length();
return length;

@ -257,7 +257,7 @@
</el-form-item>
<#elseif item.fieldType.javaType() =="String" && item.getFieldLength() lte 50 && item.fieldType != "Dict">
<el-form-item label="${item.fieldComment?default("")}" prop="${item.getFName()}">
<el-input v-model="form.${item.getFName()}" clearable size="mini" placeholder="请输入${item.fieldComment?default("")}"></el-input>
<el-input v-model="form.${item.getFName()}" clearable size="mini" placeholder="请输入${item.fieldComment?default("")}" maxlength="${item.getFieldLength()}"></el-input>
</el-form-item>
<#elseif item.fieldType.javaType() =="String" && item.getFieldLength() gt 50 && item.fieldType != "Dict">
<el-form-item label="${item.fieldComment?default("")}" prop="${item.getFName()}">

Loading…
Cancel
Save

Powered by TurnKey Linux.