如何从二进制字符串创建图像

2024-10-03 11:24:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我从文件映像中读取二进制文件,每个128字节并使用rsa加密。但是加密后我不能从二进制文件创建图像。我创建了函数generate key rsa。我把二进制转换成int进行加密

from PIL import Image
import KeyRSA
import RsaMath

pub, piv = KeyRSA.GenerateRsaKey(size=1024)
n, e = pub

with open("hinh.png", "rb") as infile:
    data = infile.read()
step = 128
cipher_data = []
for i in range(0, len(data), step):
    value = data[i:i+step]
    m = RsaMath.OS2IP(value)
    c = pow(m, e, n)
    cipher_data.append(RsaMath.I2OSP(c, 128))

cipher_data = "".join(cipher_data)

im = Image.open("hinh.png")
W, H = im.size
img = Image.frombytes("RGB", (W,H), cipher_data)
img.show()e here

Tags: imageimportdatasizepngstep二进制open
1条回答
网友
1楼 · 发布于 2024-10-03 11:24:37

因为我不能访问KeyRSA和RsaMath,所以我决定使用pycrypto包。原始RSA加密是安全的,而且速度很慢,所以这段代码使用AES,这是一种快速的,在128字节块的块模式下使用非常安全。在

下面的代码不是从文件中读取图像,而是生成一个简单的双色图像。然后对其进行加密,将加密的数据转换为图像,并将其显示并保存为PNG。然后解密加密的数据以恢复原始图像。在

加密过程是使用128字节块执行的,因此,如果您真的想这样做,您可以轻松地将其调整为适合您的RSA加密算法。但是,在一个步骤中执行加密更为有效;我添加了一个注释行,说明了如何进行加密。在

#!/usr/bin/env python3

''' Create a simple image and encrypt it with AES

    See https://stackoverflow.com/q/44314026/4014959

    Written by PM 2Ring 2017.06.02
'''

from PIL import Image
from Crypto.Cipher import AES

def color_square(size, colors):
    ''' Make a square that fades diagonally
        from the 1st color to the 2nd color
    '''
    m = 255.0 / (2 * size - 2)
    r = range(size)
    mask = bytes(int(m * (x + y)) for y in r for x in r)
    mask = Image.frombytes('L', (size, size), mask)
    imgs = [Image.new('RGB', (size, size), color=c) for c in colors]
    return Image.composite(imgs[0], imgs[1], mask)

# Create a simple image
size = 128
img = color_square(size, ('red', 'blue'))
img.show()

# Extract the raw pixel data from the image
src_data = img.tobytes()
print('Original length:', len(src_data), size*size*3)

# Create an AES cipher object
key = b'This is a key123'
iv = b'This is an IV456'
crypto = AES.new(key, AES.MODE_CBC, iv)

# Encrypt the data in 128 byte chunks
blocks = []
for i in range(0, len(src_data), 128):
    blocks.append(crypto.encrypt(src_data[i:i+128]))
cipher_data = b''.join(blocks)

# We could actually encrypt it in one step with
#cipher_data = crypto.encrypt(src_data)
print('Encoded length', len(cipher_data))

# Convert the encrypted data to an image & display it
img = Image.frombytes("RGB", (size, size), cipher_data)
img.show()
img.save('redblue_AES.png')

# We need a fresh AES cipher object to do the decoding
crypto = AES.new(key, AES.MODE_CBC, iv)
decoded_data = crypto.decrypt(cipher_data)
print('Decoded length', len(decoded_data))

# Convert the decrypted data to an image & display it
img = Image.frombytes("RGB", (size, size), decoded_data)
img.show()
img.save('redblue.png')

给你红蓝.png还有红蓝_埃斯.png在

redblue.png - the recovered originalredblue_AES.png - the encrypted version

相关问题 更多 >