|
@@ -3,37 +3,18 @@ package com.abi.qms.platform;
|
|
|
import cn.hutool.crypto.SecureUtil;
|
|
|
import cn.hutool.crypto.symmetric.AES;
|
|
|
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
|
|
|
-import sun.misc.BASE64Decoder;
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
|
public class Test {
|
|
|
+ static final String key="EC/Z+S7c3EFJa2dtvLyekg==";
|
|
|
public static String encryptAES(String data, String key) throws Exception {
|
|
|
- //生成一个随机秘钥
|
|
|
- //byte[] keyRandom = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();
|
|
|
- //将秘钥转为Base64
|
|
|
- //String keyRandomEncode = Base64.encode(keyRandom);
|
|
|
-
|
|
|
- key = "EC/Z+S7c3EFJa2dtvLyekg==";
|
|
|
-
|
|
|
- //将Base64编码的秘钥的格式进行解码转换
|
|
|
byte[] keyByte = Base64.decode(key);
|
|
|
- //加密
|
|
|
- AES aes = SecureUtil.aes(keyByte); //构建
|
|
|
+ AES aes = SecureUtil.aes(keyByte);
|
|
|
byte[] encryptData = aes.encrypt(data); //加密
|
|
|
- //加密后的数据转为Base64
|
|
|
String encryptDataEncode = Base64.encode(encryptData);
|
|
|
-
|
|
|
- //将Base64编码加密数据和秘钥的格式进行解码转换
|
|
|
- byte[] data2 = Base64.decode(encryptDataEncode);
|
|
|
- byte[] key2 = Base64.decode(key);
|
|
|
- //解密
|
|
|
- AES aes2 = SecureUtil.aes(key2);
|
|
|
- byte[] decrypt = aes.decrypt(data2);
|
|
|
-
|
|
|
return encryptDataEncode;
|
|
|
}
|
|
|
-
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
System.out.println("开始");
|
|
|
String sp_jia = "D:\\test\\test+\\100w.txt";//原始文件
|
|
@@ -47,20 +28,14 @@ public class Test {
|
|
|
String jiemi=jmencryptAES(readToString_jia,null);
|
|
|
getFile(jiemi,sp_jie);
|
|
|
System.out.println("完毕");
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
-
|
|
|
public static void getFile(String name,String path) throws IOException {
|
|
|
- // 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
|
|
|
Writer writer = new FileWriter(path, true);
|
|
|
BufferedWriter bufw = new BufferedWriter(writer);
|
|
|
bufw.write(name);
|
|
|
bufw.newLine();
|
|
|
bufw.flush();
|
|
|
}
|
|
|
-
|
|
|
public static String readToString(String fileName) {
|
|
|
String encoding = "UTF-8";
|
|
|
File file = new File(fileName);
|
|
@@ -83,29 +58,11 @@ public class Test {
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
public static String jmencryptAES(String data, String key) throws Exception {
|
|
|
- //生成一个随机秘钥
|
|
|
- //byte[] keyRandom = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();
|
|
|
- //将秘钥转为Base64
|
|
|
- //String keyRandomEncode = Base64.encode(keyRandom);
|
|
|
-
|
|
|
- key = "EC/Z+S7c3EFJa2dtvLyekg==";
|
|
|
-
|
|
|
- //将Base64编码的秘钥的格式进行解码转换
|
|
|
- byte[] keyByte = Base64.decode(key);
|
|
|
- //加密
|
|
|
- AES aes = SecureUtil.aes(keyByte); //构建
|
|
|
- //加密后的数据转为Base64
|
|
|
-
|
|
|
- //将Base64编码加密数据和秘钥的格式进行解码转换
|
|
|
byte[] data2 = Base64.decode(data);
|
|
|
byte[] key2 = Base64.decode(key);
|
|
|
- //解密
|
|
|
AES aes2 = SecureUtil.aes(key2);
|
|
|
byte[] decrypt = aes2.decrypt(data2);
|
|
|
-
|
|
|
return new String (decrypt);
|
|
|
}
|
|
|
}
|