Practicle 1:-
Substitution cipher technique
1.monoalphabetic cipher
Code:-
def encrypt_mono(plaintext,key_map):
    ciphertext=""
    for char in plaintext:
        if char.lower() in key_map:
            if char.isupper():
                ciphertext +=key_map[char.lower()].upper()
            else:
                    ciphertext+=key_map[char]
        else:
            ciphertext+=char
    return ciphertext
def decrypt_mono(ciphertext,key_map):
    inverse_key_map={v:k for k,v in key_map.items()}
    plaintext=""
    for char in ciphertext:
        if char.lower() in inverse_key_map:
            if char.isupper():
                plaintext+=inverse_key_map[char.lower()].upper()
            else:
                plaintext+=inverse_key_map[char]
        else:
            plaintext+=char
    return plaintext
substitution_key = {
'a': 'x','b':'n','c': 'o', 'd': 'p', 'e':'q','f':'r', 'g': 's','h':'t','i':'u',
'j': 'v', 'k': 'w','l':'a','m': 'b', 'n': 'c','o':'d','p':'e','q':'f', 'r':'g',
's':'h','t': 'i', 'u': 'j','v':'k','w': '1','x':'m', 'y': 'z','z':'y'}
message="HELLO TYCS students"
encrypted_msg=encrypt_mono(message,substitution_key)
print(f"Encrypted: {encrypted_msg}")
decrypted_msg=decrypt_mono(encrypted_msg,substitution_key)
print(f"decrypted: {decrypted_msg}")
Output:-
Frequency analysis test
Example
Plaintext:
 BANANA
Substitution key:
B → K
A → T
N → R
Step 1: Encrypt
Applying the substitutions:
B A N A N A
K T R T R T
Ciphertext:
KTRTRT
The most frequent letter is T.
Step 3: Analyze the Pattern
Ciphertext:
K T R T R T
Pattern:
1 2 3 2 3 2
This tells us:
The 2nd, 4th, and 6th letters are the same.
The 3rd and 5th letters are the same.
A common English word with this pattern is:
B A N A N A
1 2 3 2 3 2
Step 5: Recover the Plaintext
Apply the mapping:
KTRTRT:-
BANANA
Recovered plaintext:
BANANA

[TABLE 1]
Cipher Letter | Count
T | 3
R | 2
K | 1

[TABLE 2]
Cipher | Plain
K | B
T | A
R | N
