前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >解密日志文件工具类

解密日志文件工具类

作者头像
WindWant
发布2020-09-11 11:40:43
7340
发布2020-09-11 11:40:43
举报
文章被收录于专栏:后端码事后端码事

逐行扫描,解密整行或者解密行中关键字:

代码语言:javascript
复制
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;
    }
}

配置文件:

代码语言:javascript
复制
#日志文件路径
log.path=D:\\
#解密密钥
log.key=!@#$%^&*()_+
log.usewords=true
#需要解密的日志内容关键字
log.keywords=token|phone|username|order id
#解密生成明文日志文件名称前缀
log.deslogprefix=M-
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-01-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档