Python 实现图像只能由特定软件打开(或者是制作一种新格式)

加密后图像只能通过在指定软件中的指定处理过程解密后才能显示正确的图像数据
示例代码:
思路:读取图像二进制数据后,在图像头部插入新的bytes类型数据,新的数据是打不开的,只能将插入头部的数据清除掉图像才能正常显示;实际生产中可以将新的bytes类型数据随机插入到源图像数据中,达到增加破解难度的作用

# !/usr/bin/python3
# -*- coding:utf-8 -*-
"""
@author: [email protected]
@file: test_img.py
@time: 2023/9/3 23:22
@desc: 

"""
def img_encode(img_file,secret):
    """
    图像加密
    :param img_file:
    :param secret:
    :return:
    """
    with open(img_file,"rb")as fp:
        return secret+fp.read()

def img_decode(encode_img_data,secret):
    """
    图像解密
    :param img_decode_file:
    :param encode_img_data:
    :param secret:
    :return:
    """

    return encode_img_data[len(secret):]


if __name__ == '__main__':
    img_file = R"F:\Project\GIT\pythondevelopmenttools\tests\test_selenium\main.jpg"
    secret = b"ak47"
    img_date = img_encode(img_file,secret)
    decode_img_data = img_decode(img_date,secret)
    with open("./1.png","wb")as fp:
        fp.write(decode_img_data)





你可能感兴趣的:(无用良品,python,开发语言)