摘要:看我如何用 20 行讓女神對我轉變態度的…
本文分享自華爲雲社區《女神相冊密碼忘記了,我只用Python寫了20行代碼~~~》,原文做者:LexSaints 。python
今早上班,公司女神小姐姐說,她去年去三亞旅遊的照片打不開了,好奇問了一下才知道,原來是,她把照片壓縮了,並且還加了密碼。可是密碼不記得了,只記得是一串 6 位數字。話說照片壓縮率也不高,並且還加密,難道是有什麼可愛的小照片。segmentfault
可是做爲一個正(ba)直(gua)的技術人員,我跟她說:「這事交給我,python 寫個腳本,幫你破解掉~~」測試
對相冊進行壓縮的時候,添加了密碼。LIke This ↓加密
打開的時候,提示這樣的,須要輸入密碼。spa
首先若是想要 python 命令行來打開小姐姐相冊,那麼首先要找到嘗試打開的命令行,即解壓縮時使用的命令行。而後咱們使用 python 腳本寫嵌套循環,不斷的對 zip 文件進行嘗試解壓,而後找回真實的密碼。命令行
首先壓縮文件是 zip 格式的,咱們使用萬能的 7z 命令來進行解壓。code
爲何不用 unzip 命令呢?(由於我試過了,unzip 沒法循環)blog
#7Z詳細參數,下面只截取幾個關鍵參數PS C:\Users\lex> 7z7-Zip 21.01 alpha (x64) : Copyright (c) 1999-2021 Igor Pavlov : 2021-03-09Usage: 7z <command> [<switches>...] <archive_name> [<file_names>...] [@listfile]<Commands> a : Add files to archive #加入壓縮 d : Delete files from archive e : Extract files from archive (without using directory names) t : Test integrity of archive #嘗試密碼,不解壓...<Switches> -o{Directory} : set Output directory -p{Password} : set Password #設置密碼參數
命令太簡單,感受都有點配不上個人才華和思路ip
7z -p 123456 t 三亞相冊.zip# t:嘗試打開,相似後臺運行# -p:嘗試的密碼# 最後是要解壓的文件
根據小姐姐的需求,密碼是 6 位純數字,那就幫我節省了好大一段時間,只對 6 位純數字進行嘗試就能夠了。三分鐘就把腳本搞出來了utf-8
# -*- coding:utf-8 -*- import osimport subprocessimport zipfiledef brutecrack(): for a in range(1,10): for b in range(1,10): for c in range(1,10): for d in range(1,10): for e in range(1,10): for f in range(1,10): passwd=str(a)+str(b)+str(c)+str(d)+str(e)+str(f) command='7z -p'+passwd+' t F:/三亞相冊.zip' #t 表示test,不進行實際解壓,只測試密碼 print(passwd) child=subprocess.call(command) #os.popen(command)#這個也能夠用,可是很差監控解壓狀態 print(child) if child==0: print("相冊密碼爲:"+passwd) returnif __name__ == '__main__': brutecrack()
面對着束手無策的女神,我運行起了腳本,不到 5 秒,相冊成功打開了。效果 gif ↓
打開以後,女神看個人眼神都變了。