Cns Lab Manual
Cns Lab Manual
def analyze_text_file(file_name):
with open(file_name, 'r') as file:
text = file.read()
words = text.split()
num_words = len(words)
num_chars = len(text)
# Example usage
file_name = "example.txt" # Replace with your text file path
analyze_text_file(file_name)
Word Count: 2
Character Count: 13
Character: H ASCII Value: 72
Character: e ASCII Value: 101
Character: l ASCII Value: 108
Character: l ASCII Value: 108
Character: o ASCII Value: 111
Character: ASCII Value: 32
Character: W ASCII Value: 87
Character: o ASCII Value: 111
Character: r ASCII Value: 114
Character: l ASCII Value: 108
Character: d ASCII Value: 100
Character: ! ASCII Value: 33
2. Encryption Program (Shift Cipher with Key = 4)
# Example usage
plaintext = "computer science engineering mrecw"
key = 4
ciphertext = encrypt_message(plaintext, key)
print(f"Ciphertext: {ciphertext}")
Output:
Ciphertext: gsqtyxivwgmirgiirkmriivmrkwvqyrmzivwmxc
3. Decryption Program (Ciphertext: "PHHW PH DIWHU WKH WRJD SDUWB")
def decrypt_message(ciphertext):
for key in range(1, 26):
decrypted_text = ""
for char in ciphertext:
if char.isalpha():
ascii_offset = 65 if char.isupper() else 97
decrypted_char = chr((ord(char) - ascii_offset - key) % 26 + ascii_offset)
decrypted_text += decrypted_char
else:
decrypted_text += char
print(f"Key: {key}, Decrypted Message: {decrypted_text}")
# Example usage
ciphertext = "PHHW PH DIWHU WKH WRJD SDUWB"
decrypt_message(ciphertext)
def decrypt_message(ciphertext):
for key in range(1, 26):
decrypted_text = ""
for char in ciphertext:
if char.isalpha():
ascii_offset = 65 if char.isupper() else 97
decrypted_char = chr((ord(char) - ascii_offset - key) % 26 + ascii_offset)
decrypted_text += decrypted_char
else:
decrypted_text += char
print(f"Key: {key}, Decrypted Message: {decrypted_text}")
# Example usage
ciphertext = "ZICVTWQNGKZEIIGASXSTSLVVWLA"
decrypt_message(ciphertext)
# Example usage
plaintext = "wewishtoreplaceplayer"
ciphertext = monoalphabetic_encrypt(plaintext, key_mapping)
print(f"Ciphertext: {ciphertext}")
Output
Ciphertext: ztrkjbxjwgjoplbwtvtr
6. Decryption of Encrypted Message: "SEEMSEAOMEDSAMHL" (Using Monoalphabetic Cipher
Mapping)
# Example usage
ciphertext = "SEEMSEAOMEDSAMHL"
plaintext = monoalphabetic_decrypt(ciphertext, key_mapping)
print(f"Plaintext: {plaintext}")
Output:
Plaintext: wewishtoreplaceplayer
7. Playfair Cipher (Key: "mrecwautonomous")
# Note: This is just a sample of Playfair cipher implementation; the full implementation needs more
logic.
def playfair_encryption(plaintext, key):
# Implement Playfair encryption based on provided key.
pass # Playfair logic goes here.
# Example usage
plaintext = "HELLO WORLD"
key = "mrecwautonomous"
ciphertext = playfair_encryption(plaintext, key)
print(f"Ciphertext: {ciphertext}")
(Playfair encryption needs a matrix and digraph rules which can be quite detailed, so this is just a
placeholder for that.)
8. Hill Cipher (Key: "CBDE")
import numpy as np
# Example usage
plaintext = "HELLO WORLD"
key = "CBDE"
ciphertext = hill_cipher_encryption(plaintext, key)
decrypted_text = hill_cipher_decryption(ciphertext, key)
print(f"Ciphertext: {ciphertext}")
print(f"Decrypted Text: {decrypted_text}")
9. Vigenère Cipher Program (with keyword "deceptive")
return ciphertext
return plaintext
# Example usage:
Output: