ISS HW1 Answers
ISS HW1 Answers
(Homework 1)
Q1. (20 pts) Write a program (in any programming language) that would highlight a brute force attack
on the Caesar Cipher. Your program should accept as input the ciphertext and produce a tabular listing
of all possible plaintexts. Provide the opposite functionality, i.e., given the plaintext, produce all possible
ciphertexts for all key values. Test your program against the following two ciphertexts. BZDRZQ'R VHED
LTRS AD ZANUD RTROHBHNM KENKMOC PYBDEXK TEFKD (ciphertexts obtained from
http://simonsingh.net/cryptography/cryptograms/).
---
def generate_all_encryptions(plaintext):
print("\nAll Possible Caesar Cipher Encryptions:")
print(f"{'Key':<5} {'Ciphertext'}")
print("-" * 40)
for key in range(1, 26): # keys 1 to 25
ciphertext = caesar_cipher(plaintext, key)
print(f"{key:<5} {ciphertext}")
if __name__ == "__main__":
print("Caesar Cipher Brute Force Tool")
mode = input("Choose mode (encrypt/decrypt): ").strip().lower()
if mode == "decrypt":
cipher_input = input("Enter the ciphertext: ")
brute_force_decrypt(cipher_input)
elif mode == "encrypt":
plain_input = input("Enter the plaintext: ")
generate_all_encryptions(plain_input)
else:
print("Invalid mode. Choose 'encrypt' or 'decrypt'.")
---
Sample Run
Input:
Output:
Key Plaintext
----------------------------------------
1 Vjku ku c vguv oguucig.
2 Uijt jt b uftu nfttbhf.
3 This is a test message.
...
Input:
Output:
Key Ciphertext
----------------------------------------
1 ifmmp xpsme
2 jgnnq yqtnf
3 khoor zruog
...
---
Q2. (20 pts) Implement the Vigenere (polyalphabetic) cipher (in any programming language). Test
encryption and decryption operations attempting different keyword options including the keyword
deception.
Here is an implementation of the Vigenère Cipher in Python, with both encryption and
decryption functions. It also includes tests using various keywords, including
"deception".
---
print(f"Keyword: {key}")
print(f"Encrypted: {encrypted}")
print(f"Decrypted: {decrypted}")
print("-" * 60)
---
Keyword: KEY
Encrypted: Lixkvi vli Nkrlcpfiai, wc csh! Dvs neig xleb fujy, xli jpedw xleb jeksr!
Decrypted: Beware the Jabberwock, my son! The jaws that bite, the claws that catch!
------------------------------------------------------------
Keyword: LEMON
Encrypted: Miekvv xlk Qehlilript, pc cwv! Vrs ptmy lixk gwjx, xsx npiay lixk gpchw!
Decrypted: Beware the Jabberwock, my son! The jaws that bite, the claws that catch!
------------------------------------------------------------
Keyword: DECEPTION
Encrypted: Ejsghv xsm Hiqgoobawj, pw ikz! Hvi nadq itnx flzn, xqg peibz itnx hmvpu!
Decrypted: Beware the Jabberwock, my son! The jaws that bite, the claws that catch!
------------------------------------------------------------
Keyword: VIGENERE
Encrypted: W
Q3. (20 pts) Write a program (in any programming language) that would show whether DES and AES
exhibit a strong Avalanche effect? Provide snapshots of program input and output that answer the
above question. Provide an explanation of the generated output.
---
The Avalanche effect means that a small change in input (like flipping 1 bit) causes a
significant change in output (typically 50% of bits flipped).
---
Requirements
---
def test_avalanche(cipher_name):
print(f"--- Testing {cipher_name} ---")
if cipher_name == 'DES':
key = b'12345678' # 8 bytes
cipher_func = lambda k: DES.new(k, DES.MODE_ECB)
block_size = 8
elif cipher_name == 'AES':
key = b'1234567890abcdef' # 16 bytes
cipher_func = lambda k: AES.new(k, AES.MODE_ECB)
block_size = 16
else:
print("Unsupported cipher.")
return
cipher = cipher_func(key)
bin1 = to_binary(ciphertext1)
bin2 = to_binary(ciphertext2)
print(f"Ciphertext 1: {ciphertext1.hex()}")
print(f"Ciphertext 2: {ciphertext2.hex()}")
print(f"Bit Difference: {bit_diff} / {len(bin1)} bits ({bit_diff / len(bin1) * 100:.2f}%)\n")
# Run for both ciphers
test_avalanche('DES')
test_avalanche('AES')
---