通过jpg图片隐藏文件

比如我们现在有一个视频Video.mkv,我们想隐藏它,那么我们可以找一张背景图片谣言.jpg
把他们放在同一目录下:

Video.mkv打包成压缩包Video.rar,为什么要打包呢?
因为这是为后面解压服务得~

在该目录下编写bat文件:

copy  /b  谣言.jpg+Video.rar 谣言2.jpg

双击运行压缩.bat,我们可以看到目录下生成了一张谣言2.jpg的图片,看看大小,整合等于压缩包文件和图片文件总和~

最后,那如何还原原文件呢??
很简单,直接将文件后缀改为rar压缩包文件进行解压就可以了,因为rar解压有个专属的开始位置,解压程序会读到开始位置的标识符才执行解压程序,应该前面的jpg二进制会被忽略。

当然如果你希望获得更强大的加密可以自己编写加密解密策略。

附上Python代码:
github地址
加密

#coding:utf-8

import click
import random

@click.command()
@click.option('--background', prompt=True, help='输入用于隐藏的背景图片文件.')
@click.option('--encryptfile', prompt=True, help='输入需要加密的文件.')
def encryptfile(background, encryptfile):
seed = random.randint(1024, 2048)
seeds = []
for i in range(seed):
seeds.append(random.randint(1, 128))
confusion = bytes(seeds)

with open(background, 'rb') as fr_bg:
bg_body = fr_bg.read()

with open(encryptfile, 'rb') as fr_enc:
enc_body = fr_enc.read()

confusion_bytes_index = random.randint(1, len(enc_body)//2)

objectfile = "encry_%d_%d_%d_"%(len(bg_body), len(confusion), confusion_bytes_index) + background
data = bg_body + confusion + enc_body[confusion_bytes_index:] + confusion + enc_body[:confusion_bytes_index]

print(len(bg_body)+len(confusion))
print(len(enc_body[confusion_bytes_index:]))
print(len(enc_body[:confusion_bytes_index]))
print(len(bg_body)+2*len(confusion)+len(enc_body[confusion_bytes_index:]))
print(len(data))

with open(objectfile, "wb") as fw_obf:
fw_obf.write(data)

if __name__ == "__main__":
encryptfile()

解密:

#coding:utf-8

import click

@click.command()
@click.option('--inputfile', prompt=True, help='输入文件名及路径.')
@click.option('--outputfile', prompt=True, help='输出文件名及路径.')
def decryptFile(inputfile, outputfile):
decryptfile = outputfile
encryptfile = inputfile

encrypt = encryptfile.split("_")
bg_length, confuse_length, confusion_bytes_index = int(encrypt[1]), int(encrypt[2]), int(encrypt[3])
headerindex = bg_length + confuse_length

with open(decryptfile, "wb") as fw:
with open(encryptfile, "rb") as fr:
data = fr.read()
frist_data_num = len(data)-(headerindex + confusion_bytes_index + confuse_length)
frist_data = data[-confusion_bytes_index:]
second_data = data[headerindex:headerindex+frist_data_num]
print(headerindex + confusion_bytes_index + confuse_length)
print(len(frist_data),len(second_data))
fw.write(frist_data+second_data)

if __name__ == "__main__":
decryptFile()

shikanon wechat
欢迎您扫一扫,订阅我滴↑↑↑的微信公众号!