Lab01 - Symmetric Cryptography
Lab01 - Symmetric Cryptography
1. Symmetric Cryptography
A. OVERVIEW
1. Introduction and learning objective
Cryptography plays a vital role in modern digital communication systems. The Oxford
Dictionary1 defines cryptography as "the art of writing or solving codes" with "codes"
elsewhere defined as "a system of prearranged signals, especially used to ensure secrecy
in transmitting messages.” Historically, cryptography focused exclusively on ensuring
private communication between two parties sharing secret information in advance
using code. It was used primarily for military, government, and a few niche industry
applications for centuries.
But cryptography nowadays is much more of a science. We would say that modern
cryptography involves studying mathematical techniques for securing information,
systems, and distributed computations against adversarial attacks. Cryptography has
gone from "an art form that dealt with secret communication for the military" to "a
science to secure systems for ordinary people all across the globe". It deals with
mechanisms for ensuring integrity, techniques for exchanging secret keys, protocols for
authenticating users, electronic auctions and elections, digital currency, and more.
1
https://www.oxfordlearnersdictionaries.com
Lab 1: Symmetric Cryptography
2
The learning objective of this lab is for students to get familiar with the concepts in
secret-key encryption, particularly in classical cryptography and block ciphers. After
finishing the lab, students should gain first-hand experience with encryption
algorithms. Moreover, students may use crypto tools and write simple programs to
encrypt/decrypt messages. This lab will cover the following topics:
1. Classical ciphers
2. Secret-key encryption
3. Encryption modes, IV, and paddings
Concepts
Before beginning, we define some terms. When we’re encrypting a message,
§ The plaintext (p) refers to the original or unencrypted message
§ The ciphertext (C) refers to the coded or encrypted message.
A cipher is therefore composed of two functions:
§ Encryption or Enciphering (E) turns plaintext into ciphertext.
§ Decryption or Deciphering (D) turns a ciphertext back into plaintext.
Written as functions:
C=E(Ke,p)
p=D(Kd,C)
3
where Ke and Kd are encryption and decryption keys, respectively.
Taxonomy
1. Symmetric ciphers (or Secret-key ciphers): Using a single key for encryption and
decryption. It was the only type of encryption in use before the development
of public-key encryption in the 1970s (Figure 2).
2. Asymmetric ciphers (or Public-key ciphers): Use two related keys, a public key
and a private key, to perform complementary operations, such as encryption
and decryption or signature generation and signature verification.
The two basic building blocks of all encryption techniques are substitution and
transposition (Figure 3)
4
Figure 3: Substitution and Transposition example
Block cipher
A block cipher is one in which a block of plaintext is treated as a whole and used to
produce a ciphertext block of equal length (Figure 4). Typically, a block size of 64 or
128 bits is used. As with stream cipher, the two users share a symmetric encryption
key. Using some of the modes of operation, a block cipher can be used to achieve
the same effect as a stream cipher.
5
Figure 5 shows the overall structure of
the AES encryption process. The cipher
takes a plaintext block size of 128 bits or
16 bytes. The key length can be 16, 24,
or 32 bytes (128, 192, or 256 bits). The
algorithm is referred to as AES-128,
AES-192, or AES-256, depending on the
key length.
6
Modes of operation is a technique for enhancing the effect of a cryptographic
algorithm or adapting the algorithm for an application, such as applying a block
cipher to a sequence of data blocks or a data stream. The five modes are intended
to cover a wide variety of applications of encryption for which a block cipher could
be used. To apply a block cipher in a variety of applications, five modes of
operation have been defined by NIST (SP 800-38A):
7
B. LAB TASKS
1. Kickoff: Crack the code
Let’s begin with a straightforward task that does not use any cipher algorithm.
Try to solve the following codes:
a. We need to find the code to open the lock in Figure 8. The lock has a three-
digit pin that satisfies five conditions (hints). Can you crack this code? If
it’s possible, explain how.
8
µ µ µ µ ?
µ µ µ µ µµ
? ? µ µ µµ
? µ µ µ µµ
µµ µµ µµ µµ
Table 1: Find the corresponding encoding for each number
2. Caesar cipher
In this task, you must write an application using your chosen programming
language (without any cryptography libraries) to encrypt and decrypt a message
using Caesar cipher. Your application should satisfy the following requirements:
§ Allow user can encrypt or decrypt message (with the given key)
§ Allow brute-force all possible keys k to find the plaintext of given
ciphertext without its key.
Test your program with a message of at least 100 words and compare the result
with other cryptography tools (like Cryptool 2) to verify. Then use your program
to crack the following ciphertext:
Gurer ner gjb xvaqf bs crbcyr va guvf jbeyq: gubfr jub ner ybbxvat
sbe n ernfba naq gubfr jub ner svaqvat fhpprff. Gubfr jub ner
ybbxvat sbe n ernfba nyjnlf frrxvat gur ernfbaf jul gur jbex vf
abg svavfurq. Naq crbcyr jub svaq fhpprff ner nyjnlf ybbxvat sbe
ernfbaf jul gur jbex pna or pbzcyrgrq.
Do you find any special concerning the key used to encrypt this ciphertext?
9
3. Mono-alphabetic substitution cipher and frequency analysis
(This task is based on a lab in SEED Labs materials by Wenliang Du, Syracuse University.)
You need to find out the original text of the given ciphertext (Mono-alphabetic
ciphertext.txt) using the frequency analysis technique. It is known that the
original text is an English article. Describe how to find the plaintext in detail
(step-by-step).
Please note that you are not allowed to use the automatic mode of any tools (like CrypTool,
dCode, quipqiup,…) to decrypt.
Tips: Using the frequency analysis, you can easily find the plaintext for some of the
characters. For those characters, you may want to change them back to their plaintext, as you
may be able to get more clues. It’s better to use capital letters for plaintext, so for the same
letter, we know which is plaintext and which is ciphertext.
If you use Linux or macOS, you can use the tr command. For example, in the following, we
replace letters a, e and t in in.txt with letters X, G, E, respectively; the results are saved in
out.txt.
$ tr ‘aet’ ‘XGE’ < in.txt > out.txt
You can also use Cryptool or dCode to decrypt it manually by analyzing and replacing letters.
There are many online resources that you can use. Two valuable links as the following:
§ Cryptool Online N-gram analysis: This website can produce the statistics from
sequence), trigram frequencies (3-letter sequence), etc.
10
4. Familiar with the block cipher
We can use the openssl enc command to encrypt / decrypt a file. To see the
manual, you can type man openssl and man enc.
The file pic_original.bmp that include in this lab is a simple picture. We would like
to encrypt this picture, so people without the encryption keys cannot know what
is in the picture. Please encrypt the file using the ECB (Electronic Code Book) and
CBC (Cipher Block Chaining) modes, and then do the following:
1. Let us treat the encrypted picture as a picture and use picture viewing
software to display it. However, For the .bmp file, the first 54 bytes contain
the header information about the picture, we have to set it correctly, so the
encrypted file can be treated as a legitimate .bmp file. We will replace the
header of the encrypted picture with that of the original picture. We can use
the bless hex editor tool (already installed on our VM) to directly modify
binary files. We can also use the following commands to get the header from
p1.bmp, the data from p2.bmp (from offset 55 to the end of the file), and
then combine the header and data together into a new file.
11
2. Display the encrypted picture using a picture viewing program. Can you
derive any useful information about the original picture from the
encrypted picture? Please explain your observations.
3. Select a picture of your choice, repeat the experiment above, and report
your observations.
6. Padding - optional
(This task is based on a lab in SEED Labs materials by Wenliang Du, Syracuse University.)
For block cipher, when the size of plaintext is not a multiple of the block size,
padding may be required. The PKCS#5 padding scheme is widely used by many
block ciphers
1. Use ECB, CBC, CFB, and OFB modes to encrypt a file (you can pick any
cipher). Please report which modes have paddings and which ones do not.
For those that do not need paddings, please explain why.
12
automaticaly remove the padding by default, making it imposible for us to
see the padding. However, the command does have an option called
-nopad, which disables the padding, i.e., during the decryption, the
command will not remove the padded data. Therefore, by looking at the
decrypted data, we can see what data are used in the padding. Please use
this technique to figure out what paddings are added to these files.
It should be noted that padding data may not be printable, so you need to use a hex
tool to display the content. The following example shows how to display a file in hex
format.
C. REQUIREMENTS
You are expected to complete all tasks in section B (Lab tasks). Advanced tasks
are optional, and you could get bonus points for completing those tasks. We
prefer you work in a team of two or three to get the highest efficiency.
§ You need to submit a detailed lab report in .docx (Word Document) format,
using the report template provided on the UIT Courses website.
13
Your submissions must be your own. You are free to discuss with other
classmates to find the solution. However, copying reports is prohibited,
even if only a part of your report. Both reports of the owner and the copier
will be rejected. Please remember to cite any source of the material
(website, book,…) that influences your solution.
Notice: Combine your lab report and all related files into a single ZIP file (.zip),
name it as follow:
StudentID1_StudentID2_ReportLabX.zip
D. REFERENCES
[1] William Stallings, Cryptography and network security: Principles and practice, 7th ed,
Pearson Education, 2017. Chapter 3, chapter 4, chapter 6, chapter 7
§ ASecuritySite-https://asecuritysite.com
§ Cryptopals-https://cryptopals.com
Attention: Don’t share any materials (slides, readings, assignments, labs, etc..) out of our
class without my permission!