Ref: https://blog.csdn.net/wujinmei/article/details/116073984
import os
path=r'your dat files path'
dat文件保存的位置
def imageDecode(fpath,fn):
dat_read = open(fpath, "rb")
out=fpath+".png"
#png格式前面是89504E47
#jpg格式前面是FFD8FFE0
#gif格式前面是47494638
filetype="png"
first2=dat_read.read(2)
#先取前面的两位
#print(hex(first2[0]))
xor=0x00
png1=int(str(hex(first2[0])),16) ^ 0x89
png2=int(str(hex(first2[1])),16) ^ 0x50
if png1==png2:
xor=png1
filetype="png"
else:
jpg1=int(str(hex(first2[0])),16) ^ 0xFF
jpg2=int(str(hex(first2[1])),16) ^ 0xD8
if jpg1==jpg2:
filetype="jpg"
xor=jpg1
out=fpath+".jpg"
else:
gif1=int(str(hex(first2[0])),16) ^ 0x47
gif2=int(str(hex(first2[1])),16) ^ 0x49
if gif1==gif2:
filetype="gif"
xor=gif1
out=fpath+".gif"
if xor>0x00:
png_write = open(out, "wb")
if filetype=="png":
png_write.write(bytes([0x89]))
png_write.write(bytes([0x50]))
elif filetype=="jpg":
png_write.write(bytes([0xFF]))
png_write.write(bytes([0xD8]))
elif filetype=="gif":
png_write.write(bytes([0x47]))
png_write.write(bytes([0x49]))
for now in dat_read:
for nowByte in now:
newByte = nowByte ^ int(str(hex(xor)),16)
png_write.write(bytes([newByte]))
png_write.close()
dat_read.close()
def findFile(f):
fsinfo = os.listdir(f)
for fn in fsinfo:
temp_path = os.path.join(f, fn)
if not os.path.isdir(temp_path):
if fn[-4:]==".dat":
print('文件路径: {}' .format(temp_path))
imageDecode(temp_path,fn)
#os.remove(temp_path) #删除原dat文件
else:
findFile(temp_path)
findFile(path)
网友评论