0% found this document useful (0 votes)
183 views12 pages

Symkey

This document provides instructions for exploring symmetric key encryption modes. It outlines tasks to encrypt and decrypt files using AES with different modes like ECB, CBC, CFB and OFB. The tasks visualize encrypted images and observe how corruption propagates differently during decryption based on the encryption mode used. Students are asked to perform the tasks and record their observations and file sizes in a provided lab report template.

Uploaded by

Hạnh Đăng
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)
183 views12 pages

Symkey

This document provides instructions for exploring symmetric key encryption modes. It outlines tasks to encrypt and decrypt files using AES with different modes like ECB, CBC, CFB and OFB. The tasks visualize encrypted images and observe how corruption propagates differently during decryption based on the encryption mode used. Students are asked to perform the tasks and record their observations and file sizes in a provided lab report template.

Uploaded by

Hạnh Đăng
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/ 12

Exploring Symmetric Key Encryption Modes

Exploring Symmetric Key Encryption Modes


Getting Started
Boot your Linux system or VM. If necessary, log in and then open a terminal window and cd to
the labtainer/labtainer-student directory. The pre-packaged Labtainer VM will start with such a
terminal open for you. Then start the lab:

labtainer symkeylab

Note the terminal displays the paths to two files on your Linux host:
1) This lab manual
2) The lab report template

On most Linux systems, these are links that you can right click on and select “Open Link”. If you
chose to edit the lab report on a different system, you are responsible for copying the
completed files back to the displayed path on your Linux system before using “stoplab” to
stop the lab for the last time.

Familiarize yourself with questions in the lab report template before you start.

In this lab, you will explore the concept of encryption modes and their properties. To perform
this exploration, you will be using an open source encryption product known as OpenSSL. You
will explore the properties of these modes, seeing a visual representation of the state of the
ciphertext, and exploring error propagation during decryption in these various modes.

Note: You can use the commands “man openssl” and “man enc” to get additional help on how
to use the openssl command line tool. Appendix A contains a quick reference sheet of
commonly used Unix commands and appendix B explains hexadecimal.

Appendix D describes the BMP image format, which is helpful to understand when performing
the ECB portion of this lab.

Use the “ll” command to list the content of the directory.

Rev: 2017-02-06 Page 1 of 12


Exploring Symmetric Key Encryption Modes

Task 1: Warm-up: Encrypt and then decrypt a file (any file)


In this task you are only getting used to the syntax of the openssl command.

Do the following:

1. Create a text file with some small amount of content by either using an editor (e.g.,
leafpad) or the combination of the echo command and redirection (‘>’).

2. To encrypt the text file as “cipher.txt” type:

openssl CIPHER -e -in plain.txt -out cipher.txt -K KEY -iv IV

replacing:
 CIPHER with a specific cipher and CBC mode of operation, e.g. aes-128-cbc. (To see all the
options use “man enc”).
 plain.txt is the name of the plain text file you just created
 cipher.txt is the name of the output file which will be the ciphertext
 KEY with a hexadecimal representation of a symmetric key (your choice)
 IV with a hexadecimal representation of an initialization vector (your choice)
Be sure to use the -K option, and not the -k option. The latter may not produce an error, but
may cause issues later in the lab.
3. Observe the ciphertext you’ve created using cat, more, less or leafpad.
4. You should have observed that the encrypted file is gibberish that will not always display
well. To see the actual hex values of the ciphertext, enter the following:
hexdump –C FILENAME
5. List the contents of the directory using the long option (i.e., enter “ls –l”)to see the sizes
for your original file and the encrypted file.
Record the size of the plaintext file in item 1 of the report.

Record the size of the corresponding ciphertext file in item 2 of the report.

Note that item 3 asks a follow-up question. You can answer the question now or
later (which is true for all the follow-up questions).

6. You can decrypt the ciphertext you’ve created using the following command. Be sure to
output to a new plaintext file, and not overwrite the original plaintext.

Rev: 2017-02-06 Page 2 of 12


Exploring Symmetric Key Encryption Modes

openssl CIPHER -d -in cipher.txt -out plainmod.txt -K KEY -iv IV

7. Compare the decrypted plaintext with the original plaintext using the diff command, as
shown below (replacing ORIGINAL and UNENCRYPTED with the file names you used):
diff -a plain.txt plainmod.txt
[Note: if the two files are different then you did something wrong. If diff returns nothing,
then there were no differences, which is what you would expect.]

Task 2: Encryption Modes


In this task, you will explore the differences in security attained by several modes of encryption.
You will use a web browser on your host Linux system (to view files that you create and modify
on the “symkeylab” computer. You will have noticed that the symkeylab computer contains a
“index.html”and “nps-logo.bmp” file. To see the page, start the firefox browser using this
command:
./start_firefox &

You will see the NPS logo, the (nps-logo.bmp file), and a broken link to a modified version of the
logo, (a nps-logo_mod.bmp file that you will be creating).

1. ECB Mode

a. Observe the NPS logo.

When looking at the NPS logo with the eye of a cryptanalyst, what patterns do
you see? Record your observations in item #4 of the report.

b. Encrypt nps-logo.bmp using AES in ECB mode (with the option aes-128-ecb) to
create a ciphertext (Name your ciphertext as “nps-logo_mod.bmp”. [Because
ECB mode does not require an IV, you do not need to provide one.]

c. List the contents of the directory using the long option to see the sizes for the
plaintext logo file and the encrypted file.

Record the size of the plaintext logo file in item 5 of the report.
Record the size of the encrypted logo file in item 6 of the report.

Note: When you refresh the page in the web browser, it fails to display the modified file
because it does not recognize it as a valid image.

Rev: 2017-02-06 Page 3 of 12


Exploring Symmetric Key Encryption Modes

d. You can visualize the ciphertext by tricking an browser into thinking that the
encrypted file is still a valid image file. To do this, you need to do a little “pre-
processing” to make the file viewable.
BMP images have a 54-byte header that informs the image viewer about the image,
such as the image size, and its dimensions. You need to replace the encrypted BMP
header in the ciphertext you created with a valid BMP header from nps-
logo.bmp.

To replace the 54-byte header with one command, do the following


dd if=nps-logo.bmp of=nps-logo_mod.bmp bs=1 count=54 conv=notrunc

e. After modifying the header, go back to the web browser and refresh the web page
to view the encrypted image.

Using item 7 of the report, describe the visualization of the encrypted logo.

Note that item 8 of the report asks a follow-up question.

2. CBC Mode
a. Encrypt nps-logo.bmp again, but this time use CBC mode to
create a ciphertext (with the aes-128-cbc option). [This
time you need to provide an IV.]
b. List the contents of the directory using the long option to see the size of the
ciphertext file you just created.

Record the size of the CBC-encrypted logo file in item 9 of the report.

c. Using the dd command described above, modify the header of the new ciphertext
to have the same 54 bytes as the original BMP file.
d. View the encrypted ciphertext using the web browser refresh.

Using item 10 of the report, describe the visualization of the encrypted logo.

Rev: 2017-02-06 Page 4 of 12


Exploring Symmetric Key Encryption Modes

3. CFB Mode
a. Encrypt the nps-logo.bmp file again, but this time use the CFB mode (with the
aes-128-cfb option). [You need to provide an IV.]
b. List the contents of the directory using the long option to see the size of the
ciphertext file you just created.

Record the size of the CFB-encrypted logo file in item 11 of the report.

c. Using the dd command described above, modify the header of the new ciphertext
to have the same 54 bytes as the original BMP file.
d. View the encrypted ciphertext using the web browser refresh.

Using item 12 of the report, describe the visualization of the encrypted logo.

4. OFB Mode
a. Encrypt the nps-logo.bmp file again, but this time use the OFB mode (with the aes-
128-ofb option). [You need to provide an IV.]
b. List the contents of the directory using the long option to see the size of the
ciphertext file you just created.

Record the size of the OFB-encrypted logo file in item 13 of the report.

c. Using the dd command described above, modify the header of the new ciphertext
to have the same 54 bytes as the original BMP file.
d. View the encrypted ciphertext using the web browser refresh.

Using item 14 of the report, describe the visualization of the encrypted logo.

Note that items 15, 16 and 17 of the report ask follow-up questions.

Rev: 2017-10-26 Page 5 of 12


Exploring Symmetric Key Encryption Modes

Task 3: Error Propagation During Decryption


This task will help you develop an understanding of the ability of various cipher modes of
operation to recover from corruption. You will encrypt a text file in two different modes, change a
single bit in the middle of the encrypted file, decrypt the corrupted file, and then view the effects
of the corrupted bit on the plaintext file.

1. Introduction
a. List the contents of the directory using the long option so you can see the size of the
declare.txt file (in bytes).
Using item 18 of the report, record the size of declare.txt.

Calculate the number of AES blocks it will take to encrypt declare.txt.


Write your answer in item 19 of the report. Show your work.

If each character in a text file is represented in ASCII format, where each


character is represented in one byte, how many characters does it take to fill
up an AES block? Write your answer in item 20 of the report.

2. ECB Mode
a. Encrypt declare.txt using AES-128 in ECB mode (with the aes-128-ecb
option).
b. Open the encrypted file using ghex, as shown below, replacing ENCRYPTEDFILE
with the name you chose:
ghex ENCRYPTEDFILE

c. Select Edit > Goto Byte and enter 0x1230, to take you to near the middle of the
encrypted file. You should end up with “0x1230” as the “Offset:” in the lower left of
the GHex window, and the cursor highlighted on the first of two hex characters
reflecting the value stored at address as in the following figure.

Rev: 2017-10-26 Page 6 of 12


Exploring Symmetric Key Encryption Modes

Note the addresses displayed on the left side may differ on your display, but
the highlighted byte should correspond to address 1230. Change the right-
most digit of this pair such that only one bit is modified. Refer to the table
in Appendix C to determine whether to change the hex digit up or down
one to ensure that only one bit is modified in the ciphertext.
Save the change when you are done and then exit the hex editor.
d. Decrypt the ciphertext without overwriting the original file.
e. Use the diff command (with the “-a” option, as shown earlier) to show where the
original file is different from the decrypted file.
Using item 21 of the report, describe the corruption of the decrypted file.

Note that item 22 is a follow-up question.

Rev: 2017-10-26 Page 7 of 12


Exploring Symmetric Key Encryption Modes

3. CBC Mode

a. Encrypt declare.txt again using AES-128 in CBC mode (with option aes-128-cbc).

b. Use ghex again to modify the ciphertext in the same location such that only one bit
is modified.

c. Decrypt the ciphertext without overwriting the original file.


d. Use the diff command (with the “-a” option, as shown earlier) to show where the
original file is different from the decrypted file.
Using item 23 of the report, describe the corruption of the decrypted file.

Note that item 24 is a follow-up question.

Submission
After finishing the lab, go to the terminal on your Linux system that was used to start the lab and
type:
stoplab symkeylab

If you modified the lab report on a different system, you must copy those completed files into the
directory paths displayed when you started the lab, and you must do that before typing “stoplab”.
When you stop the lab, the system will display a path to the zipped lab results on your Linux
system. Provide that file to your instructor, e.g., via the Sakai site.

Rev: 2017-01-26 Page 8 of 12


Exploring Symmetric Key Encryption Modes

Appendix – Some Unix Commands


cd Change the current directory.
cd destination
With no “destination” your current directory will be changed to your
home directory. If you “destination” is “..”, then your current directory
will be changed to the parent of your current directory.

cp Copy a file.
cp source destination
This will copy the file with the “source” name to a copy with the
“destination” name. The “destination” can also include the path to
another directory.

clea Erase all the output on the current terminal and place the shell prompt at
r the top of the terminal.

less Display a page of a text file at a time in the terminal. (Also see more).
less file
To see another page press the space bar. To see one more line press the
Enter key. To quit at any time press ‘q’ to quit.

ls List the contents and/or attributes of a directory or file


ls location
ls file
With no “location” or “file” it will display the contents of the current
working directory.

man Manual
man command
Displays the manual page for the given “command”. To see another page
press the space bar. To see one more line press the Enter key. To quit
before reaching the end of the file enter ‘q’.

more Display a page of a text file at a time in the terminal. (Also see less).
more file
To see another page press the space bar. To see one more line press the
Enter key. To quit at any time press ‘q’ to quit.

mv Move and/or Rename a file/directory


mv source destination
The “source” file will be moved and/or renamed to the given
“destination.”

pwd Display the present working directory


pwd

Rev: 2017-01-26 Page 9 of 12


Exploring Symmetric Key Encryption Modes

Appendix B – Hex Explained


Hex is short for hexadecimal. Numbers can be represented in many ways. We typically count in
base-10, or decimal. Binary is counting in base-2. Hexadecimal is counting in base-16.

A standard way to describe these notations is to explain that each ‘position’ counts powers in
these bases. Let Xb be a number whose value (X) should be interpreted as being written in base-
b. The decimal number 13 might be written as 1310, for clarity.

Counting by powers:

153410 = 4 x 100+ 3 x 101 + 5x 102+ 1 x 103 = 4 + 30 + 500 + 1000


10112 = 1x 20 + 1x 21+ 0x 22+ 1x 23 = 1 + 2 + 8 = 1110

The symbols we use to count by powers are all values less than base:
 Base 2 uses two symbols (0,1) to count.
 Base 10 uses ten symbols (0,1,2,3,4,5,6,7,8,9) to count.
 Base 16 uses symbols sixteen symbols: 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f.

153416 = 4x 160+ 3x 161+ 5x 162+ 1x 163 = 542810


101116 = 1x 160+ 1x 161+ 0x 162+ 1x 163 = 411310
beef16 = 15 x 160 + 14 x 161 + 14x 162 + 11 x 163 = 4887910

Sometimes we write hex digits using a leading ‘0x’ to signal that the number is in hex, since its
not always clear: 0x1534, 0x1011, 0xbeef, etc.

A single hex digit can represent any four-digit binary value, or 4 bits:

00002 = 0x0, 00012 = 0x1, ..., 11102 = 0xe, 11112 = 0xf

As an aside, just as 8 bits is a byte, we sometimes call 4 bits a nibble. Since a byte is eight bits, this
means a byte is two nibbles, or two hex digits:

0x1f is the byte 000111112

This makes hex a convenient notation for writing bytes of random data, like keys.

Rev: 2017-01-26 Page 10 of 12


Exploring Symmetric Key Encryption Modes

Appendix C – Hex to Binary Translation


The following table provides the translation of hex to binary.

Hex digit Binary


0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
A 1010
B 1011
C 1100
D 1101
E 1110
F 1111

Rev: 2017-01-26 Page 11 of 12


Exploring Symmetric Key Encryption Modes

Appendix D – Pixels and the Bitmapped (BMP) File Format


A pixel (short for picture element) is a small addressable dot on a computer monitor that can be
set to a requested color. One of the important properties of a monitor is its resolution, which is a
measure of the dimensions of a monitor expressed in pixels. For example, a resolution of “1280 x
720” is describing a monitor with 1280 pixels across and 720 pixels down (for a total of 921,600
pixels). Other example resolutions are 2048 x 1152 and 4096 x 2160 (thus the name 4K TVs).

One approach for specifying the color that a pixel should have is the RGB Color Model, whic
theorizes that any color in the rainbow can be expressed as a mixture of Red, Green and Blue. On
a computer, this is generally expressed as an 8-bit (one byte) number representing the amount of
each color, where 0 means nothing, and 255 means all the intensity the monitor can provide.
With 256 possible values for red, green and blue, that means that over 16 million possible colors
can be set for any given pixel. If all three bytes are zero, then the pixel will be black; if all three
bytes are 255, then the pixel will be white.

The bitmapped (BMP) file format is one approach for saving an image. The BMP format specifies
that the first 54 bytes of the file (known as a file header) contain descriptive information that
informs software (e.g., Microsoft Paint) how the image should be displayed. For example, the
dimensions of the image must be described because images can come in various widths and
heights. Following the header, the remainder of the BMP file contains three bytes for every pixel
in the image.

Rev: 2017-01-26 Page 12 of 12

You might also like