UNIT 3.2 Classical and Modern Encryption Techniques.pdf
1. Unit 3: Classical and Modern Encryption Techniques
Prepared By : Dr C D Bawankar
Unit 3: Classical and Modern Encryption Techniques
2. Modern Encryption Techniques
o RC4 Stream Cipher, Symmetric Encryption (AES), Asymmetric Encryption (RSA)
Symmetric and Asymmetric Encryption: AES & RSA
Encryption is the process of converting plaintext into ciphertext to secure data from unauthorized
access. There are two main types of encryption:
1. Symmetric Encryption (Same key for encryption & decryption)
2. Asymmetric Encryption (Different keys for encryption & decryption)
1. Symmetric Encryption - AES (Advanced Encryption Standard)
Overview
• AES is a block cipher that encrypts data in fixed-size blocks (128-bit).
• Uses the same key for encryption and decryption.
• Developed by Vincent Rijmen and Joan Daemen and adopted as a standard by NIST in
2001.
• AES supports key sizes of 128-bit, 192-bit, and 256-bit.
• Faster than asymmetric encryption.
• Used in SSL/TLS, Wi-Fi Security (WPA2), and VPNs.
AES Algorithm Steps
AES operates in multiple rounds (10 for 128-bit key, 12 for 192-bit, 14 for 256-bit):
1. Key Expansion: Generates multiple round keys from the initial key.
2. Initial Round:
o AddRoundKey (XOR plaintext with the key)
3. Main Rounds (Repeated):
o SubBytes (Byte substitution using an S-box)
o ShiftRows (Row-wise shifting of bytes)
o MixColumns (Matrix multiplication to mix data)
o AddRoundKey
4. Final Round (No MixColumns)
o SubBytes
o ShiftRows
o AddRoundKey
Numerical Example of AES (Simplified)
Given Data:
• Plaintext (16 bytes): "HELLO_WORLD_AES!" (ASCII representation)
• Key (16 bytes, 128-bit): "THIS_IS_MY_KEY!!"
Convert plaintext & key to binary:
H = 01001000 E = 01000101 L = 01001100 L = 01001100
O = 01001111 _ = 01011111 W = 01010111 O = 01001111
2. Unit 3: Classical and Modern Encryption Techniques
Prepared By : Dr C D Bawankar
R = 01010010 L = 01001100 D = 01000100 _ = 01011111
A = 01000001 E = 01000101 S = 01010011 ! = 00100001
1.
2. AddRoundKey (Initial XOR with key).
3. SubBytes (Byte substitution using S-box).
4. ShiftRows (Rearrange rows).
5. MixColumns (Mathematical transformation).
6. Repeat for 10 rounds (for 128-bit key).
7. Ciphertext Output: Binary converted back to text.
Python Program for AES Encryption
Using pycryptodome library:
from Crypto.Cipher import AES
import base64
# Function to pad plaintext to be multiple of 16 bytes
def pad(text):
return text + (16 - len(text) % 16) * chr(16 - len(text) % 16)
# Function to encrypt using AES
def aes_encrypt(plain_text, key):
cipher = AES.new(key.encode('utf-8'), AES.MODE_ECB) # ECB mode
return base64.b64encode(cipher.encrypt(pad(plain_text).encode('utf-8'))).decode('utf-8')
# Function to decrypt using AES
def aes_decrypt(cipher_text, key):
cipher = AES.new(key.encode('utf-8'), AES.MODE_ECB)
decrypted = cipher.decrypt(base64.b64decode(cipher_text)).decode('utf-8')
return decrypted.rstrip(decrypted[-1]) # Remove padding
# Example usage
key = "THIS_IS_MY_KEY!!" # 16-byte key
plaintext = "HELLO_WORLD_AES!"
encrypted = aes_encrypt(plaintext, key)
print("Encrypted Text:", encrypted)
decrypted = aes_decrypt(encrypted, key)
print("Decrypted Text:", decrypted)
3. Unit 3: Classical and Modern Encryption Techniques
Prepared By : Dr C D Bawankar
2. Asymmetric Encryption - RSA (Rivest-Shamir-Adleman)
Overview
• RSA is an asymmetric encryption algorithm that uses two keys:
o Public Key (for encryption)
o Private Key (for decryption)
• Developed by Ron Rivest, Adi Shamir, and Leonard Adleman in 1977.
• Used in SSL/TLS, Digital Signatures, and Secure Communications.
• Slower than AES but provides stronger security.
RSA Algorithm Steps
1. Key Generation:
o Choose two large prime numbers p and q.
o Compute n = p × q (modulus).
o Compute ϕ(n) = (p - 1) × (q - 1) (Euler's totient function).
o Choose an encryption exponent e such that 1 < e < ϕ(n) and gcd(e, ϕ(n)) = 1.
o Compute decryption exponent d such that (e × d) mod ϕ(n) = 1.
o Public Key: (n, e), Private Key: (n, d).
2. Encryption:
C=(Pe)mod nC = (P^e) mod nC=(Pe)modn
where P is plaintext, C is ciphertext.
3. Decryption:
P=(Cd)mod nP = (C^d) mod nP=(Cd)modn
using private key (n, d).
Numerical Example of RSA
Given:
• Choose p = 3, q = 11
• Compute n = 3 × 11 = 33
• Compute ϕ(n) = (3 - 1) × (11 - 1) = 2 × 10 = 20
• Choose e = 7 (since gcd(7, 20) = 1)
• Compute d such that (7 × d) mod 20 = 1, so d = 3
• Public Key: (n = 33, e = 7), Private Key: (n = 33, d = 3)
Encryption:
4. Unit 3: Classical and Modern Encryption Techniques
Prepared By : Dr C D Bawankar
• Plaintext (P) = 5
• Ciphertext C = (5^7) mod 33 = 78125 mod 33 = 14
Decryption:
• P = (C^d) mod n = (14^3) mod 33 = 2744 mod 33 = 5
Decrypted message matches the original plaintext!
•
5. Unit 3: Classical and Modern Encryption Techniques
Prepared By : Dr C D Bawankar
•
6. Unit 3: Classical and Modern Encryption Techniques
Prepared By : Dr C D Bawankar
Python Program for RSA
Using cryptography library:
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
import base64
# Generate RSA key pair
key = RSA.generate(1024)
public_key = key.publickey().export_key()
7. Unit 3: Classical and Modern Encryption Techniques
Prepared By : Dr C D Bawankar
private_key = key.export_key()
# Encrypt function
def rsa_encrypt(plaintext, public_key):
recipient_key = RSA.import_key(public_key)
cipher_rsa = PKCS1_OAEP.new(recipient_key)
encrypted_text = cipher_rsa.encrypt(plaintext.encode())
return base64.b64encode(encrypted_text).decode()
# Decrypt function
def rsa_decrypt(ciphertext, private_key):
private_key = RSA.import_key(private_key)
cipher_rsa = PKCS1_OAEP.new(private_key)
decrypted_text = cipher_rsa.decrypt(base64.b64decode(ciphertext))
return decrypted_text.decode()
# Example usage
plaintext = "Hello RSA!"
encrypted = rsa_encrypt(plaintext, public_key)
print("Encrypted Text:", encrypted)
decrypted = rsa_decrypt(encrypted, private_key)
print("Decrypted Text:", decrypted)
Comparison: AES vs RSA
Feature AES (Symmetric) RSA (Asymmetric)
Key Type Single secret key Public & Private keys
Speed Faster Slower
Security Strong but vulnerable if key is
exposed
More secure but computationally expensive
Use
Cases
Encrypting files, Wi-Fi security,
VPNs
Digital signatures, key exchange, secure
emails