[Solved] Write a python program to encrypt and decrypt the message using caesar cipher alogorithm and rail fence algorithm [closed]

Introduction

This article will provide a step-by-step guide on how to write a Python program to encrypt and decrypt a message using the Caesar Cipher algorithm and the Rail Fence algorithm. The Caesar Cipher algorithm is a substitution cipher that shifts the letters of a message by a certain number of places in the alphabet. The Rail Fence algorithm is a transposition cipher that rearranges the letters of a message in a zig-zag pattern. Both algorithms are simple to implement and can be used to encrypt and decrypt messages.

Solution

# Caesar Cipher Algorithm

# Encryption
def encrypt(text,s):
result = “”

# traverse text
for i in range(len(text)):
char = text[i]

# Encrypt uppercase characters
if (char.isupper()):
result += chr((ord(char) + s-65) % 26 + 65)

# Encrypt lowercase characters
else:
result += chr((ord(char) + s – 97) % 26 + 97)

return result

# Decryption
def decrypt(text,s):
result = “”

# traverse text
for i in range(len(text)):
char = text[i]

# Decrypt uppercase characters
if (char.isupper()):
result += chr((ord(char) – s-65) % 26 + 65)

# Decrypt lowercase characters
else:
result += chr((ord(char) – s – 97) % 26 + 97)

return result

# Driver code
text = “ATTACKATONCE”
s = 4

# Encrypt the string
print (“Text : ” + text)
print (“Shift : ” + str(s))
print (“Cipher: ” + encrypt(text,s))

# Decrypt the string
print (“Text : ” + encrypt(text,s))
print (“Shift : ” + str(s))
print (“Plain : ” + decrypt(encrypt(text,s),s))

# Rail Fence Algorithm

# Encryption
def encryptRailFence(text, key):
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))

# Decryption
def decryptRailFence(cipher, key):

# create the matrix to fill
# in the rail fence pattern
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 text = "ATTACKATONCE" key = 3 # Encrypt the string print ("Text : " + text) print ("Shift : " + str(key)) print ("Cipher: " + encryptRailFence(text,key)) # Decrypt the string print ("Text : " + encryptRailFence(text,key)) print ("Shift : " + str(key)) print ("Plain : " + decryptRailFence(encryptRailFence(text,key),key))



Write a python program to encrypt and decrypt the message using caesar cipher alogorithm and rail fence algorithm [closed]

Encrypting and Decrypting Messages Using Caesar Cipher Algorithm and Rail Fence Algorithm

Encrypting and decrypting messages is an important part of modern communication. There are many different algorithms that can be used to encrypt and decrypt messages, such as the Caesar Cipher algorithm and the Rail Fence algorithm. In this article, we will discuss how to use these two algorithms to encrypt and decrypt messages.

Caesar Cipher Algorithm

The Caesar Cipher algorithm is a substitution cipher that uses a single key to encrypt and decrypt messages. It works by shifting each letter of the plaintext message by a certain number of places in the alphabet. For example, if the key is 3, then the letter A would be shifted to the letter D, the letter B would be shifted to the letter E, and so on. To decrypt the message, the same key is used to shift the letters back to their original positions.

Rail Fence Algorithm

The Rail Fence algorithm is a transposition cipher that uses a single key to encrypt and decrypt messages. It works by writing the plaintext message in a zigzag pattern, with each letter being written in a different row. For example, if the key is 3, then the message would be written in three rows, with the first letter being written in the first row, the second letter being written in the second row, and so on. To decrypt the message, the same key is used to read the message in the same zigzag pattern.

Conclusion

Encrypting and decrypting messages using the Caesar Cipher algorithm and the Rail Fence algorithm is a simple and effective way to protect sensitive information. Both algorithms are easy to use and can be used to encrypt and decrypt messages quickly and securely.