The image is named "tux.bmp" and I got it from Wikipedia.
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.
from Crypto.Cipher import AES
key = "aaaabbbbccccdddd"
cipher = AES.new(key, AES.MODE_ECB)
Execute these commands to read the "tux.bmp" 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)
The encrypted file still shows a lot of information about the image, as shown below.
(The image on Windows is twisted diagonally but still recognizable.)
with open("tux_ecb.bmp", "rb") as f:
bytes = f.read()
Execute these commands to show the last four bytes of the encrypted file in hexadecimal. After the second line, press Enter twice.
for c in bytes[-4:]:
print hex(ord(c)),
Four hexadecimal values appear, as shown below.
In this image I have redacted the actual hexadecimal
values.
Use the form below to put your name on the WINNERS PAGE.
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)
The encrypted file is now random pixels, as shown below.
with open("tux_cbc.bmp", "rb") as f:
bytes = f.read()
Execute these commands to show the last four bytes of the encrypted file in hexadecimal. After the second line, press Enter twice.
for c in bytes[-4:]:
print hex(ord(c)),
Four hexadecimal values appear, as shown below.
In this image I have redacted the actual hexadecimal
values.
Use the form below to put your name on the WINNERS PAGE.