暴力破解压缩包(不可用)

引入依赖,方便解压加密的压缩包

        <!-- 解压压缩包相关的依赖 -->
        <dependency>
            <groupId>net.lingala.zip4j</groupId>
            <artifactId>zip4j</artifactId>
            <version>2.6.1</version>
        </dependency>

        <!-- guava 工具类,可以不引入 -->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>22.0</version>
        </dependency>

循环尝试破解(效率太低。。。)

package com.qiankai.io;

import com.google.common.base.Stopwatch;
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.concurrent.TimeUnit;

/**
 * 破解压缩包
 *
 * @author kai_qian
 * @version v1.0
 * @since 2020/08/04 16:47
 */
public class ZipMain {


    /**
     * 密码中可能存在的字符
     */
    public static char[] chars = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
            '.', '@'};
    public static char[] num = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    public static char[] lowercaseLetter = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
    public static char[] capitalLetter = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
    public static char[] symbol = new char[]{',', '.', '@', '#', '$', '%'};

    public static void main(String[] args) {
        String filePath = "D:\\Users\\Desktop\\aa.zip";
        String outputPath = "D:\\Users\\Desktop\\aaFolder";
        crackZip(filePath, outputPath, 3);// n>3 以后耗时太长
    }

    public static void crackZip(String filePath, String outputPath, int n) {
        ZipFile zipFile = new ZipFile(filePath);
        for (int i = 1; i <= n; i++) {
            Stopwatch stopwatch = Stopwatch.createStarted();
            generatePassword(zipFile, "", outputPath, i);
            long nanos = stopwatch.elapsed(TimeUnit.MILLISECONDS);
            System.out.println(i + "位密码,总耗时:" + nanos + "ms");
        }

    }

    private static boolean readPwdFile(ZipFile zipFile, File pwdFile, String outputPath) {

    }

    /**
     * 穷举密码解压
     *
     * @param zipFile    压缩包文件
     * @param pwd        上一次尝试的密码
     * @param outputPath 文件输出路径
     * @param n          密码位数
     * @return
     */
    private static boolean generatePassword(ZipFile zipFile, String pwd, String outputPath, int n) {
        if (n == 0) {
            return false;
        }
//        System.out.println("n=" + n);
        for (char ch : chars) {
            String newPwd = pwd + ch;
//            Stopwatch stopwatch = Stopwatch.createStarted();
            boolean success = extractFiles(zipFile, newPwd, outputPath);
//            long nanos = stopwatch.elapsed(TimeUnit.MILLISECONDS);
//            System.out.println("解压耗时:" + nanos + "ms");
            if (success) {
                return true;
            }
            if (n > 0) {
                boolean isSuccess = generatePassword(zipFile, newPwd, outputPath, n - 1);
                if (isSuccess) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 解压文件
     *
     * @param zipFile    压缩包文件
     * @param password   压缩包密码
     * @param outputPath 解压后的文件路径
     * @return 返回是否解压成功
     */
    private static boolean extractFiles(ZipFile zipFile, String password, String outputPath) {
        try {
            if (StringUtils.isNotEmpty(password) && zipFile.isEncrypted()) {
                // if yes, then set the password for the zip file
                zipFile.setPassword(password.toCharArray());
            }
            zipFile.extractAll(outputPath);
            System.out.println("解压成功,密码:" + password);
        } catch (ZipException e) {
//            System.out.println("使用密码:" + password + " 验证," + e.getMessage());
            return false;
        }
        return true;
    }

}

循环尝试密码,可以自定义chars中的字符,由于是穷举密码,chars越大效率越低。
解压密码大于3位以后,效率极低,这个代码也就是写着玩一玩了。。。
可以尝试在网上找找密码数据字典。

posted @ 2020-08-05 15:29  它山之玉  阅读(642)  评论(0编辑  收藏  举报