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
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()
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.
Where is variable img
cv2.imwrite('frame0.jpg',img)
this also overwrites img with a boolean :
img=cv2.imwrite('frame0.jpg',img)
@vijaya what do you expect to happen ? you cannot save arbitrary data as an image
I want image to be encrypted and then decrypted.
for encryption using aes the image array has to be converted to bytes. after encryption the encrypted data must saved a jpg file.
Again. Actually, where is variable img? This should be
cv2.imwrite('frame0.jpg',image)
instead ofcv2.imwrite('frame0.jpg',img)
?@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 is absolutely by using open(). It can be solved. There are 2 types.... shape or filename. You cannot used
imread()
.