Browse Source

修改方式

liguosong 3 years ago
parent
commit
9b8d46326e

+ 61 - 21
abi-cloud-qr-platform-server/src/test/java/com/abi/qms/platform/Test.java

@@ -2,42 +2,57 @@ 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 {
+    public static String encryptAES(String data, String key) throws Exception {
+        //生成一个随机秘钥
+        //byte[] keyRandom = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();
+        //将秘钥转为Base64
+        //String keyRandomEncode = Base64.encode(keyRandom);
 
-        static String secretKey = "sXRnMPO9dXWkFNKY9GMv3g==";
-        static final BASE64Decoder decoder = new BASE64Decoder();
+        key = "EC/Z+S7c3EFJa2dtvLyekg==";
 
-        public static void main(String[] args) throws IOException {
-        String secretKey = "sXRnMPO9dXWkFNKY9GMv3g==";
-        System.out.println(secretKey+"开始");
+        //将Base64编码的秘钥的格式进行解码转换
+        byte[] keyByte = Base64.decode(key);
+        //加密
+        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";//原始文件
         String sp = "D:\\test\\100w.txt";//原始文件
         String sp_jie = "D:\\test\\test-\\100w.txt";//原始文件
         String readToString=readToString(sp);//读取文件
-//       System.out.println(readToString);
-        getFile(jiami(readToString),sp_jia);//写入加密文件夹
-
+        String jiami=encryptAES(readToString,null);
+        getFile(jiami,sp_jia);//加密
+        System.out.println("写入完毕");
         String readToString_jia=readToString(sp_jia);//读取文件
-        getFile(jiemi(readToString_jia),sp_jie);//写入加密文件夹
+        String jiemi=jmencryptAES(readToString_jia,null);
+        getFile(jiemi,sp_jie);
         System.out.println("完毕");
-    }
 
-        public static String jiami(String content) throws IOException {
-        AES aes = SecureUtil.aes(decoder.decodeBuffer(secretKey));
-        String encryptHex = aes.encryptHex(content);
-        return encryptHex;
-    }
-        public static String jiemi(String content) throws IOException {
-        AES aes = SecureUtil.aes(decoder.decodeBuffer(secretKey));
-        String decryptStr = aes.decryptStr(content);
-        return decryptStr;
+
+
     }
 
-        public static void getFile(String name,String path) throws IOException {
+    public static void getFile(String name,String path) throws IOException {
         // 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
         Writer writer = new FileWriter(path, true);
         BufferedWriter bufw = new BufferedWriter(writer);
@@ -46,7 +61,7 @@ public class Test {
         bufw.flush();
     }
 
-        public static String readToString(String fileName) {
+    public static String readToString(String fileName) {
         String encoding = "UTF-8";
         File file = new File(fileName);
         Long filelength = file.length();
@@ -68,5 +83,30 @@ 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);
+    }
 }