|
|
@@ -6,6 +6,8 @@ import javax.crypto.spec.SecretKeySpec;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.util.Base64;
|
|
|
|
|
|
+import static com.zsElectric.boot.common.constant.ConnectivityConstants.*;
|
|
|
+
|
|
|
/**
|
|
|
* AES加解密工具类
|
|
|
* 支持AES-128-CBC-PKCS5Padding对称加解密算法
|
|
|
@@ -135,66 +137,70 @@ public class AESCryptoUtil {
|
|
|
/**
|
|
|
* 测试方法
|
|
|
*/
|
|
|
- public static void main(String[] args) {
|
|
|
- try {
|
|
|
- // 测试数据
|
|
|
- String originalData = "{\n" +
|
|
|
- " \"total\": 1,\n" +
|
|
|
- " \"stationStatusInfo\": {\n" +
|
|
|
- " \"operationlD\": \"123456789\",\n" +
|
|
|
- " \"stationlD\": \"111111111111111\",\n" +
|
|
|
- " \"connectorStatusInfos\":{\"connectorD\":1,\"equipmentD\":\"10000000000000000000001\",\n" +
|
|
|
- " \"status\":4,\n" +
|
|
|
- " \"curentA\":0,\n" +
|
|
|
- " \"currentB\":0,\n" +
|
|
|
- " \"curentC\":0,\n" +
|
|
|
- " \"voltageA\":0,\n" +
|
|
|
- " \"voltageB\":0,\n" +
|
|
|
- " \"voltageC\":0,\n" +
|
|
|
- " \"soc\":10\n" +
|
|
|
- " }\n" +
|
|
|
- " }\n" +
|
|
|
- "}";
|
|
|
- String key = "1234567890abcdef"; // 16字节密钥
|
|
|
- String iv = "1234567890abcdef"; // 16字节初始化向量
|
|
|
-
|
|
|
- System.out.println("=== AES-128-CBC-PKCS5Padding 加解密测试 ===");
|
|
|
- System.out.println("原始数据: " + originalData);
|
|
|
- System.out.println("密钥: " + key);
|
|
|
- System.out.println("初始化向量: " + iv);
|
|
|
-
|
|
|
- // 加密
|
|
|
- long startTime = System.currentTimeMillis();
|
|
|
- String encryptedData = encrypt(originalData, key, iv);
|
|
|
- long encryptTime = System.currentTimeMillis() - startTime;
|
|
|
- System.out.println("加密结果: " + encryptedData);
|
|
|
- System.out.println("加密耗时: " + encryptTime + "ms");
|
|
|
-
|
|
|
- // 解密
|
|
|
- startTime = System.currentTimeMillis();
|
|
|
- String decryptedData = decrypt(encryptedData, key, iv);
|
|
|
- long decryptTime = System.currentTimeMillis() - startTime;
|
|
|
- System.out.println("解密结果: " + decryptedData);
|
|
|
- System.out.println("解密耗时: " + decryptTime + "ms");
|
|
|
-
|
|
|
- // 验证加解密一致性
|
|
|
- boolean isSuccess = originalData.equals(decryptedData);
|
|
|
- System.out.println("加解密验证: " + (isSuccess ? "成功" : "失败"));
|
|
|
-
|
|
|
- // 测试随机密钥生成
|
|
|
- System.out.println("\n=== 随机密钥生成测试 ===");
|
|
|
- String randomKey = generateRandomKey();
|
|
|
- String randomIV = generateRandomIV();
|
|
|
- System.out.println("随机密钥: " + randomKey);
|
|
|
- System.out.println("随机IV: " + randomIV);
|
|
|
-
|
|
|
- // 使用随机密钥进行加解密测试
|
|
|
- String testEncrypted = encrypt("测试数据", randomKey, randomIV);
|
|
|
- String testDecrypted = decrypt(testEncrypted, randomKey, randomIV);
|
|
|
- System.out.println("随机密钥加解密测试: " + ("测试数据".equals(testDecrypted) ? "成功" : "失败"));
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
+ String decryptedData = decrypt("B3eJFJNOlN7ubFMoxqAxVg==",
|
|
|
+ DATA_SECRET,
|
|
|
+ DATA_SECRET_IV);
|
|
|
+ System.out.println("解密结果: " + decryptedData);
|
|
|
+// try {
|
|
|
+// // 测试数据
|
|
|
+// String originalData = "{\n" +
|
|
|
+// " \"total\": 1,\n" +
|
|
|
+// " \"stationStatusInfo\": {\n" +
|
|
|
+// " \"operationlD\": \"123456789\",\n" +
|
|
|
+// " \"stationlD\": \"111111111111111\",\n" +
|
|
|
+// " \"connectorStatusInfos\":{\"connectorD\":1,\"equipmentD\":\"10000000000000000000001\",\n" +
|
|
|
+// " \"status\":4,\n" +
|
|
|
+// " \"curentA\":0,\n" +
|
|
|
+// " \"currentB\":0,\n" +
|
|
|
+// " \"curentC\":0,\n" +
|
|
|
+// " \"voltageA\":0,\n" +
|
|
|
+// " \"voltageB\":0,\n" +
|
|
|
+// " \"voltageC\":0,\n" +
|
|
|
+// " \"soc\":10\n" +
|
|
|
+// " }\n" +
|
|
|
+// " }\n" +
|
|
|
+// "}";
|
|
|
+// String key = "1234567890abcdef"; // 16字节密钥
|
|
|
+// String iv = "1234567890abcdef"; // 16字节初始化向量
|
|
|
+//
|
|
|
+// System.out.println("=== AES-128-CBC-PKCS5Padding 加解密测试 ===");
|
|
|
+// System.out.println("原始数据: " + originalData);
|
|
|
+// System.out.println("密钥: " + key);
|
|
|
+// System.out.println("初始化向量: " + iv);
|
|
|
+//
|
|
|
+// // 加密
|
|
|
+// long startTime = System.currentTimeMillis();
|
|
|
+// String encryptedData = encrypt(originalData, key, iv);
|
|
|
+// long encryptTime = System.currentTimeMillis() - startTime;
|
|
|
+// System.out.println("加密结果: " + encryptedData);
|
|
|
+// System.out.println("加密耗时: " + encryptTime + "ms");
|
|
|
+//
|
|
|
+// // 解密
|
|
|
+// startTime = System.currentTimeMillis();
|
|
|
+// String decryptedData = decrypt(encryptedData, key, iv);
|
|
|
+// long decryptTime = System.currentTimeMillis() - startTime;
|
|
|
+// System.out.println("解密结果: " + decryptedData);
|
|
|
+// System.out.println("解密耗时: " + decryptTime + "ms");
|
|
|
+//
|
|
|
+// // 验证加解密一致性
|
|
|
+// boolean isSuccess = originalData.equals(decryptedData);
|
|
|
+// System.out.println("加解密验证: " + (isSuccess ? "成功" : "失败"));
|
|
|
+//
|
|
|
+// // 测试随机密钥生成
|
|
|
+// System.out.println("\n=== 随机密钥生成测试 ===");
|
|
|
+// String randomKey = generateRandomKey();
|
|
|
+// String randomIV = generateRandomIV();
|
|
|
+// System.out.println("随机密钥: " + randomKey);
|
|
|
+// System.out.println("随机IV: " + randomIV);
|
|
|
+//
|
|
|
+// // 使用随机密钥进行加解密测试
|
|
|
+// String testEncrypted = encrypt("测试数据", randomKey, randomIV);
|
|
|
+// String testDecrypted = decrypt(testEncrypted, randomKey, randomIV);
|
|
|
+// System.out.println("随机密钥加解密测试: " + ("测试数据".equals(testDecrypted) ? "成功" : "失败"));
|
|
|
+//
|
|
|
+// } catch (Exception e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
}
|
|
|
}
|