基于Python的图像加密算法实现


前言

  1. 虽然理论上只要是对称加密都会被破解,但是这么闲的人应该比较少
  2. 加密速度有些慢【都是for循环,如有矩阵运算的加密算法应该会提升很多】,但是加密结果还不错,基本上把图片都摇匀了
    在这里插入图片描述

安装依赖

pip install numpy matplotlib

算法实现

import numpy as np
import matplotlib.pyplot as plt
import os


class ImageEncryption(object):
    '''
    图像加密算法
    '''
    def __init__(self, code_times=1, shuffle_times=3, a=4, b=2):
        self.code_times = code_times        # 加密次数
        self.shuffle_times = shuffle_times  # 打乱的次数
        self.a, self.b = a, b               # 拉伸因子

    def encode_one(self, img_path):
        '''
        单图加密
        :param img_path:
        :return:
        '''
        # 加载图片为numpy格式
        image = plt.imread(img_path)
        encode_image = None

        # 解密次数
        for times in range(code_times):

            # 创建新图像
            encode_image = np.zeros(shape=image.shape)

            # 计算N
            h, w = image.shape[0], image.shape[1]
            N = h  # 或N=w

            # 遍历像素坐标变换
            for time in range(self.shuffle_times):
                for ori_x in range(h):
                    for ori_y in range(w):
                        # 按照公式坐标变换
                        new_x = (1 * ori_x + self.b * ori_y) % N
                        new_y = (self.a * ori_x + (self.a * self.b + 1) * ori_y) % N
                        encode_image[new_x, new_y, :] = image[ori_x, ori_y, :]

            # 记录当前解密进度
            image = encode_image


        plt.imsave(img_path, encode_image)


    def decode_one(self, img_path):
        '''
        单图解密
        :param img_path:
        :return:
        '''
        # 加载图片为numpy格式
        image = plt.imread(img_path)
        decode_image=None

        # 解密次数
        for times in range(code_times):

            # 创建新图像
            decode_image = np.zeros(shape=image.shape)

            # 计算N
            h, w = image.shape[0], image.shape[1]
            N = h  # 或N=w

            # 遍历像素坐标变换
            for time in range(self.shuffle_times):
                for ori_x in range(h):
                    for ori_y in range(w):
                        # 按照公式坐标变换
                        new_x = ((self.a * self.b + 1) * ori_x + (-self.b) * ori_y) % N
                        new_y = ((-self.a) * ori_x + ori_y) % N
                        decode_image[new_x, new_y, :] = image[ori_x, ori_y, :]

            # 记录当前解密进度
            image = decode_image

        plt.imsave(img_path, decode_image)

    def encode_dir(self, img_dir):
        '''
        针对文件夹下的图片加密
        :param img_dir:
        :return:
        '''
        img_path_list = [os.path.join(img_dir, img_name) for img_name in os.listdir(img_dir)]
        assert img_path_list
        for idx, img_path in enumerate(img_path_list):
            print(f'当前进度:{idx+1} / {len(img_path_list)}')
            self.encode_one(img_path)

    def decode_dir(self, img_dir):
        '''
        针对文件夹下的图片解密
        :param img_dir:
        :return:
        '''
        img_path_list = [os.path.join(img_dir, img_name) for img_name in os.listdir(img_dir)]
        assert img_path_list
        for idx, img_path in enumerate(img_path_list):
            print(f'当前进度:{idx + 1} / {len(img_path_list)}')
            self.decode_one(img_path)


if __name__ == '__main__':

    # 加密因子
    code_times = 2      # 加密次数【别给太大,影响加密速度】
    shuffle_times = 3   # 打乱的次数
    a, b = 10, 10       # 拉伸因子
    # 舒适化加密类
    iec = ImageEncryption(code_times=code_times, shuffle_times=shuffle_times, a=a, b=b)


    # # 针对单图加密解密 ----------------
    # 图片路径
    img_path = '../狗砸1.png'
    # 加密
    iec.encode_one(img_path=img_path)
    # 解密
    iec.decode_one(img_path=img_path)
    # --------------------------------

    # 针对文件夹中的图加密解密 --------
    # 图片文件夹
    img_dir = '../img'
    # 加密
    iec.encode_dir(img_dir=img_dir)
    # 解密
    iec.decode_dir(img_dir=img_dir)
    # -----------------------------


ps:不要用它加密一些奇奇怪怪的东西啊(狗头)


版权声明:本文为weixin_43721000原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。