dat数据图像代码转码可视化

可直接复制使用

# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

import os
into_path = r'********'  # 微信image.dat文件路径   
						
out_path = r"********" #创建这个路径把dat文件放在这

def imageDecode(f, fn):
    dat_read = open(f, "rb")  						# 读取.dat 文件
    xo = Format(f)  								# 判断图片格式 并计算返回异或值 
    out = out_path + fn + ".jpg"  					# 图片输出路径
    print("文件输出路径{}".format(out), end='\n\n')
    png_write = open(out, "wb")  					# 图片写入
    dat_read.seek(0)  								# 重置文件指针位置

    for now in dat_read:  							# 循环字节
        for nowByte in now:
            newByte = nowByte ^ xo  				# 转码计算
            png_write.write(bytes([newByte]))  		# 转码后重新写入

    dat_read.close()
    png_write.close()


def findFile(f):
    """
    寻找文件
    param f:微信图片路径
    return:
    """
    fsinfo = os.listdir(f) 		 					# 把路径文件夹下的文件以列表呈现
    print(fsinfo)
    for fn in fsinfo:  								# 逐步读取文件
        temp_path = os.path.join(f, fn)  			# 拼接路径:微信图片路径+图片名
        if os.path.isfile(temp_path):  				# 判断目录还是.dat  #temp_path需为绝对路径,判断是否为文件,也可 if not os.path.isdir(temp_path):
            print('找到文件路径{}'.format(temp_path))
            fn = fn[:-4]  							# 截取字符串 去掉后缀.dat
            imageDecode(temp_path, fn)  			# 转码函数
        else:
            pass


def Format(f):
    """
    计算异或值
    各图片头部信息
    jpeg:ff d8 ff
    png:89 50 4e 47
    gif: 47 49 46 38
        """
    dat_r = open(f, "rb")

    try:
        a = [(0x89, 0x50, 0x4e), (0x47, 0x49, 0x46), (0xff, 0xd8, 0xff)]
        for now in dat_r:
            for xor in a:
                i = 0
                res = []
                nowg = now[:3]						#取前三个 数据信息
                for nowByte in nowg:
                    res.append(nowByte ^ xor[i])	#进行判断
                    i += 1
                if res[0] == res[1] == res[2]:		#三次异或值想等 说明就是那种格式
                    return res[0]					#返回异或值
    except:
        pass
    finally:
        dat_r.close()


# 运行
if __name__ == '__main__':
    findFile(into_path)

关注公众号获取更多有趣分享

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_文和_

感觉对您有用,可以来点鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值