KrutikShah CSS Exp3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Experiment No.

3
Date of Performance: 25-03-2021 Date of Submission: 01-04-2021

SAP ID: 60004198011 Name: Krutik Shah

Div: B Batch: B4

AIM: Design and Implement your “own” cipher which will combine both “Substitution” and
“Transposition” Techniques studied.
THEORY:
Encryption:
1. Take the plain text as input
2. Take a numeric key and text key from the user
3. Calculate the Fkey as nCr of numeric key entered by the user and mod it by 26 .
4. Substitute the plain text as Fkey times ahead in alphabetical order.
5. Arrange substituted text by arranging it in rows depending on text key.
6. Find the transpose of the matrix obtained.
7. Rotate the matrix in clockwise direction by 90 degree
8. Retrieve the text column-wise to get Cipher text.

Decryption:
1. Take the plain text as input
2. Take a numeric key and text key from the user
3. Calculate the Fkey as nCr of numeric key entered by the user and mod it by 26.
4. Arrange substituted text by arranging it in rows depending on text key.
5. Rotate the matrix in anti-clockwise direction by 90 degree
6. Find the transpose of the matrix obtained.
7. Retrieve the text column-wise to get Substituted Cipher text.
8. Substitute the plain text as Fkey times behind in alphabetical order to get the plain
text.
EXAMPLE:
Encryption:
Plain text : DJSANGHVI
Numeric key: 6
Text key : ABC (length =3)
nCr = 84
Fkey = 84%26 = 6
Substituted Text : JPYGTMNBO

[J P Y
G T M
N B O]

Transpose:
[J G N
P T B
Y M O]

Rotation by 90 degree clock-wise:


[Y P J
M T G
O B N]

Cipher Text: YMOPTBJGN

Decryption:
Cipher Text: YMOPTBJGN
Numeric key: 6
Text key : ABC (length =3)
nCr = 84
Fkey = 84%26 = 6

[Y P J
M T G
O B N]

Rotation by 90 degree anti-clock-wise:

[J G N
P T B
Y M O]
Transpose:

[J P Y
G T M
N B O]

Substituted Cipher Text : JPYGTMNBO


Plain text : DJSANGHVI

CODE:
import math
import numpy as np

def efunc(pt, finalkey1):


encryption = ""

for c in pt:

if c.isupper():
c_unicode = ord(c)
c_index = ord(c) - ord("A")
new_index = (c_index + finalkey1) % 26
new_unicode = new_index + ord("A")
new_character = chr(int(new_unicode))
encryption = encryption + new_character

else:
encryption += c
return encryption

def decryptMessage(ct,key):
msg = ""
msg1 = ""

msg_indx = 0
msg_len = float(len(ct))
msg_lst = list(ct)

col = len(r2)

row = int(math.ceil(msg_len / col))

key_lst = sorted(list(r2))
k_indx=len(key_lst)-1
print(key_lst)
dec_cipher = []
for _ in range(row):
dec_cipher += [[None] * col]

for _ in range(col):
curr_idx = key.index(key_lst[k_indx])

for j in range(row):
dec_cipher[j][curr_idx] = msg_lst[msg_indx]
print( dec_cipher[j][curr_idx])
msg_indx += 1
k_indx -= 1

try:
msg = ''.join(sum(dec_cipher, []))

print("Decryped Message after Tansposition: ", msg)

except TypeError:
raise TypeError("This program cannot",
"handle repeating words.")

null_count = msg.count('_')

if null_count > 0:
return msg[: -null_count]

for c in msg:
if c.isupper():
c_unicode = ord(c)
c_index = ord(c) - ord("A")
new_index = (c_index - decryptkey1) % 26
new_unicode = new_index + ord("A")
new_character = chr(int(new_unicode))
msg1 = msg1 + new_character

else:

msg1 += c

msg1= msg1[:-1]
return msg1

# -----------------------------MENU--------------------------------------

while(True):
print("--------------------MENU------------------")
choice = int(input("1.Encryption \n2.Decryption \nEnter your choice: "))

if(choice == 1):
pt = input("Enter the plain text: ")
n = int(len(pt))
print("Enter the Key for Subsitution: ", end="")
r = int(input())
r1 =str(r)
fact = i = 1
while i<=n:
fact = i*fact
i += 1

numerator = fact
sub = n-r
fact = i = 1

while i<=sub:
fact = i*fact
i += 1

denominator = fact
fact = i = 1
while i<=r:
fact = i*fact
i += 1

denominator = fact*denominator
finalkey = numerator/denominator
print(finalkey)

finalkey1 = (finalkey % 26)


print(finalkey1)
encryption = efunc(pt,finalkey1)
print("Plain text:",pt)
print("Cipher text after Subsitution :",encryption)
key=input ('Enter a Key for Transposition :')

col=len(key)
if((len(encryption)%col)!=0):encryption+="x"*(len(encryption)%col)
encryption=encryption.replace(' ', '')
o=[]
for i in key:
o.append(i)
h=[]
for i in range(col):
h.append(encryption[i:len(encryption):col])
dic=dict(zip(o,h))
so=sorted(dic.keys())
print("Cipher text after Transposition :")
print(''.join(dic[i]for i in so))
res = dict(reversed(list(dic.items())))
print("Final Cipher Text after Tranpose and Rotation",''.join(res[i]for i in res))

elif(choice == 2):

ct = input("Enter the cipher text: ")


n1 = int(len(ct))
print("Enter the Subsitutuion Key: ", end="")
r1 = int(input())

fact = i = 1
while i<=n1:
fact = i*fact
i += 1

numerator = fact
sub = n1-r1
fact = i = 1

while i<=sub:
fact = i*fact
i += 1

denominator = fact
fact = i = 1
while i<=r1:
fact = i*fact
i += 1

denominator = fact*denominator
decryptkey = numerator/denominator
print(decryptkey)
decryptkey1 = (decryptkey % 26)
print(decryptkey1)

r2 = input("Enter the Key Transposition :")


print("Decrypted Message after Substitution :{}".
format(decryptMessage(ct,r2)))

else:

print("Invalid choice")

OUTPUT:

CONCLUSION: Hence, we have successfully implemented our own cipher technique by


combining substitution and transposition ciphers.

You might also like