All Practical INS
All Practical INS
(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)
121020405010 3
INFORMATION AND NETWORK SECURITY
(102046708)
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)
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
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)
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)
121020405010 9
INFORMATION AND NETWORK SECURITY
Output:
121020405010 1