07.07.2023 Views

Implementing-cryptography-using-python

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 6 ■ Using Cryptography with Images 183

The resulting image will look like Figure 6.8.

Figure 6.8: ECB encrypted plane

Now we will reverse the process. The decryption process will look like the

following:

with open("plane_ecb.bmp", "rb") as f:

byteblock = f.read()

byteblock_trimmed = byteblock[64:-6]

plaintext = cipher.decrypt(byteblock_trimmed)

plaintext = byteblock[0:64] + plaintext + byteblock[-6:]

with open("dplane_ecb.bmp", "wb") as f:

byteblock = f.write(plaintext)

The result, shown in Figure 6.9, will produce a decrypted version that matches

the original version.

Figure 6.9: ECB decrypted plane

Depending on the image you choose, you will have a variable of bytes that

will need to be captured. The following code stores the value in a pad variable.

To simplify the code, I multiplied it by −1 to produce a negative number. The

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!