java根据密码字典解密word和excel加密文件

时间:2024-04-15 22:13:23

 本类为word解密的工具类,后期还会有压缩包的加密解密,以及暴力破解相关方法,喜欢的朋友可以关注我的后期更新,尊重原创,切勿胡乱转发

/** 
 * @Description word破解工具类
 * 
 * @ClassName: CrackWordUtil.java
 * @createDate 2018年9月28日
 * @Encoding UTF-8
 * @author chenheng
 * @version 1.0
 * @since 1.0 
 */
public class CrackWordUtil {

    //计数器,用来定义密码库中出现重复的密码个数
    private static final Integer ONE = 1;
 

    public static List<String> getPassword(File pathPasslib) {
        //用来装载密码库,并记录密码出现次数
        Map<String, Integer> map = new HashMap<String, Integer>();
        //用来装载去重后的密码
        List<String> arrPass = new ArrayList<String>();
        /* 读取数据 */
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(pathPasslib),"UTF-8"));
            String lineTxt = null;
            while ((lineTxt = br.readLine()) != null) {
                String[] names = lineTxt.split("\\s");
                for (String name : names) {
                    //for (String key : map.keySet()) 利用这种方式可以遍历Map中的key
                    if (map.keySet().contains(name)) { //map.keySet()获取map全部的key值,迭代取出Map中的key,组成一个Set集合
                        map.put(name, (map.get(name) + ONE));
                    } else {
                        map.put(name, ONE);
                    }
                }
            }
            br.close();
        } catch (Exception e) {
            System.err.println("read errors :" + e.getMessage());
        }
        
        //遍历map将密码库转换成List,去掉重复的密码
        for (String key : map.keySet()) {
            arrPass.add(key);
        }
        System.out.println(arrPass.toString());
        return arrPass;
    }
    
    /**
     * 
     * 函数的目的/功能   解密docx文件,返回解密成功的密码
     * @param WordFile 
     * @param password
     * @return
     */
    public static String parseWord(String WordFile,String password){
        File file = new File(WordFile);
        InputStream is = null;
        String psd= "";
        try {
            is = new FileInputStream(file);
            POIFSFileSystem pfs = new POIFSFileSystem(is);
            EncryptionInfo ei = new EncryptionInfo(pfs);
            Decryptor dec = Decryptor.getInstance(ei);
            //word文件解密,返回解密结果
            boolean flag = dec.verifyPassword(password);
            if (flag) {
                System.out.println("解密成功,密码为:"+password);
                psd = password;
            }
        } catch (Exception e) {
            System.out.println("B");
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return psd;
    }
    
    /**
     * 
     * 函数的目的/功能   解密 xls 或 xlsx 文件,返回解密成功的密码
     * @param url
     * @param pwd
     * @return
     */
    public static String decryptXlsx(String url, String pwd) {
        File file = new File(url);
        InputStream is = null;
        String psd= "";
        try {
            is = new FileInputStream(file);
            POIFSFileSystem pfs = new POIFSFileSystem(is);
            is.close();
            EncryptionInfo ei = new EncryptionInfo(pfs);
            Decryptor decryptor = Decryptor.getInstance(ei);
            //word文件解密,返回解密结果
            boolean flag = decryptor.verifyPassword(pwd);
            if (flag) {     
//              System.out.println("解密成功,密码为:"+pwd);
              psd = pwd;
            }else{
                  //密码不匹配
                  System.out.println("密码不匹配");
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return psd;
    }
    
}