B
B
bcubak2019-12-09 22:21:38
Python
bcubak, 2019-12-09 22:21:38

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

4 answer(s)
P
pfg21, 2019-12-09
@pfg21

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.

H
Homo Ludens, 2019-12-09
@HLudens

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.

X
xmoonlight, 2019-12-10
@xmoonlight

You need to encrypt the canvas, not the structure ...
Ppc ..

T
towin, 2019-12-10
@towin

The easiest way to do this is with a BMP file, it has a simple header structure, the data is not compressed.
You need to encrypt the data separately from the header. The output will be a normal picture, only instead of an image - garbage from pixels.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question