After encryption of an image array using aes, when i try to save the encrypted image in jpg format using imwrite then the jpg image will have 0 bytes and type of the image becomes none

asked 2020-01-18 18:34:34 -0600

vijaya gravatar image

updated 2020-01-19 04:45:29 -0600

Code:

def encrypt_frames(arr,key_encrypt,iv_encrypt):
      cipher_text=np.array([])

      cipher = AES.new(key_encrypt, AES.MODE_CBC,iv_encrypt)
      cipher_text=cipher.encrypt(pad(arr,AES.block_size))
       return(cipher_text)

def main():
     video_file='thumb0001.jpg'
     image=cv2.imread(video_file)
     print("Image Shape",image.shape)
     vid_arr_bytes=np.array([1,720,1280,3])
     vid_arr_bytes=image.tobytes()
     key=get_random_bytes(16)
     iv=get_random_bytes(16)
     input_for_decrypt=encrypt_frames(vid_arr_bytes,key,iv)
     img=np.frombuffer(input_for_decrypt,dtype=np.uint8)
     cv2.imwrite('frame0.jpg',img)
if __name__=="__main__":
    main()
edit retag flag offensive close merge delete

Comments

your aes encrypted data is not a proper numpy 2d array.

you cannot save this as an image, it is no more one.

also: lossy jpg encoding will totally wreck it.

berak gravatar imageberak ( 2020-01-18 18:59:43 -0600 )edit
1

Where is variable imgcv2.imwrite('frame0.jpg',img)

supra56 gravatar imagesupra56 ( 2020-01-18 21:26:54 -0600 )edit

this also overwrites img with a boolean : img=cv2.imwrite('frame0.jpg',img)

berak gravatar imageberak ( 2020-01-19 02:32:41 -0600 )edit
1

@vijaya what do you expect to happen ? you cannot save arbitrary data as an image

berak gravatar imageberak ( 2020-01-19 03:58:38 -0600 )edit

I want image to be encrypted and then decrypted.

vijaya gravatar imagevijaya ( 2020-01-19 04:06:38 -0600 )edit

for encryption using aes the image array has to be converted to bytes. after encryption the encrypted data must saved a jpg file.

vijaya gravatar imagevijaya ( 2020-01-19 04:07:47 -0600 )edit

Again. Actually, where is variable img? This should be cv2.imwrite('frame0.jpg',image) instead of cv2.imwrite('frame0.jpg',img)?

supra56 gravatar imagesupra56 ( 2020-01-19 04:22:42 -0600 )edit
1

@vijaya it does not make any sense to do it this way.

what you can do is: take an existing jpg image file, load it into memory (using open(), not imread() !) and encrypt that.

but again, this is no more an image, even if you save it as jpg, and you cannot use imwrite()

berak gravatar imageberak ( 2020-01-19 04:25:31 -0600 )edit

@berak is absolutely by using open(). It can be solved. There are 2 types.... shape or filename. You cannot used imread().

supra56 gravatar imagesupra56 ( 2020-01-19 06:47:15 -0600 )edit