Caesar Playfair
Caesar Playfair
Caeser Cipher:
CODE:
def caeser_cipher(string, key, mod=26):
ans = ""
for char in range(len(string)):
n = ord(string[char])
string[char] = chr(n)
return ans.join(string)
OUTPUT:
Playfair cipher:
CODE:
# Function to convert the string to lowercase
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'
]
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
char2 = ''
if e2c == 4:
char2 = matr[e2r][0]
else:
char2 = matr[e2r][e2c + 1]
char2 = ''
if e2r == 4:
char2 = matr[0][e2c]
else:
char2 = matr[e2r + 1][e2c]
char2 = ''
char2 = matr[e2r][e1c]
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
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'
]
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
char2 = ''
if e2c == 4:
char2 = matr[e2r][0]
else:
char2 = matr[e2r][e2c + 1]
return char1, char2
char2 = ''
if e2r == 4:
char2 = matr[0][e2c]
else:
char2 = matr[e2r + 1][e2c]
char2 = ''
char2 = matr[e2r][e1c]
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)
CipherText = ""
for i in CipherList:
CipherText += i
print("CipherText:", CipherText)
OUTPUT: