前言
想找以前聊天的图片的时候才发现PC版的微信图片储存的文件都dat格式,不能直接打开。在度娘搜索到微信会加密存储用户接受到的所有图片信息,解密方法也好多。
刚好最近在学python,看到有用python的解密方法,拿来练练手好了
解密方法
在知乎上看到大佬Python小宇宙的方法

代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| import os
''' 图片字节头信息, [0][1]为jpg头信息, [2][3]为png头信息, [4][5]为gif头信息 ''' pic_head = [0xff, 0xd8, 0x89, 0x50, 0x47, 0x49]
decode_code = 0
def get_code(file_path): """ 自动判断文件类型,并获取dat文件解密码 :param file_path: dat文件路径 :return: 如果文件为jpg/png/gif格式,则返回解密码,否则返回0 """ dat_file = open(file_path, "rb") dat_read = dat_file.read(2) head_index = 0 while head_index < len(pic_head): code = dat_read[0] ^ pic_head[head_index] idf_code = dat_read[1] ^ code head_index = head_index + 1 if idf_code == pic_head[head_index]: dat_file.close() return code head_index = head_index + 1
print("not jpg, png , gif") return 0
def decode_dat(file_path): """ 解密文件,并生成图片 :param file_path: dat文件路径 : return: 无 """ decode_code = get_code(file_path) dat_file = open(file_path, "rb") pic_name = os.path.splitext(file_path)[0] + ".jpg" if not os.path.isfile(pic_name): pic_write = open(pic_name, "wb") for dat_data in dat_file: for dat_byte in dat_data: pic_data = dat_byte ^ decode_code pic_write.write(bytes([pic_data])) print(pic_name + '完成') dat_file.close() pic_write.close()
def find_datfile(dir_path): """ 获取dat文件目录下所有的文件 :param dir_path: dat文件目录 :return: 无 """ flies_list = os.listdir(dir_path) for file_name in flies_list: file_path = os.path.join(dir_path, file_name) if os.path.isdir(file_path): find_datfile(file_path) if os.path.isfile(file_path): decode_dat(file_path)
path = 'C:\\Users\\qiu\\Desktop\\222' find_datfile(path) print("文件解密完成")
|
感谢大佬 Python小宇宙