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

 本类为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;
    }
    
}

 

转载于:https://www.cnblogs.com/fei-tian/p/9729966.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值