0% found this document useful (0 votes)
16 views4 pages

Ins 123456

The document describes two practical implementations of encryption techniques: 1) Rail fence cipher - A Python code sample that implements the rail fence cipher technique to encrypt a plaintext message using a specified "rails" or number of rows. 2) Transposition cipher - Another Python code sample that implements transposition cipher encryption by arranging the characters of a plaintext message into a grid based on a keyword, and then reading out the ciphertext by the columns indicated by the sorted keyword letters. Both code samples take a plaintext message and encryption key as input and output the encrypted ciphertext.

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)
16 views4 pages

Ins 123456

The document describes two practical implementations of encryption techniques: 1) Rail fence cipher - A Python code sample that implements the rail fence cipher technique to encrypt a plaintext message using a specified "rails" or number of rows. 2) Transposition cipher - Another Python code sample that implements transposition cipher encryption by arranging the characters of a plaintext message into a grid based on a keyword, and then reading out the ciphertext by the columns indicated by the sorted keyword letters. Both code samples take a plaintext message and encryption key as input and output the encrypted ciphertext.

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/ 4

FACULTY OF TECHNOLOGY

Department of Information Technology


01IT0611 – Information and Network Security

Practical 4.1

AIM - Implement Rail Fence cipher.

Code :-

def encrypt(text, rails):


fence = [[' ' for _ in range(len(text))] for _ in range(rails)]
rail = 0
direction = 1

for i in range(len(text)):
fence[rail][i] = text[i]
rail += direction
if rail == rails - 1 or rail == 0:
direction *= -1

for rail in fence:


print(''.join(rail))
fence = [[] for _ in range(rails)]
rail = 0
direction = 1

for char in text:


fence[rail].append(char)
rail += direction
if rail == rails - 1 or rail == 0:
direction *= -1

cipher_text = ''.join(char for rail in fence for char in rail)


return cipher_text

text = input("Enter the text to encrypt : ")

Pranav Gediya - 92100104066 7


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

rails = int(input("Enter the depth : "))


rail_fence = encrypt(text, rails)
print("\nEncrypted Text:", rail_fence)

Output :-

Pranav Gediya - 92100104066 8


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

Practical 4.2

AIM - Implement Transposition cipher.

Code :-

import math
def row(s,key):

temp=[]
for i in key:
if i not in temp:
temp.append(i)
k=""
for i in temp:
k+=i
print("The key used for encryption is: ",k)

b=math.ceil(len(s)/len(k))

if(b<len(k)):
b=b+(len(k)-b)

arr=[['_' for i in range(len(k))]


for j in range(b)]
i=0
j=0
for h in range(len(s)):
arr[i][j]=s[h]
j+=1
if(j>len(k)-1):
j=0
i+=1

Pranav Gediya - 92100104066 9


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

print("The message matrix is: ")


for i in arr:
print(i)

cipher_text=""

kk=sorted(k)

for i in kk:
h=k.index(i)
for j in range(len(arr)):
cipher_text+=arr[j][h]
print("The cipher text is: ",cipher_text)

msg=input("Enter the message: ")


key=input("Enter the key in alphabets: ")
row(msg,key)

Output :-

Pranav Gediya - 92100104066 10

You might also like