CEASER CIPHER:
AIM :
To develope a simple Python program to implement ceaser cipher.
ALGORITHM :
Step 1 :
Design of Ceaser Cipher algorithm .
Step 2 :
Implementation using pyhton code .
Step 3 :
Testing algorithm with different key values .
PROGRAM :
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 = "ALLUGURICHARANTEJA"
s = 4
print ("Text : " + text)
print ("Shift : " + str(s))
print ("Cipher: " + encrypt(text,s))
OUTPUT :
![image](https://private-user-images.githubusercontent.com/118343892/307523992-18258d1e-3995-4cad-9b68-b577252e770f.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjE0OTM3MDAsIm5iZiI6MTcyMTQ5MzQwMCwicGF0aCI6Ii8xMTgzNDM4OTIvMzA3NTIzOTkyLTE4MjU4ZDFlLTM5OTUtNGNhZC05YjY4LWI1NzcyNTJlNzcwZi5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNzIwJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDcyMFQxNjM2NDBaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT03YjlhYTY1Mjk3NjQyYmMyODAxM2UwNzE2OWE0OGVjOWFkMjk2NmY4ODZmMTFjNzE2NGY2ZDA1MDQwZDkyZjEwJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.aqBFMFRXd5Jeod-GTsb6npKf8Z4y8EzZIAzw8KNpPZI)
RESULT :
Thus , The program is executed perfectly and output is verified.
PLAYFAIR CIPHER:
AIM :
To develop a simple Python Program to implement PlayFair Cipher.
ALGORITHM :
Step 1 :
Design of PlayFair Cipher algorithm.
Step 2 :
Now implement the Python program.
Step 3 :
Testing algorithm with different key values.
PROGRAM :
def toLowerCase(text):
return text.lower()
# Function to remove all spaces in a string
def removeSpaces(text):
newText = ""
for i in text:
if i == " ":
continue
else:
newText = newText + i
return newText
# Function to group 2 elements of a string
# as a list element
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
# Function to fill a letter in a string element
# If 2 letters in the same string matches
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']
# Function to generate the 5x5 key square matrix
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]
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 = "Monarchy"
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)
OUTPUT :
![image](https://private-user-images.githubusercontent.com/118343892/307529586-3c257fd3-9dfb-4f10-b8a3-6e1520e53d1d.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjE0OTM3MDAsIm5iZiI6MTcyMTQ5MzQwMCwicGF0aCI6Ii8xMTgzNDM4OTIvMzA3NTI5NTg2LTNjMjU3ZmQzLTlkZmItNGYxMC1iOGEzLTZlMTUyMGU1M2QxZC5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNzIwJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDcyMFQxNjM2NDBaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0wNjFkMzVlOGY1YmQzY2ZkNzYwNmU3NDg4ZThkYjMwY2JiMDJiZDJmODlhN2RjYTc0Njk2MWNiNDE0NTBiYWJmJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.8XASUAaSovu78EGciC_R-u4Y0cOurEwJWv4K7wbmZo4)
RESULT :
Thus , The program is executed perfectly and output is verified.
HILL CIPHER
AIM :
To develop a simple Python Program to implement Hill Cipher.
ALGORITHM :
Step 1 :
First, develop the hill cipher program and design it.
Step 2 :
Now, implement the Python Program for the Hill Cipher Algorithm.
Step 3 :
Now, Make a Test for the different key values.
PROGRAM :
keyMatrix = [[0] * 3 for i in range(3)]
# Generate vector for the message
messageVector = [[0] for i in range(3)]
# Generate vector for the cipher
cipherMatrix = [[0] for i in range(3)]
# Following function generates the
# key matrix for the key string
def getKeyMatrix(key):
k = 0
for i in range(3):
for j in range(3):
keyMatrix[i][j] = ord(key[k]) % 65
k += 1
# Following function encrypts the message
def encrypt(messageVector):
for i in range(3):
for j in range(1):
cipherMatrix[i][j] = 0
for x in range(3):
cipherMatrix[i][j] += (keyMatrix[i][x] *
messageVector[x][j])
cipherMatrix[i][j] = cipherMatrix[i][j] % 26
def HillCipher(message, key):
# Get key matrix from the key string
getKeyMatrix(key)
# Generate vector for the message
for i in range(3):
messageVector[i][0] = ord(message[i]) % 65
# Following function generates
# the encrypted vector
encrypt(messageVector)
# Generate the encrypted text
# from the encrypted vector
CipherText = []
for i in range(3):
CipherText.append(chr(cipherMatrix[i][0] + 65))
# Finally print the ciphertext
print("Ciphertext: ", "".join(CipherText))
# Driver Code
def main():
# Get the message to
# be encrypted
message = "ACT"
key = "GYBNQKURP"
HillCipher(message, key)
if __name__ == "__main__":
main()
OUTPUT :
![image](https://private-user-images.githubusercontent.com/118343892/307530099-f81f6436-6f5d-4aff-85c9-ddafe78eaff7.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjE0OTM3MDAsIm5iZiI6MTcyMTQ5MzQwMCwicGF0aCI6Ii8xMTgzNDM4OTIvMzA3NTMwMDk5LWY4MWY2NDM2LTZmNWQtNGFmZi04NWM5LWRkYWZlNzhlYWZmNy5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNzIwJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDcyMFQxNjM2NDBaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1kM2E4YzAzNjViNzAwNzYyNzExNDZiZjA5ODQwYTJmYTljMTE3N2Y4OWU1NzdkOWZjZTQ1MmI1Mzc1Yzg4MjRmJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.Hg766GmdUaJ_Qr9XCtKPeK82DIzjF5r9VrP7UEC9HKo)
RESULT :
Thus , The program is executed perfectly and output is verified.
Vigenere Cipher
AIM :
To develop a simple python program to implement Vigenere Cipher.
ALGORITHM :
Step 1 :
First, develop the Vigenere cipher program and design it.
Step 2 :
Now, implement the Python Program for the Vigenere Cipher Algorithm.
Step 3 :
Now, Make a Test for the different key values.
PROGRAM :
# Python code to implement
# Vigenere Cipher
# This function generates the
# key in a cyclic manner until
# it's length isn't equal to
# the length of original text
def generateKey(string, key):
key = list(key)
if len(string) == len(key):
return(key)
else:
for i in range(len(string) -
len(key)):
key.append(key[i % len(key)])
return("" . join(key))
# This function returns the
# encrypted text generated
# with the help of the key
def cipherText(string, key):
cipher_text = []
for i in range(len(string)):
x = (ord(string[i]) +
ord(key[i])) % 26
x += ord('A')
cipher_text.append(chr(x))
return("" . join(cipher_text))
# This function decrypts the
# encrypted text and returns
# the original text
def originalText(cipher_text, key):
orig_text = []
for i in range(len(cipher_text)):
x = (ord(cipher_text[i]) -
ord(key[i]) + 26) % 26
x += ord('A')
orig_text.append(chr(x))
return("" . join(orig_text))
# Driver code
if __name__ == "__main__":
string = "GEEKSFORGEEKS"
keyword = "AYUSH"
key = generateKey(string, keyword)
cipher_text = cipherText(string,key)
print("Ciphertext :", cipher_text)
print("Original/Decrypted Text :",
originalText(cipher_text, key))
# This code is contributed
# by Pratik Somwanshi
OUTPUT :
![image](https://private-user-images.githubusercontent.com/118343892/307530422-163346aa-f451-499a-a564-1e0ce44d9650.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjE0OTM3MDAsIm5iZiI6MTcyMTQ5MzQwMCwicGF0aCI6Ii8xMTgzNDM4OTIvMzA3NTMwNDIyLTE2MzM0NmFhLWY0NTEtNDk5YS1hNTY0LTFlMGNlNDRkOTY1MC5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNzIwJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDcyMFQxNjM2NDBaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1lODdlZmRhZGU3ZmJlMzQ0NTA5NTVkNjMyMDg1YTJmNjg1YWRlNTk0Mzc0ZTgyYTY3ZTFiNjU0YmQzNjk1OGE5JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.6itkaNekUvFncW53TUY92xh0qlxoS_D19EepZjZaLFs)
RESULT :
Thus , The program is executed perfectly and output is verified.
Rail Fence Cipher
AIM :
To develop a simple Python program to implement Rail Fence Cipher.
ALGORRITHM :
Step 1 :
To develop a simple python program to implement Rail Fence Cipher.
Step 2 :
Now, implement the Python Program for the Rail Fence Cipher Algorithm.
Step 3 :
Now, Make a Test for the different key values.
PROGRAM :
# Python3 program to illustrate
# Rail Fence Cipher Encryption
# and Decryption
# function to encrypt a message
def encryptRailFence(text, key):
# create the matrix to cipher
# plain text key = rows ,
# length(text) = columns
# filling the rail matrix
# to distinguish filled
# spaces from blank ones
rail = [['\n' for i in range(len(text))]
for j in range(key)]
# to find the direction
dir_down = False
row, col = 0, 0
for i in range(len(text)):
# check the direction of flow
# reverse the direction if we've just
# filled the top or bottom rail
if (row == 0) or (row == key - 1):
dir_down = not dir_down
# fill the corresponding alphabet
rail[row][col] = text[i]
col += 1
# find the next row using
# direction flag
if dir_down:
row += 1
else:
row -= 1
# now we can construct the cipher
# using the rail matrix
result = []
for i in range(key):
for j in range(len(text)):
if rail[i][j] != '\n':
result.append(rail[i][j])
return("" . join(result))
# This function receives cipher-text
# and key and returns the original
def decryptRailFence(cipher, key):
# create the matrix to cipher
# plain text key = rows ,
# length(text) = columns
# filling the rail matrix to
rail = [['\n' for i in range(len(cipher))]
for j in range(key)]
# to find the direction
dir_down = None
row, col = 0, 0
# mark the places with '*'
for i in range(len(cipher)):
if row == 0:
dir_down = True
if row == key - 1:
dir_down = False
# place the marker
rail[row][col] = '*'
col += 1
# find the next row
# using direction flag
if dir_down:
row += 1
else:
row -= 1
# now we can construct the
# fill the rail matrix
index = 0
for i in range(key):
for j in range(len(cipher)):
if ((rail[i][j] == '*') and
(index < len(cipher))):
rail[i][j] = cipher[index]
index += 1
# now read the matrix in
# zig-zag manner to construct
# the resultant text
result = []
row, col = 0, 0
for i in range(len(cipher)):
# check the direction of flow
if row == 0:
dir_down = True
if row == key-1:
dir_down = False
# place the marker
if (rail[row][col] != '*'):
result.append(rail[row][col])
col += 1
# find the next row using
# direction flag
if dir_down:
row += 1
else:
row -= 1
return("".join(result))
# Driver code
if __name__ == "__main__":
print(encryptRailFence("attack at once", 2))
print(encryptRailFence("GeeksforGeeks ", 3))
print(encryptRailFence("defend the east wall", 3))
print(decryptRailFence("GsGsekfrek eoe", 3))
print(decryptRailFence("atc toctaka ne", 2))
print(decryptRailFence("dnhaweedtees alf tl", 3))
OUTPUT :
![image](https://private-user-images.githubusercontent.com/118343892/307530854-80c42ce4-3e9c-4761-8d9a-8fecb066e5df.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjE0OTM3MDAsIm5iZiI6MTcyMTQ5MzQwMCwicGF0aCI6Ii8xMTgzNDM4OTIvMzA3NTMwODU0LTgwYzQyY2U0LTNlOWMtNDc2MS04ZDlhLThmZWNiMDY2ZTVkZi5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNzIwJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDcyMFQxNjM2NDBaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT05ODBlNzU0N2YyZjkwMjYwMjFjMTRlODdmYzE2ODY5YWUwZjNjMzAzMmY5MmEzNDhkMGE1MDk3NGE0OTNmZGNkJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.YwBfmg16kngC2vX0EywPvwTzx5JEfNKMMOIKwTjePao)
RESULT :
Thus , The program is executed perfectly and output is verified.