Browse Source

第二行加密的逻辑在全文件加密下有问题

tanzhongran 3 years ago
parent
commit
6be8713fd2

+ 12 - 3
abi-cloud-qr-platform-server/src/main/java/com/abi/qms/platform/service/impl/GenerateCodeServiceImpl.java

@@ -109,6 +109,9 @@ public class GenerateCodeServiceImpl implements GenerateCodeService {
     /** 允许单个码生成重复的次数 */
     private static final int REPEAT_BUILD_CODE_TIMES = 10;
 
+    /** 加密文件第二行揭秘开来的内容 */
+    private static final String QR_PACKAGE_CHECK_LINE = "ABI I-QMS";
+
     /**
      * 生成码
      */
@@ -504,6 +507,9 @@ public class GenerateCodeServiceImpl implements GenerateCodeService {
     private String createCodeFile(QrPackage qrPackage,BaseFactory coverFactory, QrRepertoryVO qrRepertory,
                                   Map<Long,QrBoxCodeFormatSplit> splitCache,String batchNumber,QrBoxCodeFormat boxCodeFormat,
                                   ArrayList<File> fileList,List<QrData> codeList) {
+        //是按行加密
+        boolean isLineEncode = QrCodeEncodeTypeEnum.LINE_ENCODE.is(coverFactory.getIsQrCodeEncode());
+
         List<QrRepertoryColumnVO> qrRepertoryColumnList = qrRepertory.getQrRepertoryColumnList();
         List<String> columnNameList = new ArrayList<>();
         StringBuilder content = new StringBuilder();
@@ -536,8 +542,11 @@ public class GenerateCodeServiceImpl implements GenerateCodeService {
         }
         content.append("\r\n");
 
-        // 2-第二行放固定的校验密钥;密钥样式为:ABI I-QMS,放置于码包内第二行
-        String checkInfo = AesEncodeUtil.aesEncode("ABI I-QMS", encodeKey);
+        // 2-第二行放固定的校验密钥;密钥样式为:ABI I-QMS,放置于码包内第二行(如果是按行加密则单独加密这一行,否则就不加密放着,最后整个文件会打包加密的)
+        String checkInfo = QR_PACKAGE_CHECK_LINE;
+        if(isLineEncode){
+            checkInfo = AesEncodeUtil.aesEncode(checkInfo, encodeKey);
+        }
         content.append(checkInfo);
         content.append("\r\n");
 
@@ -564,7 +573,7 @@ public class GenerateCodeServiceImpl implements GenerateCodeService {
             String innerData = String.join(",",codeTextList);
 
             // *特殊逻辑,单行加密 如果是单行加密就在这个地方加密
-            if(QrCodeEncodeTypeEnum.LINE_ENCODE.is(coverFactory.getIsQrCodeEncode())){
+            if(isLineEncode){
                 innerData = AesEncodeUtil.aesEncode(innerData, encodeKey);
             }
 

+ 41 - 36
abi-cloud-qr-platform-server/src/test/java/com/abi/qms/platform/EncryptionTest.java

@@ -33,42 +33,6 @@ public class EncryptionTest {
     }
 
 
-
-
-    /**
-     * 写入文件
-     *
-     * @param name
-     * @param path
-     * @throws IOException
-     */
-    public static void getFile(String name, String path) throws IOException {
-        Writer writer = new FileWriter(path, true);
-        BufferedWriter buff = null;
-        try {
-            buff = new BufferedWriter(writer);
-            buff.write(name);
-            buff.newLine();
-            buff.flush();
-        }finally {
-            if (null != buff){
-                buff.close();
-            }
-        }
-    }
-
-
-    public static void main(String[] args) {
-        String key = "EC/Z+S7c3EFJa2dtvLyekg==";
-        // 单个字符串解码
-        String content = "aLI5iXza3R/w7aJ2AsYhUT7EGS0Zqk94k1nXK9rT8Av5vycunG6LV66hW277u21j68NwDCVe7b1uVF8/sZaqnQ==";
-        // 文件解码
-        //String content = readToString("C:\\Users\\ThinkPad\\Downloads\\8905ecf5-bfaa-4962-9743-04a5ca2a8807\\1.txt");
-        String decodeStr = AesEncodeUtil.aesDecode(content, key);
-        System.out.println(decodeStr);
-    }
-
-
     /**
      * 读取文件
      *
@@ -103,6 +67,47 @@ public class EncryptionTest {
         }
     }
 
+    /**
+     * 写入文件
+     *
+     * @param content
+     * @param path
+     * @throws IOException
+     */
+    public static void outputFile(String content, String path) throws IOException {
+        Writer writer = new FileWriter(path, true);
+        BufferedWriter buff = null;
+        try {
+            buff = new BufferedWriter(writer);
+            buff.write(content);
+            buff.newLine();
+            buff.flush();
+        }finally {
+            if (null != buff){
+                buff.close();
+            }
+        }
+    }
+
+    /**
+     * 读文件解码再写文件
+     * @param readFilePath
+     * @param writeFilePath
+     */
+    public static void readAndDecodeToFile(String readFilePath,String writeFilePath){
+        String content = readToString("D:\\0.txt");
+        String decodeStr = AesEncodeUtil.aesDecode(content, key);
+        try{
+            outputFile(decodeStr,"D:\\decode.txt");
+        }catch (Exception e){
+
+        }
+    }
+
+    public static void main(String[] args) {
+        readAndDecodeToFile("D:\\0.txt","D:\\decode.txt");
+    }
+
     /**
      * 解密
      *