温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么利用python破解zip加密文件

发布时间:2022-05-23 09:17:45 来源:亿速云 阅读:400 作者:iii 栏目:开发技术

本篇内容主要讲解“怎么利用python破解zip加密文件”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么利用python破解zip加密文件”吧!

一、破解zip加密文件的思路

  • 准备一个加密的zip文件。

  • zipfile模块可以解压zip文件。

解压时可以提供密码zfile.extractall("./", pwd=password.encode("utf8"))

  • itertools.permutations实现全字符的全排列。

通过函数itertools.permutations("abc", 3)实现全字符的全排列:abc/acb/bca/bac/cab/cba

二、实例代码演示

0、zip的压缩方式

本文介绍的zip文件知道密码一共是4位的,密码字符的范围是a-z1-0。并且不存在重复字符的,不会有“aabb”的密码。zip压缩时是选择了zip传统加密!

1、解压zip文件

导入zipfile模块,使用其中的extractall()函数。

import itertools
filename = "readme.zip"
# 创建一个解压的函数,入参为文件名和密码
# 并使用try-except,避免报错中断程序。
def uncompress(file_name, pass_word):
    try:
        with zipfile.ZipFile(file_name) as z_file:
            z_file.extractall("./", pwd=pass_word.encode("utf-8"))
        return True
    except:
        return False

2、实现密码字符的全排列

import zipfile
import itertools
filename = "readme.zip"
# 创建一个解压的函数,入参为文件名和密码
# 并使用try-except,避免报错中断程序。
def uncompress(file_name, pass_word):
    try:
        with zipfile.ZipFile(file_name) as z_file:
            z_file.extractall("./", pwd=pass_word.encode("utf-8"))
        return True
    except:
        return False
# chars是密码可能的字符集
chars = "abcdefghijklmnopqrstuvwxyz0123456789"
for c in itertools.permutations(chars, 4):
    password = ''.join(c)
    print(password)
    result = uncompress(filename, password)
    if not result:
        print('解压失败。', password)
    else:
        print('解压成功。', password)
        break

文件压缩时,一些注意的事项: 

怎么利用python破解zip加密文件

三、密码是几位未知,也可以破解密码

查过一些资料,zip压缩文件密码最长为12位,在原来的程序上增加上一个for循环就可以实现破解密码了。

import zipfile
import itertools
filename = "readme.zip"
def uncompress(file_name, pass_word):
    try:
        with zipfile.ZipFile(file_name) as z_file:
            z_file.extractall("./", pwd=pass_word.encode("utf-8"))
        return True
    except:
        return False
chars = "abcdefghijklmnopqrstuvwxyz0123456789"
for i in range(12):
    for c in itertools.permutations(chars, i):
        password = ''.join(c)
        print(password)
        result = uncompress(filename, password)
        if not result:
            print('解压失败。', password)
        else:
            print('解压成功。', password)
            break

到此,相信大家对“怎么利用python破解zip加密文件”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI