From 376f2ccf5e8c0e82ce00a4842b281b6220cc1f75 Mon Sep 17 00:00:00 2001 From: Student Date: Tue, 5 Aug 2025 10:24:18 +0530 Subject: [PATCH 1/2] added lab2 questions --- IS/Lab/Lab2/des_vs_aes256.py | 87 ++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/IS/Lab/Lab2/des_vs_aes256.py b/IS/Lab/Lab2/des_vs_aes256.py index e69de29..a5e45c7 100644 --- a/IS/Lab/Lab2/des_vs_aes256.py +++ b/IS/Lab/Lab2/des_vs_aes256.py @@ -0,0 +1,87 @@ +from Crypto.Cipher import DES, AES +from Crypto.Util.Padding import pad, unpad +import time +from bokeh.plotting import figure, show +from bokeh.models import ColumnDataSource +from bokeh.io import output_file + + +def benchmark_encryption_decryption(cipher_func, data, key): + # Measure encryption time + start_time = time.time() + encrypted = cipher_func(data, key) + encrypt_time = time.time() - start_time + + # Measure decryption time + start_time = time.time() + decrypted = cipher_func(encrypted, key, decrypt=True) + decrypt_time = time.time() - start_time + + return encrypt_time, decrypt_time + + +def des_cipher(data, key, decrypt=False): + cipher = DES.new(key.encode('utf-8'), DES.MODE_ECB) + if decrypt: + return unpad(cipher.decrypt(data), DES.block_size).decode('utf-8') + else: + padded_data = pad(data.encode('utf-8'), DES.block_size) + return cipher.encrypt(padded_data) + + +def aes_cipher(data, key, decrypt=False): + cipher = AES.new(key.encode('utf-8'), AES.MODE_ECB) + if decrypt: + return unpad(cipher.decrypt(data), AES.block_size).decode('utf-8') + else: + padded_data = pad(data.encode('utf-8'), AES.block_size) + return cipher.encrypt(padded_data) + + +def main(): + message = "Performance Testing of Encryption Algorithms" + + # Test with different key sizes + des_key = "12345678" # 8 bytes for DES + aes_key = "12345678901234567890123456789012" # 32 bytes for AES-256 + + # Benchmark DES + des_encrypt_time, des_decrypt_time = benchmark_encryption_decryption(des_cipher, message, des_key) + + # Benchmark AES-256 + aes_encrypt_time, aes_decrypt_time = benchmark_encryption_decryption(aes_cipher, message, aes_key) + + # Print results + print("Performance Results:") + print("-" * 40) + print(f"DES Encryption: {des_encrypt_time:.9f} seconds") + print(f"DES Decryption: {des_decrypt_time:.9f} seconds") + print(f"AES-256 Encryption: {aes_encrypt_time:.9f} seconds") + print(f"AES-256 Decryption: {aes_decrypt_time:.9f} seconds") + + # Create Bokeh visualization + algorithms = ['DES', 'AES-256'] + encrypt_times = [des_encrypt_time, aes_encrypt_time] + decrypt_times = [des_decrypt_time, aes_decrypt_time] + + # Create the plot + p = figure(x_range=algorithms, height=400, title="Encryption/Decryption Performance Comparison", + toolbar_location=None, responsive=True) + + p.vbar(x='algorithm', top='encrypt_time', width=0.4, color='blue', alpha=0.7, legend_label="Encryption", + source=ColumnDataSource(data=dict(algorithm=algorithms, encrypt_time=encrypt_times))) + p.vbar(x='algorithm', top='decrypt_time', width=0.4, color='red', alpha=0.7, legend_label="Decryption", + source=ColumnDataSource(data=dict(algorithm=algorithms, decrypt_time=decrypt_times))) + + p.legend.location = "top_right" + p.xaxis.axis_label = "Algorithm" + p.yaxis.axis_label = "Time (seconds)" + p.title.text_font_size = "14px" + + # Save and show the plot + output_file("encryption_performance.html") + show(p) + + +if __name__ == '__main__': + main() \ No newline at end of file From de98ad42680e4c52cda7b069386d2d62c9dd99ff Mon Sep 17 00:00:00 2001 From: Student Date: Tue, 19 Aug 2025 08:32:10 +0530 Subject: [PATCH 2/2] fixed autokey --- IS/Lab/Lab1/q2_autokey.py | 35 ++++++++++++++--------------------- IS/Lab/Lab3/DiffieHellman.py | 11 ----------- 2 files changed, 14 insertions(+), 32 deletions(-) diff --git a/IS/Lab/Lab1/q2_autokey.py b/IS/Lab/Lab1/q2_autokey.py index 8771e65..0ae235a 100644 --- a/IS/Lab/Lab1/q2_autokey.py +++ b/IS/Lab/Lab1/q2_autokey.py @@ -25,34 +25,27 @@ def vigenere_de(ctext, vk): result.append(ch) return ''.join(result) - def autokey_en(ptext, ak): - result = [] - ptext = ptext.upper() - current_key = ak - for ch in ptext: + k = ord(ak.upper()) if isinstance(ak, str) else ak + out = [] + for ch in ptext.upper(): if ch.isalpha(): - shift = (current_key - ord('A')) % 26 - cipher_char = chr((ord(ch) - ord('A') + shift) % 26 + ord('A')) - result.append(cipher_char) - current_key = ord(cipher_char) + out_ch = chr((ord(ch) - 65 + (k - 65)) % 26 + 65) + out.append(out_ch); k = ord(ch) else: - result.append(ch) - return ''.join(result) + out.append(ch) + return ''.join(out) def autokey_de(ctext, ak): - result = [] - ctext = ctext.upper() - current_key = ak - for ch in ctext: + k = ord(ak.upper()) if isinstance(ak, str) else ak + out = [] + for ch in ctext.upper(): if ch.isalpha(): - shift = (current_key - ord('A')) % 26 - plain_char = chr((ord(ch) - ord('A') - shift) % 26 + ord('A')) - result.append(plain_char) - current_key = ord(plain_char) + p = chr((ord(ch) - 65 - (k - 65)) % 26 + 65) + out.append(p); k = ord(p) else: - result.append(ch) - return ''.join(result) + out.append(ch) + return ''.join(out) def operator(argument,ptext,ak,vk): match argument: diff --git a/IS/Lab/Lab3/DiffieHellman.py b/IS/Lab/Lab3/DiffieHellman.py index dcac7e0..e1eb076 100644 --- a/IS/Lab/Lab3/DiffieHellman.py +++ b/IS/Lab/Lab3/DiffieHellman.py @@ -38,12 +38,6 @@ def int_to_fixed_length_bytes(value: int, length_bits: int) -> bytes: def generate_keypair(p: int, g: int, private_bits: int = 256) -> tuple[int, int]: - """ - Generate a Diffie-Hellman private/public key pair. - - - private_bits controls the size of the secret exponent for performance and security. - 256 bits is a common choice for Group 14. - """ a = 0 while a < 2: a = number.getRandomNBitInteger(private_bits) @@ -52,10 +46,6 @@ def generate_keypair(p: int, g: int, private_bits: int = 256) -> tuple[int, int] def derive_shared_key(peer_public: int, private: int, p: int, key_len: int = 32) -> bytes: - """ - Compute the shared secret and derive a symmetric key using HKDF-SHA256. - Returns key_len bytes (default 32 bytes = 256-bit key). - """ shared_secret = pow(peer_public, private, p) # Use fixed-length big-endian encoding of the shared secret for KDF input shared_bytes = int_to_fixed_length_bytes(shared_secret, p.bit_length()) @@ -63,7 +53,6 @@ def derive_shared_key(peer_public: int, private: int, p: int, key_len: int = 32) return key -@dataclass class TimingResult: alice_keygen_s: float bob_keygen_s: float