我从文件映像中读取二进制文件,每个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
更多推荐
如何从二进制字符串创建图像
发布评论