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

编程入门 行业动态 更新时间:2024-10-23 22:22:30
本文介绍了如何从二进制字符串创建图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我从文件映像中读取二进制文件,每个128字节并使用rsa进行加密。但加密后我无法从二进制文件创建图像。我创建了funtion genarate key rsa。我将二进制转换为int进行加密

来自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这里

解决方案

由于我无法访问KeyRSA和RsaMath,我决定使用

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

解决方案

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 stackoverflow/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 Imageposite(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

更多推荐

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

本文发布于:2023-11-30 11:38:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649833.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   图像

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!