0% found this document useful (0 votes)
19 views10 pages

All Practical INS

rtgfg

Uploaded by

Nachiket Panchal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views10 pages

All Practical INS

rtgfg

Uploaded by

Nachiket Panchal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

INFORMATION AND NETWORK SECURITY

(102046708)

Practical – 1
Aim: To implement Caesar cipher encryption-decryption.
Code:
def encrypt(text,s):
result = ""
for i in range(len(text)):
char = text[i]
if (char.isupper()):
result += chr((ord(char) + s-65) % 26 + 65)
else:
result += chr((ord(char) + s - 97) % 26 + 97)

return result

text = "HelloGcet"
s=4
print ("Text : " + text)
print ("Shift : " + str(s))
print ("Cipher: " + encrypt(text,s))

Output:

121020405010 1
INFORMATION AND NETWORK SECURITY
(102046708)

121020405010 2
INFORMATION AND NETWORK SECURITY
(102046708)

Practical – 2
Aim: To implement Monoalphabetic cipher encryption-decryption.
Code:
def generate_cipher_key(shift):
alphabet = 'abcdefghijklmnopqrstuvwxyz'
shifted_alphabet = alphabet[shift:] + alphabet[:shift]
key = dict(zip(alphabet, shifted_alphabet))
return key
def encrypt(message, key):
encrypted_message = ''
for char in message:
if char.isalpha():
if char.islower():
encrypted_message += key[char]
else:
encrypted_message += key[char.lower()].upper()
else:
encrypted_message += char
return encrypted_message
def decrypt(ciphertext, key):
reverse_key = {v: k for k, v in key.items()}
decrypted_message = ''
for char in ciphertext:
if char.isalpha():
if char.islower():
decrypted_message += reverse_key[char]
else:
decrypted_message += reverse_key[char.lower()].upper()
else:
decrypted_message += char
return decrypted_message
def main():
shift = int(input("Enter the shift value for the cipher: "))
key = generate_cipher_key(shift)

choice = input("Encrypt or decrypt? (e/d): ").lower()


if choice == 'e':
plaintext = input("Enter the message to encrypt: ")
encrypted = encrypt(plaintext, key)
print("Encrypted message:", encrypted)
elif choice == 'd':
ciphertext = input("Enter the message to decrypt: ")
decrypted = decrypt(ciphertext, key)
print("Decrypted message:", decrypted)
else:
print("Invalid choice. Please enter 'e' for encrypt or 'd' for decrypt.")

121020405010 3
INFORMATION AND NETWORK SECURITY
(102046708)

if name == " main ":


main()

Output:

121020405010 4
INFORMATION AND NETWORK SECURITY
(102046708)

Practical – 3
Aim: To implement Playfair cipher encryption-decryption.
Code:
def toLowerCase(text):
return text.lower()
def removeSpaces(text):
newText = ""
for i in text:
if i == " ":
continue
else:
newText = newText + i
return newText

def Diagraph(text):
Diagraph = []
group = 0
for i in range(2, len(text), 2):
Diagraph.append(text[group:i])

group = i
Diagraph.append(text[group:])
return Diagraph

def FillerLetter(text):
k = len(text)
if k % 2 == 0:
for i in range(0, k, 2):
if text[i] == text[i+1]:
new_word = text[0:i+1] + str('x') + text[i+1:]
new_word = FillerLetter(new_word)
break
else:
new_word = text
else:
for i in range(0, k-1, 2):
if text[i] == text[i+1]:
new_word = text[0:i+1] + str('x') + text[i+1:]
new_word = FillerLetter(new_word)
break
else:
new_word = text
return new_word
list1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

121020405010 5
INFORMATION AND NETWORK SECURITY
(102046708)

def generateKeyTable(word, list1):


key_letters = []
for i in word:
if i not in key_letters:
key_letters.append(i)

compElements = []
for i in key_letters:
if i not in compElements:
compElements.append(i)
for i in list1:
if i not in compElements:
compElements.append(i)
matrix = []
while compElements != []:
matrix.append(compElements[:5])
compElements = compElements[5:]
return matrix

def search(mat, element):


for i in range(5):
for j in range(5):
if(mat[i][j] == element):
return i, j

def encrypt_RowRule(matr, e1r, e1c, e2r, e2c):


char1 = ''
if e1c == 4:
char1 = matr[e1r][0]
else:
char1 = matr[e1r][e1c+1]
char2 = ''
if e2c == 4:
char2 = matr[e2r][0]
else:
char2 = matr[e2r][e2c+1]
return char1, char2
def encrypt_ColumnRule(matr, e1r, e1c, e2r, e2c):
char1 = ''
if e1r == 4:
char1 = matr[0][e1c]
else:
char1 = matr[e1r+1][e1c]
char2 = ''
if e2r == 4:
char2 = matr[0][e2c]
else:
char2 = matr[e2r+1][e2c]

121020405010 6
INFORMATION AND NETWORK SECURITY
(102046708)
return char1, char2
def encrypt_RectangleRule(matr, e1r, e1c, e2r, e2c):
char1 = ''
char1 = matr[e1r][e2c]
char2 = ''
char2 = matr[e2r][e1c]
return char1, char2
def encryptByPlayfairCipher(Matrix, plainList):
CipherText = []
for i in range(0, len(plainList)):
c1 = 0
c2 = 0
ele1_x, ele1_y = search(Matrix, plainList[i][0])
ele2_x, ele2_y = search(Matrix, plainList[i][1])

if ele1_x == ele2_x:
c1, c2 = encrypt_RowRule(Matrix, ele1_x, ele1_y, ele2_x, ele2_y)
# Get 2 letter cipherText
elif ele1_y == ele2_y:
c1, c2 = encrypt_ColumnRule(Matrix, ele1_x, ele1_y, ele2_x, ele2_y)
else:
c1, c2 = encrypt_RectangleRule(
Matrix, ele1_x, ele1_y, ele2_x, ele2_y)

cipher = c1 + c2
CipherText.append(cipher)
return CipherText

text_Plain = 'instruments'
text_Plain = removeSpaces(toLowerCase(text_Plain))
PlainTextList = Diagraph(FillerLetter(text_Plain))
if len(PlainTextList[-1]) != 2:
PlainTextList[-1] = PlainTextList[-1]+'z'

key = "GcetComputer"
print("Key text:", key)
key = toLowerCase(key)
Matrix = generateKeyTable(key, list1)

print("Plain Text:", text_Plain)


CipherList = encryptByPlayfairCipher(Matrix, PlainTextList)

CipherText = ""
for i in CipherList:
CipherText += i
print("CipherText:", CipherText)

121020405010 7
INFORMATION AND NETWORK SECURITY
(102046708)

Output:

121020405010 8
INFORMATION AND NETWORK SECURITY
(102046708)

Practical – 4
Aim: To implement Polyalphabetic cipher encryption-decryption.
Code:
def generate_key(msg, key):
key = list(key)
if len(msg) == len(key):
return key
else:
for i in range(len(msg) - len(key)):
key.append(key[i % len(key)])
return "".join(key)

def encrypt_vigenere(msg, key):


encrypted_text = []
key = generate_key(msg, key)
for i in range(len(msg)):
char = msg[i]
if char.isupper():
encrypted_char = chr((ord(char) + ord(key[i]) - 2 * ord('A')) % 26 + ord('A'))
elif char.islower():
encrypted_char = chr((ord(char) + ord(key[i]) - 2 * ord('a')) % 26 + ord('a'))
else:
encrypted_char = char
encrypted_text.append(encrypted_char)
return "".join(encrypted_text)

def decrypt_vigenere(msg, key):


decrypted_text = []
key = generate_key(msg, key)
for i in range(len(msg)):
char = msg[i]
if char.isupper():
decrypted_char = chr((ord(char) - ord(key[i]) + 26) % 26 + ord('A'))
elif char.islower():
decrypted_char = chr((ord(char) - ord(key[i]) + 26) % 26 + ord('a'))
else:
decrypted_char = char
decrypted_text.append(decrypted_char)
return "".join(decrypted_text)

text_to_encrypt = "Gcet CP Department"


key = "KEY"

encrypted_text = encrypt_vigenere(text_to_encrypt, key)


print(f"Encrypted Text: {encrypted_text}")

decrypted_text = decrypt_vigenere(encrypted_text, key)

121020405010 9
INFORMATION AND NETWORK SECURITY

print(f"Decrypted Text: {decrypted_text}")

Output:

121020405010 1

You might also like