Rachna College of Engineering and Technology, Gujranwala
(A Constituent College of UET, Lahore)
Department of Computer Science
Lab 4 [Classical Cryptography Implementation and Cryptanalysis] [CLO 2]
Outline:
1. Implement, use, and break the Caesar cipher.
2. Understand modular arithmetic, keyspace exhaustion (brute force), and manual frequency analysis.
3. Understand and use basic python functions required for simple cryptography.
Core Terminology:
Cryptography is the science of securing information by transforming it into an unreadable format, ensuring
that only authorized parties can access the original content. The word comes from the Greek words kryptos
(hidden) and graphein (to write). Cryptography has been practiced for thousands of years — long before
computers existed — and these ancient techniques form the foundation of modern cryptographic theory.
Classical cryptography refers to historical encryption methods that were used before the era of digital
computing. These ciphers rely primarily on two fundamental operations: substitution (replacing characters
with other characters) and transposition (rearranging the order of characters). While classical ciphers are no
longer secure by modern standards, studying them is essential for understanding why modern cryptographic
algorithms are designed the way they are.
Term Definition
Plaintext The original, readable message before encryption. Also called cleartext.
Ciphertext The encrypted, unreadable output produced by applying a cipher to plaintext.
Encryption The process of converting plaintext into ciphertext using a cipher and a key.
Decryption The reverse process — recovering the original plaintext from ciphertext using a key.
Cipher An algorithm or method used to perform encryption and decryption.
A piece of information (number, word, or string) that determines the specific transformation
Key
applied by a cipher.
Cryptanalysis The study of breaking ciphers — recovering plaintext without knowing the key.
Cryptology The combined field of cryptography (designing ciphers) and cryptanalysis (breaking ciphers).
Keyspace The set of all possible keys for a given cipher. Larger keyspace = harder to brute-force.
Substitution A cipher technique that replaces each character in plaintext with another character.
Transposition A cipher technique that rearranges the positions of characters without changing them.
Monoalphabetic A substitution cipher where each plaintext letter always maps to the same ciphertext letter.
A substitution cipher where a plaintext letter can map to different ciphertext letters depending
Polyalphabetic
on position.
Frequency A cryptanalysis technique that exploits the uneven frequency of letters in natural language to
Analysis crack substitution ciphers.
Kerckhoffs'
Security of a cipher must depend only on the key, not the secrecy of the algorithm itself.
Principle
Basic Python Functions:
1
CSC-201L Information Security (Lab) Semester Spring 2026
Function Purpose Example
ord() Converts a character into its ASCII/Unicode ord('A') → 65
number.
chr() Converts an ASCII/Unicode number back into a chr(65) → 'A'
character.
isalpha() Checks if a character is a letter (A–Z or a–z). 'A'.isalpha() → True
upper() Converts all letters in a string to uppercase. "hello".upper() → "HELLO"
lower() Converts all letters in a string to lowercase. "HELLO".lower() → "hello"
append() Adds an element to the end of a list. [Link]('A')
"".join(['H','E','L','L','O'])
join() Combines a list of characters into a single string. → "HELLO"
len() Returns the length of a string/list. len("HELLO") → 5
range() Generates a sequence of numbers for loops. range(5)
% (modulus) Returns remainder after division. 5 % 26 → 5
Counter() (from Counts occurrences of each element in a list or Counter("HELLO") →
collections) string. {'L':2,'H':1,'E':1,'O':1}
most_common( Returns elements sorted by frequency. Counter("HELLO").most_common()
)
len() Returns the number of items in a list/string. len("HELLO") → 5
Lab Tasks:
1. Python Implementation of Ceasor Cipher - Ceasor Cipher: Write a Python program to implement the
Caesar Cipher for encryption and decryption. Add a function Encryption which takes plaintext and shift
key as arguments and returns ciphertext. Add a function Decryption which takes ciphertext and shift key
as arguments and returns plaintext. Make a mechanism which asks the user to choose from encryption or
decryption and then call the respective function. Plaintext/Ciphertext will be entered by user at runtime.
Function Caesar_Encrypt(plaintext, key):
ciphertext = ""
For each character in plaintext:
If character is a letter:
Shift character forward by key positions (circular shift if needed)
Append the new character to ciphertext
Return ciphertext
Function Caesar_Decrypt(ciphertext, key):
plaintext = ""
For each character in ciphertext:
If character is a letter:
Shift character backward by key positions (circular shift if needed)
Append the new character to plaintext
Return plaintext
2. Breaking Caesar Cipher with Brute Force – Brute force technique is a way of finding the desired
output by trying every possible key. Write a python script which use the functions created in previous
task to generate all possible outputs for the given ciphertext for the key range 0 to 25.
3. Breaking Caesar Cipher with Frequency Analysis - Frequency analysis is a cryptanalytic technique
based on the observation that in any natural language, certain letters appear far more often than others. In
English, the most frequent letters are E, T, A, O, I, N (in roughly that order). If we count letter
frequencies in the ciphertext, we can guess which encrypted letter corresponds to 'E' and determine the
key.
Letter E T A O I N S H R D L U
Frequency 12.7% 9.1% 8.2% 7.5% 7.0% 6.7% 6.3% 6.1% 6.0% 4.3% 4.0% 2.8%
2
CSC-201L Information Security (Lab) Semester Spring 2026
Write a python script to implement frequency analysis technique to determine the key.
Algorithm for frequency analysis:
START
FUNCTION FrequencyAnalysis(ciphertext)
CREATE empty list LETTERS
FOR each character C in ciphertext
CONVERT C to uppercase
IF C is an alphabet letter
ADD C to LETTERS
ENDIF
ENDFOR
TOTAL ← number of elements in LETTERS
COUNT frequency of each letter in LETTERS
STORE result in FREQ
PRINT "Letter Frequency in Ciphertext"
PRINT "Total letters analyzed:", TOTAL
SORT FREQ by highest frequency first
FOR each LETTER and COUNT in sorted FREQ
PERCENTAGE ← (COUNT / TOTAL) × 100
BAR ← repeat '#' symbol PERCENTAGE times (approx.)
PRINT LETTER, BAR, PERCENTAGE, COUNT
ENDFOR
MOST_COMMON ← letter with highest frequency in FREQ
PRINT "Most frequent letter in ciphertext:", MOST_COMMON
// Assume most frequent encrypted letter represents 'E'
LIKELY_KEY ← (ASCII(MOST_COMMON) − ASCII('E')) MOD 26
PRINT "Likely Key:", LIKELY_KEY
RETURN LIKELY_KEY
END FUNCTION
MAIN PROGRAM
SAMPLE ← "KBIIL OBZXHBQ QEFP JBPPXDB FK QEB LCCFZB"
KEY_GUESS ← FrequencyAnalysis(SAMPLE)
DECRYPTED_TEXT ← CaesarDecrypt(SAMPLE, KEY_GUESS)
PRINT "Decrypted attempt:", DECRYPTED_TEXT
END
<<Good Luck>>
3
CSC-201L Information Security (Lab) Semester Spring 2026