def caesar_cipher_encrypt(text, shift):
encrypted_text = ""
for char in text:
char = chr(ord(char) + shift)
encrypted_text += char
return encrypted_text
def caesar_cipher_decrypt(text, shift):
decrypted_text = ""
for char in text:
char = chr(ord(char) - shift)
decrypted_text += char
return decrypted_text
plaintext = input("Enter the plaintext: ")
shift_amount = 3
encrypted_text = caesar_cipher_encrypt(plaintext, shift_amount)
print("Encrypted:", encrypted_text)
decrypted_text = caesar_cipher_decrypt(encrypted_text, shift_amount)
print("Decrypted:", decrypted_text)
Output:
Enter the plaintext: deathnote
Encrypted: ghdwkqrwh
Decrypted: deathnote
import string
def key_matrix_generator() :
i=j=0
a_to_z = string.ascii_lowercase.replace("j", "*")
key = input("Enter key: ").lower().replace("j", "i")
key_matrix = ["" for i in range(5)]
for char in key:
if char in a_to_z:
key_matrix[i] += char
a_to_z = a_to_z.replace(char, "*")
j += 1
if j > 4:
i += 1
j=0
for char in a_to_z:
if char != "*":
key_matrix[i] += char
a_to_z = a_to_z.replace(char, "*")
j += 1
if j > 4:
i += 1
j=0
for row in key_matrix:
for char in row:
print(char, end = " ")
print("")
print("")
return key_matrix
def text_pair_generator(text):
i=0
a_to_z = string.ascii_lowercase.replace("j", "*")
text_pairs = []
for char in text:
if char not in a_to_z:
text = text.replace(char, "")
while i < len(text):
a = text[i]
b = ""
if (i + 1) == len(text):
b = "x"
else:
b = text[i + 1]
if a != b:
text_pairs.append(a + b)
i += 2
else:
text_pairs.append(a + "x")
i += 1
return text_pairs
def ciphertext_generator(key_matrix):
plaintext_pairs = text_pair_generator(plaintext)
ciphertext_pairs = []
for pair in plaintext_pairs:
applied_rule = False
for row in key_matrix:
if pair[0] in row and pair[1] in row:
j0 = row.find(pair[0])
j1 = row.find(pair[1])
cipher_text = row[(j0 + 1) % 5] + row[(j1 + 1) % 5]
ciphertext_pairs.append(cipher_text)
applied_rule = True
if applied_rule:
continue
for j in range(5):
col = "".join([key_matrix[i][j] for i in range(5)])
if pair[0] in col and pair[1] in col:
i0 = col.find(pair[0])
i1 = col.find(pair[1])
cipher_text = col[(i0 + 1) % 5] + col[(i1 + 1) % 5]
ciphertext_pairs.append(cipher_text)
applied_rule = True
if applied_rule:
continue
i0 = i1 = j0 = j1 = 0
for i in range(5):
row = key_matrix[i]
if pair[0] in row:
i0 = i
j0 = row.find(pair[0])
if pair[1] in row:
i1 = i
j1 = row.find(pair[1])
cipher_text = key_matrix[i0][j1] + key_matrix[i1][j0]
ciphertext_pairs.append(cipher_text)
return ciphertext_pairs
def plaintext_generator(key_matrix):
ciphertext_pairs = text_pair_generator(ciphertext)
plaintext_pairs = []
for pair in ciphertext_pairs:
applied_rule = False
for row in key_matrix:
if pair[0] in row and pair[1] in row:
j0 = row.find(pair[0])
j1 = row.find(pair[1])
j0 = j0 - 1
if j0 == -1:
j0 = 4
j1 = j1 - 1
if j1 == -1:
j1 = 4
plain_text = row[j0] + row[j1]
plaintext_pairs.append(plain_text)
applied_rule = True
if applied_rule:
continue
for j in range(5):
col = "".join([key_matrix[i][j] for i in range(5)])
if pair[0] in col and pair[1] in col:
i0 = col.find(pair[0])
i1 = col.find(pair[1])
i0 = i0 - 1
if i0 == -1:
i0 = 4
i1 = i1 - 1
if i1 == -1:
i1 = 4
plain_text = col[i0] + col[i1]
plaintext_pairs.append(plain_text)
applied_rule = True
if applied_rule:
continue
i0 = i1 = j0 = j1 = 0
for i in range(5):
row = key_matrix[i]
if pair[0] in row:
i0 = i
j0 = row.find(pair[0])
if pair[1] in row:
i1 = i
j1 = row.find(pair[1])
plain_text = key_matrix[i1][j0] + key_matrix[i0][j1]
plaintext_pairs.append(plain_text)
return plaintext_pairs
ciphertext = ""
plaintext = input("Enter plaintext: ").lower().replace("j", "i")
key_matrix = key_matrix_generator()
ciphertext_pairs = ciphertext_generator(key_matrix)
for pairs in ciphertext_pairs:
ciphertext += pairs
print("Ciphertext is: " + ciphertext)
plaintext_pairs = plaintext_generator(key_matrix)
plaintext = ""
for pairs in plaintext_pairs:
plaintext += pairs
print("Plaintext is: " + plaintext.replace("x", ""))
Output:
Enter plaintext: deathnote
Enter key: light
l i g h t
a b c d e
f k m n o
p q r s u
v w x y z
Ciphertext is: eaeldsuecz
Plaintext is: deathnote