Answer the question
In order to leave comments, you need to log in
How to encrypt a picture in AES and get a picture with chaotic pixels at the output?
The essence of the problem:
You need to encrypt the picture in AES and get back a chaotic set of pixels.
How I do it:
I read the picture by bytes and write it to a txt file, encode this file and write the encrypted bytes to the picture, but when I start the picture it says: "Invalid permission"
code:
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
def read ():
fileFirst = input('Enter file name: ')
with open(fileFirst, 'rb') as byteImg:
byte_text = byteImg.read()
with open('byte_img_text.txt','wb') as re:
re .write(byte_text)
print(byte_text)
def encrypt():
with open('byte_img_text.txt', 'rb') as txtToEntxt:
TextForEncrypt = txtToEntxt.read()
cipher = AES.new(key, AES.MODE_CTR)
byte_text_en = cipher.encrypt(TextForEncrypt)
with open('byte_img_text_en.txt ', 'wb') as newF:
newF.write(byte_text_en)
def write():
with open('byte_img_text_en.txt', 'rb') as reWrite:
text = reWrite.read()
with open('end.jpg ', 'wb') as wrWrite:
wrWrite.write(text)
print(text)
#read()
#encrypt()
write()
Answer the question
In order to leave comments, you need to log in
good tasks are given to students, however. Respect to the teacher - students to think and study.
We proceed from the assumption that png is not compressed.
open the original png through the appropriate library. create target png.
then we take the colors of the first pixel of the source png - encrypt - write to the colors of the first pixel of the target file.
and so on until the end.
close png.
we get the correct png format where the colors of each pixel are encrypted.
and also in reverse.
the algorithm is certainly written with a pitchfork on the water :) but you, a student, need a teacher.
Actually, the system writes to you correctly :)
The image file, in addition to the data on the color of the pixels, also has a bunch of data described by the format, i.e. stores inside itself the height, width, compression method, additional information like the shooting coordinates and the name of the camera ...
i.e. by encrypting the entire file you have made this data unreadable.
how to solve the problem: the first option was described to you, byte-by-byte encrypt ONLY the region of pixels.
The second option is to create a new file, write there a header for png with all the necessary fields, and then encrypt your entire file.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question