前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >通过jpg图片隐藏文件

通过jpg图片隐藏文件

作者头像
机械视角
发布2019-10-23 11:16:17
1.4K0
发布2019-10-23 11:16:17
举报
文章被收录于专栏:TensorbytesTensorbytesTensorbytes

比如我们现在有一个视频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()
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-03-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档