Java 暴力破解zip压缩包密码

1、引用zip4j包

<dependency>
    <groupId>net.lingala.zip4j</groupId>
    <artifactId>zip4j</artifactId>
    <version>1.3.2</version>
</dependency>

2、获取所有可能的密码集合

package com.yymagicer.util;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
/**
 * @ClassName StringUtil
 * @Description
 * @Author yangxd
 * @Date 2021/5/6 16:39
 * @Version 1.0
 **/
public class StringUtil {
    /**
     * 数字
     */
    public static final String NUMBER = "0123456789";
    /**
     * 字母
     */
    public static final String ALPHABET = "abcdefghijklmnopqrstuvwyxz";
    /**
     * 符号
     */
    public static final String SYMBOL = "~!@#$%^&*()_+[]{};,.<>?-=";
 
    /**
     * 获取指定的字符
     *
     * @param includeNumber   是否包含数字
     * @param includeAlphabet 是否包含字母
     * @param includeSymbol   是否包含字符
     * @param length          字符长度
     * @return
     */
    public static List<String> getStr(boolean includeNumber, boolean includeAlphabet, boolean includeSymbol, int length) {
 
        List<String> result = new ArrayList<>();
        StringBuffer sb = new StringBuffer();
        if (includeNumber) {
            sb.append(NUMBER);
        }
        if (includeAlphabet) {
            sb.append(ALPHABET);
            sb.append(ALPHABET.toUpperCase());
        }
        if (includeSymbol) {
            sb.append(SYMBOL);
        }
        if (sb.length() <= length) {
            result.add(sb.toString());
        }
        char[] chars = sb.toString().toCharArray();
        String[] strings = new String[chars.length];
        for (int i = 0; i < chars.length; i++) {
            strings[i] = String.valueOf(chars[i]);
        }
        String[] allLists = getAllLists(strings, length);
        return Arrays.asList(allLists);
    }
 
    /**
     * 获取指定位数的数据集合
     *
     * @param elements 基类字符数组
     * @param length   指定字符串位数
     * @return
     */
    public static String[] getAllLists(String[] elements, int length) {
        String[] allLists = new String[(int) Math.pow(elements.length, length)];
        if (length == 1) {
            return elements;
        } else {
            String[] allSublists = getAllLists(elements, length - 1);
            int arrayIndex = 0;
            for (int i = 0; i < elements.length; i++) {
                for (int j = 0; j < allSublists.length; j++) {
                    allLists[arrayIndex] = elements[i] + allSublists[j];
                    arrayIndex++;
                }
            }
            return allLists;
        }
    }
 
    /**
     * 获取全部字符集合,包含数字,字母,特殊字符
     *
     * @param length
     * @return
     */
    public static List<String> getFullStr(int length) {
        return getStr(true, true, true, length);
    }
 
    /**
     * 获取数字字符集合
     *
     * @param length
     * @return
     */
    public static List<String> getNumberStr(int length) {
        return getStr(true, false, false, length);
    }
 
    /**
     * 获取字母字符集合
     *
     * @param length
     * @return
     */
    public static List<String> getAlphabetStr(int length) {
        return getStr(false, true, false, length);
    }
 
    /**
     * 获取特殊符号字符集合
     *
     * @param length
     * @return
     */
    public static List<String> getSymbolStr(int length) {
        return getStr(false, false, true, length);
    }
 
    public static void main(String[] args) {
        final List<String> str = getNumberStr(4);
        System.out.println(str.toString());
    }
 
}

3、解压工具类

package com.yymagicer.util;
 
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.FileHeader;
 
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
 
/**
 * @ClassName UnZipUtils
 * @Description
 * @Author yangxd
 * @Date 2021/5/7 10:20
 * @Version 1.0
 **/
public class UnZipUtils {
 
    public static void main(String[] args) throws IOException {
        String source = "E:\\BaiduNetdiskDownload\\test\\02.zip";
        String dest = "E:\\BaiduNetdiskDownload\\test";
        String password = "1234";
        boolean unZip = unZip(source, dest, password);
        System.out.println(unZip);
    }
 
    /**
     * @param source   原始文件路径
     * @param dest     解压路径
     * @param password 解压文件密码(可以为空)
     */
    public static boolean unZip(String source, String dest, String password) {
        try {
            File zipFile = new File(source);
            // 首先创建ZipFile指向磁盘上的.zip文件
            ZipFile zFile = new ZipFile(zipFile);
            zFile.setFileNameCharset(StandardCharsets.UTF_8.name());
            // 解压目录
            File destDir = new File(dest);
            // 目标目录不存在时,创建该文件夹
            if (!destDir.exists()) {
                destDir.mkdirs();
            }
            if (zFile.isEncrypted()) {
                // 设置密码
                zFile.setPassword(password.toCharArray());
            }
            // 将文件抽出到解压目录(解压)
            zFile.extractAll(dest);
            List<FileHeader> headerList = zFile.getFileHeaders();
            List<File> extractedFileList = new ArrayList<>();
            for (FileHeader fileHeader : headerList) {
                if (!fileHeader.isDirectory()) {
                    extractedFileList.add(new File(destDir, fileHeader.getFileName()));
                }
            }
            File[] extractedFiles = new File[extractedFileList.size()];
            extractedFileList.toArray(extractedFiles);
            for (File f : extractedFileList) {
                System.out.println(f.getAbsolutePath() + "文件解压成功!");
            }
        } catch (ZipException e) {
            return false;
        }
        return true;
    }
}

4、多线程处理逻辑

package com.yymagicer.core;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * @ClassName PasswordCrackService
 * @Description
 * @Author yangxd
 * @Date 2021/5/7 10:01
 * @Version 1.0
 **/
public interface PasswordCrackService {
 
    Integer THREAD_NUM = 5;
 
    String run(String source, String dest);
 
    default public List<List<String>> getShardingList(List<String> numberStr) {
        int num = numberStr.size() / THREAD_NUM;
        List<List<String>> dataList = new ArrayList<>();
        int index = 0;
        int temp = 0;
        while (temp < numberStr.size()) {
            temp = (index + 1) * num;
            dataList.add(numberStr.subList(index * num, temp > numberStr.size() ? numberStr.size() : temp));
            index++;
        }
        return dataList;
    }
}

package com.yymagicer.core;
 
import com.yymagicer.util.StringUtil;
import com.yymagicer.util.UnZipUtils;
import org.apache.commons.lang3.StringUtils;
 
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.*;
 
/**
 * @ClassName ThreadProcessPasswordCrackServiceImpl
 * @Description
 * @Author yangxd
 * @Date 2021/5/7 13:50
 * @Version 1.0
 **/
public class ThreadProcessPasswordCrackServiceImpl implements PasswordCrackService {
 
    private ThreadPoolExecutor threadPoolExecutor;
 
    public ThreadProcessPasswordCrackServiceImpl() {
        this.threadPoolExecutor = new ThreadPoolExecutor(4, 20, 60,
                TimeUnit.SECONDS, new LinkedBlockingQueue<>(), Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
    }
 
    @Override
    public String run(String source, String dest) {
        List<String> numberStr = StringUtil.getNumberStr(6);
        LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<>();
        numberStr.forEach(num -> queue.offer(num));
        long startTime = System.currentTimeMillis();
        while (!queue.isEmpty()) {
            threadPoolExecutor.execute(() -> {
                String name = Thread.currentThread().getName();
                String key = queue.poll();
                if (StringUtils.isNotBlank(key)) {
                    boolean result = UnZipUtils.unZip(source, dest, key);
                    if (result) {
                        try (FileWriter fileWriter = new FileWriter(dest + "\\password.txt")) {
                            fileWriter.write(key);
                            System.out.println("线程:" + name + ",密码是:" + key);
                            queue.clear();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    } else {
                        System.out.println("线程:" + name + "," + key + "密码错误");
                    }
                }
            });
        }
 
        final long endTime = System.currentTimeMillis();
        System.out.println("共花费:" + (endTime - startTime) / 1000 + "秒");
        return null;
    }
}

5、测试

package com.yymagicer.core;
 
/**
 * @ClassName Main
 * @Description
 * @Author yangxd
 * @Date 2021/5/7 10:00
 * @Version 1.0
 **/
public class Main {
 
    public static void main(String[] args) {
        PasswordCrackService service = new ThreadProcessPasswordCrackServiceImpl();
        String source = "E:\\BaiduNetdiskDownload\\test\\wzzz.zip";
        String dest = "E:\\BaiduNetdiskDownload\\test";
        service.run(source, dest);
    }
}

项目地址:源码地址

  • 5
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值