逐行扫描,解密整行或者解密行中关键字:
import com.xxx.common.util.EncryptUtil;//相应的解密工具 import java.io.BufferedReader; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.Properties; public class LogDecryptionTool { private static String KEY = "!@#$%^&*()_+"; //解密key private static List<String> wordList = new ArrayList(); private static boolean usewords = false;//解密关键字,还是解密整行 private static String DES_PREFIX = "M-"; //解密生成的文件的前缀 public LogDecryptionTool() { } public static void main(String[] args) { FileInputStream in = null; try { try { Properties e = new Properties(); in = new FileInputStream(System.getProperty("user.dir") + "/config.properties"); e.load(in); String logPath = e.getProperty("log.path"); KEY = e.getProperty("log.key"); DES_PREFIX = e.getProperty("log.deslogprefix"); usewords = Boolean.valueOf(e.getProperty("log.usewords")).booleanValue(); if(usewords) { String keywords = e.getProperty("log.keywords"); String[] words = keywords.split("\\|"); if(words.length == 0) { return; } wordList = Arrays.asList(words); } System.out.println("log path: " + logPath); decrypt(new File(logPath)); } catch (IOException var16) { System.out.println(var16.getMessage()); } } finally { if(in != null) { try { in.close(); } catch (IOException var15) { System.out.println(var15.getMessage()); } } } } /** * 解密日志文件 * @param logFile */ public static void decrypt(File logFile) { //文件夹,则筛选特定日志文件 if(logFile.isDirectory()) { List files = Arrays.asList(logFile.listFiles((pathname) -> { return pathname.getName().endsWith(".log") && !pathname.getName().startsWith(DES_PREFIX) || pathname.isDirectory() && pathname.getName().startsWith("xxx"); })); Iterator it = files.iterator(); //递归 while(it.hasNext()) { File file = (File)it.next(); decrypt(file); } } else { (new Thread(() -> { decrypteLog(logFile); })).start(); } } //解密实际的日志文件 public static void decrypteLog(File logFile) { String path = logFile.getAbsolutePath(); if(!path.endsWith("/") && !path.endsWith("\\")) { int lastIndex = path.lastIndexOf("/"); lastIndex = lastIndex == -1?path.lastIndexOf("\\"):lastIndex; String spath = path.substring(0, lastIndex); String desPath = spath + "/" + "M-" + logFile.getName(); BufferedReader fbr = null; FileReader fr = null; FileOutputStream fo = null; OutputStreamWriter out = null; try { fr = new FileReader(path); fo = new FileOutputStream(desPath); out = new OutputStreamWriter(fo); fbr = new BufferedReader(fr); System.out.println("log file " + path + " decryption begin..."); String e; for(; (e = fbr.readLine()) != null; out.append("\r\n")) { if(usewords) { out.append(dealWords(e)); } else { out.append(EncryptUtil.aesDecrypt(e, KEY));//使用相应的解密算法解密像一个的字段 } } out.flush(); System.out.println("log file " + path + " decryption over, des file path: " + desPath + " !"); } catch (Exception var18) { System.out.println(var18.getMessage()); } finally { try { if(fbr != null) { fbr.close(); } if(out != null) { out.close(); } if(fr != null) { fr.close(); } if(fo != null) { fo.close(); } } catch (IOException var17) { System.out.println(var17.getMessage()); } } } else { System.out.println("file path required!"); } } /** * 处理关键字解密 * @param line * @return */ private static String dealWords(String line) { String tempLine = line; Iterator var2 = wordList.iterator(); while(var2.hasNext()) { String word = (String)var2.next(); int wordIndex = tempLine.indexOf(word + ":"); if(wordIndex != -1) { int endIndex = tempLine.indexOf(",", wordIndex); if(endIndex == -1) { endIndex = tempLine.length() - 1; } String tword = tempLine.substring(wordIndex + 1 + word.length(), endIndex); String mi = EncryptUtil.aesDecrypt(tword.trim(), KEY); tempLine = tempLine.replace(tword, mi); } } return tempLine; } public static void testEncryptLog() { BufferedReader fbr = null; FileReader fr = null; FileOutputStream fo = null; OutputStreamWriter out = null; try { fr = new FileReader("D:\\×××-12-05-2016-1.log"); fo = new FileOutputStream("D:\\×××-12-05-2016-1.log"); out = new OutputStreamWriter(fo); String e; for(fbr = new BufferedReader(fr); (e = fbr.readLine()) != null; out.append("\r\n")) { if(usewords) { out.append(dealEnWords(e)); } else { out.append(EncryptUtil.aesDecrypt(e, KEY)); } } out.flush(); System.out.println("en over!"); } catch (FileNotFoundException var15) { var15.printStackTrace(); } catch (IOException var16) { var16.printStackTrace(); } finally { try { if(fbr != null) { fbr.close(); } if(out != null) { out.close(); } if(fr != null) { fr.close(); } if(fo != null) { fo.close(); } } catch (IOException var14) { var14.printStackTrace(); } } } private static String dealEnWords(String line) { String tempLine = line; Iterator var2 = wordList.iterator(); while(var2.hasNext()) { String word = (String)var2.next(); int wordIndex = tempLine.indexOf(word + ":"); if(wordIndex != -1) { int endIndex = tempLine.indexOf(",", wordIndex); if(endIndex == -1) { endIndex = tempLine.length() - 1; } String tword = tempLine.substring(wordIndex + 1 + word.length(), endIndex); String mi = EncryptUtil.aesEncrypt(tword.trim(), KEY); tempLine = tempLine.replace(tword, mi); } } return tempLine; } }
配置文件:
#日志文件路径 log.path=D:\\ #解密密钥 log.key=!@#$%^&*()_+ log.usewords=true #需要解密的日志内容关键字 log.keywords=token|phone|username|order id #解密生成明文日志文件名称前缀 log.deslogprefix=M-