复合混沌加密(python版)

复合混沌加密

以下代码使用python实现的复合混沌加密一张图片,该加密算法原理就不讲了,到目前为止破解的人还没出现,先看一看效果图。
在这里插入图片描述
图片从左到右分别为原始图像、加密图像、以及解密后的图像,下面为代码。(注意,可加密的图片格式可以为png,bmp,jpg,其它的没有测试,但是可以顺利解密的是png,bmp。jpg格式的图片解密会有不一样的效果,想看的自己可以试一下!)
所用到的库有 opencv、math、numpy

#!/usr/bin/env python
# _*_ coding:utf-8 _*_

"""
@Author       :  AI-Zhang
@Version      : V1.0
@E-Mail       : 1415984778@qq.com
@File         : Cryption.py
@CreateTime   : 2021/3/13
@Description  : Encryption and Decryption of image
@ModifyTime   : 2021/3/13
@company      : CSUFT
"""

import cv2
import math
import numpy as np


def int2bin8(x):                               # 整型转8位二进制
    result="";
    for i in range(8):
        y=x&(1)
        result+=str(y)
        x=x>>1
    return result[::-1]

def int2bin16(x):                              # 整型转8位二进制
    result="";
    for i in range(16):
        y=x&(1)
        result+=str(y)
        x=x>>1
    return result

def Encryption(img,j0,g0,x0,EncryptionImg):
    x = img.shape[0]
    y = img.shape[1]
    c = img.shape[2]
    g0 = int2bin16(g0)
    for s in range(x):
        for n in range(y):
            for z in range(c):
                m = int2bin8(img[s][n][z])                   # 像素值转八位二进制
                ans=""
                print("ok")
                for i in range(8):
                    ri=int(g0[-1])                           # 取手摇密码机最后一位ri
                    qi=int(m[i])^ri                          # 与像素值异或得qi
                    xi = 1 - math.sqrt(abs(2 * x0 - 1))      # f1(x)混沌迭代
                    if qi==0:                                # 如果qi=0,则运用x0i+x1i=1;
                        xi=1-xi;
                    x0=xi                                    # xi迭代
                    t=int(g0[0])^int(g0[12])^int(g0[15])     # 本源多项式x^15+x^3+1
                    g0=str(t)+g0[0:-1]                       # gi迭代
                    ci=math.floor(xi*(2**j0))%2              # 非线性转换算子
                    ans+=str(ci)
                re=int(ans,2)
                EncryptionImg[s][n][z]=re                    # 写入新图像

def Decryption(EncryptionImg, j0, g0, x0, DecryptionImg):
    x = EncryptionImg.shape[0]
    y = EncryptionImg.shape[1]
    c = EncryptionImg.shape[2]
    g0 = int2bin16(g0)
    for s in range(x):
        for n in range(y):
            for z in range(c):
                cc = int2bin8(img[s][n][z])
                ans = ""
                print("no")
                for i in range(8):
                    xi = 1 - math.sqrt(abs(2 * x0 - 1))
                    x0 = xi
                    ssi = math.floor(xi * (2 ** j0)) % 2
                    qi=1-(ssi^int(cc[i]))
                    ri = int(g0[-1])
                    mi=ri^qi
                    t = int(g0[0]) ^ int(g0[12]) ^ int(g0[15])
                    g0 = str(t) + g0[0:-1]
                    ans += str(mi)
                re = int(ans, 2)
                DecryptionImg[s][n][z] = re


if __name__ == "__main__":
    img = cv2.imread("D:/pycharmproject/network_security/2.bmp", 1)                    # 读取原始图像
    cv2.imshow("img", img)                                                             # 显示原图

    EncryptionImg = np.zeros(img.shape, np.uint8)
    Encryption(img,10,30,0.123345,EncryptionImg)                                       # 加密
    cv2.imwrite("D:/pycharmproject/network_security/EncryptionImg2.bmp",EncryptionImg)  # 保存
    cv2.imshow("EncryptionImg", EncryptionImg)                                        # 显示

    img = cv2.imread("D:/pycharmproject/network_security/EncryptionImg2.bmp", 1)        # 读取加密图像
    DecryptionImg = np.zeros(img.shape, np.uint8)
    Decryption(img, 10, 30, 0.123345, DecryptionImg)                                   # 解密
    cv2.imwrite("D:/pycharmproject/network_security/DecryptionImg2.bmp", DecryptionImg) # 保存
    cv2.imshow("DecryptionImg ", DecryptionImg)                                        # 显示

    cv2.waitKey(0)


  • 15
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
Python混沌加密文本是一种基于混沌理论的加密算法,它使用混沌序列和异或运算来加密文本。具体实现过程如下: 1. 生成混沌序列c(i)。可以使用Lorenz系统或Chen系统等混沌系统生成。 2. 将待加密的文本转换为比特流b(i)。 3. 将混沌序列c(i)和比特流b(i)做异或运算,得到加密后的序列W(i)。 4. 将加密后的序列W(i)转换为文本输出。 下面是一个Python实现的例子: ```python import numpy as np # 生成混沌序列 def chaos_sequence(x0, y0, z0, a, b, c, n): x, y, z = x0, y0, z0 result = [] for i in range(n): x, y, z = y * z - b * x, x * z - c * y, x * y - a * z result.append(x % 256) return result # 将文本转换为比特流 def text_to_bits(text): bits = [] for char in text: bits.extend([int(bit) for bit in bin(ord(char))[2:].zfill(8)]) return bits # 将比特流转换为文本 def bits_to_text(bits): chars = [] for i in range(0, len(bits), 8): char = chr(int(''.join([str(bit) for bit in bits[i:i+8]]),2)) chars.append(char) return ''.join(chars) # 加密文本 def encrypt(text, x0, y0, z0, a, b, c): bits = text_to_bits(text) chaos = chaos_sequence(x0, y0, z0, a, b, c, len(bits)) encrypted_bits = [bit ^ chaos[i] for i, bit in enumerate(bits)] return bits_to_text(encrypted_bits) # 解密文本 def decrypt(text, x0, y0, z0, a, b, c): bits = text_to_bits(text) chaos = chaos_sequence(x0, y0, z0, a, b, c, len(bits)) decrypted_bits = [bit ^ chaos[i] for i, bit in enumerate(bits)] return bits_to_text(decrypted_bits) # 测试 text = 'Hello, world!' x0, y0, z0 = 1, 2, 3 a, b, c = 10, 28, 8/3 encrypted_text = encrypt(text, x0, y0, z0, a, b, c) decrypted_text = decrypt(encrypted_text, x0, y0, z0, a, b, c) print('原文:', text) print('加密后:', encrypted_text) print('解密后:', decrypted_text) ```
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值