[go: up one dir, main page]

0% found this document useful (0 votes)
15 views4 pages

LAB 12.3 ECB v. CBC Modes With Python

The document outlines a project that demonstrates the differences between AES encryption in ECB and CBC modes using a penguin image. It provides step-by-step instructions for encrypting the image in both modes using Python, including necessary commands and file handling. The project requires students to submit screenshots of the encrypted images and email them for grading.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

LAB 12.3 ECB v. CBC Modes With Python

The document outlines a project that demonstrates the differences between AES encryption in ECB and CBC modes using a penguin image. It provides step-by-step instructions for encrypting the image in both modes using Python, including necessary commands and file handling. The project requires students to submit screenshots of the encrypted images and email them for grading.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Proj 8: ECB v. CBC Modes with Python (15 pts.

)
What you need:

A Kali machine works best. I was able to get it working on a Mac


also, but Windows seems hopeless.

Purpose
To use AES in Electronic Code Book (ECB) mode, see it fail to remove
patterns from an image, and demonstrate that Cipher Block Chaining (CBC)
is better.

Download Image
Right-click the penguin image on the right side of this page and save it
somewhere you can find it, such as in your Downloads folder.

The image is named "tux.bmp" and I got it from Wikipedia.

Opening Python
Open a Terminal or Command Prompt window and execute these
commands:

cd Downloads
python

Python should open in Immediate Mode, as shown below. If it does not, you
need to install or repair Python--see the instructions at the top of this page.

Encrypting the Image in ECB Mode


Execute these commands to import the AES functions and create a new "cipher" object. The encryption mode is not specified, so it defaults to
ECB.

from Crypto.Cipher import AES


key = "aaaabbbbccccdddd"
cipher = AES.new(key)

Execute these commands to read the "tux.png" binary file into a variable named "clear". After the second line, press Enter twice.

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


clear = f.read()

Execute this command to encrypt the data in "clear".

ciphertext = cipher.encrypt(clear)
An error message appears, saying the input data must be a multiple of 16 bytes, as shown below.

Execute these commands to see the length of the "clear" data, and the length modulus 16.

len(clear)
len(clear)%16

As shown below, the length mod 16 is 2.

Execute this command to trim a section of "clear" out, skipping the first 64 bytes (the image file header), and the last 2 bytes, saving the result in a
variable named "clear_trimmed".

clear_trimmed = clear[64:-2]

Execute these commands to see the length of the "clear_trimmed" data, and the length modulus 16.

len(clear_trimmed)
len(clear_trimmed)%16

As shown below, the length mod 16 is 0. It's now a multiple of 16 bytes long.

Execute this command to encrypt the data in "clear_trimmed", and put it in a variable named "ciphertext".

ciphertext = cipher.encrypt(clear_trimmed)

Execute these commands to add the first 64 bytes and the last 2 bytes to "ciphertext", and write it to a file named "tux_ecb.bmp".

After the third line, press Enter twice.

ciphertext = clear[0:64] + ciphertext + clear[-2:]


with open("tux_ecb.bmp", "w") as f:
f.write(ciphertext)

Viewing the Encrypted File


In a file browser, navigate to your Downloads folder and double-click the "tux_ecb.bmp" file, as shown below.
The encrypted file still shows a lot of information about the image, as shown below.

Saving a Screen Image


Save a whole-desktop image showing the encrypted Tux file, as shown above.

YOU MUST SUBMIT A FULL-SCREEN IMAGE FOR FULL CREDIT!

Save the document with the filename "YOUR NAME Proj 8a", replacing "YOUR NAME" with your real name.

Encrypting the Image in CBC Mode


Execute these commands to create a new "cipher" object in CBC mode.

CBC mode requires an additional "iv" parameter, as shown below.

iv = "0000111122223333"
cipher = AES.new(key, AES.MODE_CBC, iv)

Execute this command to encrypt the data in "clear_trimmed", and put it in a variable named "ciphertext".

ciphertext = cipher.encrypt(clear_trimmed)

Execute these commands to add the first 64 bytes and the last 2 bytes to "ciphertext", and write it to a file named "tux_cbc.bmp".

After the third line, press Enter twice.

ciphertext = clear[0:64] + ciphertext + clear[-2:]


with open("tux_cbc.bmp", "w") as f:
f.write(ciphertext)
Viewing the Encrypted File
In a file browser, navigate to your Downloads folder and double-click the "tux_cbc.bmp" file, as shown below.

The encrypted file is now random pixels, as shown below.

Saving a Screen Image


Save a whole-desktop image showing the encrypted Tux file, as shown above.

YOU MUST SUBMIT A FULL-SCREEN IMAGE FOR FULL CREDIT!

Save the document with the filename "YOUR NAME Proj 8b", replacing "YOUR NAME" with your real name.

Turning in your Project


Email the images to cnit.141@gmail.com with the subject line: Proj 8 from YOUR NAME.

Posted 9-17-17 by Sam Bowne


Revised 10-16-17

You might also like