How to create image from binary string

六月ゝ 毕业季﹏ 提交于 2020-01-07 08:02:06

问题


I read binary from file image, each 128-bytes and encrypt using rsa. but after encrypt i can't create image from binary. I created funtion genarate key rsa. I convert binary to int for encrypt

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

回答1:


Since I don't have access to KeyRSA and RsaMath I decided to use the pycrypto package. Raw RSA encryption is not safe, and it's slow, so instead this code uses AES, which is fast, and quite safe to use in block mode with 128 byte blocks.

Rather than reading an image from a file, the code below generates a simple two color image. It then encrypts it, converts the encrypted data to an image, which it displays and saves as a PNG. It then decrypts the encrypted data to recover the original image.

The encryption process is performed using 128 byte chunks, so you can easily adapt it to your RSA encryption algorithm, if you really want to do that. However, it's more efficient to perform the encryption in one step; I've included a commented-out line that shows how to do that.

#!/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')

Here are redblue.png and redblue_AES.png



来源:https://stackoverflow.com/questions/44314026/how-to-create-image-from-binary-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!