0% found this document useful (0 votes)
21 views

Ins (1) - 4 F

The document describes code implementations for the Rail Fence cipher and Transposition cipher. For the Rail Fence cipher, the code defines a function that takes plaintext and a key as input and outputs the encrypted ciphertext. For the Transposition cipher, another function is defined that takes plaintext and a key, arranges the plaintext in a matrix based on the key, and returns the encrypted ciphertext. Both functions prompt the user for input and print out the encrypted message.

Uploaded by

nagabe8303
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Ins (1) - 4 F

The document describes code implementations for the Rail Fence cipher and Transposition cipher. For the Rail Fence cipher, the code defines a function that takes plaintext and a key as input and outputs the encrypted ciphertext. For the Transposition cipher, another function is defined that takes plaintext and a key, arranges the plaintext in a matrix based on the key, and returns the encrypted ciphertext. Both functions prompt the user for input and print out the encrypted message.

Uploaded by

nagabe8303
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

FACULTY OF ENGINEERING AND TECHNOLOGY

Department of Information Technology


01IT0611 – Information and Network Security

Practical 4.1

AIM - Implement Rail Fence cipher.

Code :-

def rail_fence_cipher(text: str, key: int) -> str:


rail = [['\n' for i in range(len(text))] for j in range(key)]
dir_down = False
row, col = 0, 0
for i in range(len(text)):
if row == 0 or row == key - 1:
dir_down = not dir_down
rail[row][col] = text[i]
col += 1
row += 1 if dir_down else -1
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)
text = input("Enter the plain text : ")
key = int(input("Enter the key : "))
cipher_text = rail_fence_cipher(text, key)
print("The encrypted message is: " , cipher_text)

Output :-

Pranav Gediya - 92100104066 7


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Information Technology
01IT0611 – Information and Network Security

Practical 4.2

AIM - Implement Transposition cipher.

Code :-

def transposition_cipher(plaintext, key):


plaintext = plaintext.replace(" ", "")
rows = len(plaintext) // len(key) + 1
cols = len(key)
plaintext += "_" * (rows * cols - len(plaintext))
matrix = [list(plaintext[i:i+cols]) for i in range(0, rows*cols, cols)]
key_map = {key[i]: i for i in range(cols)}
ciphertext = ""
for k in sorted(key_map):
j = key_map[k]
for i in range(rows):
ciphertext += matrix[i][j]
return ciphertext

plaintext = input("Enter Plain Text: ")


key = input("Enter KEY: ")
ciphertext = transposition_cipher(plaintext, key)
print(ciphertext)

Output :-

Pranav Gediya - 92100104066 8

You might also like