Sunteți pe pagina 1din 9

INS LAB 160117733185

Aim​ : To implement all substitution cipher techniques.

Description : ​There are 4 types of substitution methods :


1) ROT13: ​In this method during encryption, replacement of plain text characters using a
rotate of +13 characters at a time down the alphabet is done to generate ciphertext and
rotate of -13 characters up the alphabet is done to obtain original plain text. Ceaser
Cipher can be done up to 12 characters only.
Ex: ​For the plain text A-M, N-Z is the cipher text with the rotate of +13.

2) Homophonic method: ​In this method, for each alphabet in plain text, one character,a
pair of characters, or triplets of characters can be replaced to generate ciphertext.

Ex: A ciphertext can be S(replacement of one unit of character)


For A ciphertext can be ST(replacement of two unit of character)
For A cipher text can be STU(replacement of triplets of character)

3) Jumbled method: ​Characters of plain text can be jumbled so that cipher text can be
generated.
Ex: For the plain text ​AB​CD​E​FGHIJKLMNOPQ​R​STUVWXY​Z​,
ciphertext can be ZEBRACDFGHIJKLMNOPQSTUVWXY
4) Random replace: ​For each character in plain text, different units of characters can be
replaced so that cipher text can be generated.
EX: For the plain text​ ​ABCDEFGHIJKLMNOPQRSTUVWXYZ​,
cipher text can be DFIMRXEMVFQCPDSIZRKEZVSQPP.
INS LAB 160117733185

Algorithm:
1. ROT13:
1.1. Start
1.2. Input given plain text, and ciphertext
1.3. Increment and decrement each character by 13 in plaintext and ciphertext
respectively
1.4. Stop
2. Homphonic :
2.1. Start
2.2. Input plaintext, ciphertext and number of char to be substituted.
2.3. Substitute respective characters and encrypt and decrypt.
2.4. Stop

3. Jumbled:
3.1. Start
3.2. Input plaintext, cipher text, and keyword to be inserted to encrypt
3.3. Plaintext characters appearing in keyword are removed and keyword is appended
in the beginning
3.4. Stop
4. Random:
4.1. Start
4.2. Input plaintext, starting encrypt number and ciphertext
4.3. The plaintext characters are switched accordingly
4.4. Stop
INS LAB 160117733185

Code:
1) Rot13 Substitution:
class Rot13:

def __init__(self, pT, eT):


self.pT=pT
self.eT=eT

def encrypt(self):
op=''
for i in pT:
op+=chr((ord(i)-65+13)%26+65)
print(op)
def decrypt(self):
op=''
for i in eT:
op+=chr((ord(i)-65-13+26)%26+65)
print(op)

pT=input("Enter string to be encrypted: ")


eT=input("Enter String to be decrypted: ")
r = Rot13(pT,eT)
print("The encrypted form of "+pT+" is ")
r.encrypt()
print("The decrypted form of "+eT+" is ")
r.decrypt()

Output:
INS LAB 160117733185

2)Homophobic Substitution:

class Homo:

def __init__(self, pT, key, eT):


self.pT=pT
self.key=key
self.eT=eT

def encrypt(self):
op=''
for i in pT:
j= 0
k=key
while k>0:
op+=chr((ord(i)-65+13+j)%26+65)
k-=1
j+=1
print(op)

def decrypt(self):
op=''
i=1
while i<=len(eT):
op+=chr((ord(eT[i-1])-65-13+26)%26+65)
i+=key
print(op)

pT=input("Enter string to be encrypted: ")


key=int(input("Enter the no of characters to be substituted"))
eT=input("Enter String to be decrypted: ")
r = Homo(pT, key, eT)
print("The encrypted form of "+pT+" is ")
r.encrypt()
print("The decrypted form of "+eT+" is ")
r.decrypt()
INS LAB 160117733185

Output:
INS LAB 160117733185

3) Jumbled Substitution:
arr=[]
class Jumble:

def __init__(self, pT, keyword, eT):


self.pT=pT
self.keyword=keyword
self.eT=eT

def encrypt(self):
op=keyword
pos=0

for i in pT:

if i in keyword:
arr.append(pos)

else:
op+=i
pos+=1
print(op)

def decrypt(self):
op=''
pos=0
i= len(keyword)
temp=0
while i< len(eT):
if i==len(eT)-1 and pos<len(arr):
op+=eT[i]
op+=pT[arr[pos]]
break

if (eT[i] not in keyword and pos<len(arr)) and i>=arr[pos]<=temp:


op+=pT[arr[pos]]
pos+=1
INS LAB 160117733185

temp+=1

#print('hi'+op+' '+str(arr[pos])+' '+str(i))


continue
else:

op+=eT[i]
temp+=1
i+=1

print(op)

pT=input("Enter string to be encrypted: ")


keyword=input("Enter the keyword to be substituted: ")
eT=input("Enter String to be decrypted: ")
r = Jumble(pT, keyword, eT)
print("The encrypted form of "+pT+" is ")
r.encrypt()
print("The decrypted form of "+eT+" is ")
r.decrypt()

Output:
INS LAB 160117733185

4) Random substitution :

class Rand:

def __init__(self, pT, s, eT):


self.pT=pT
self.s=s
self.eT=eT

def encrypt(self):
op=''
j=s
k=1
for i in pT:
op+=chr((ord(i)-65+j)%26+65)
j+=k
k+=1

print(op)

def decrypt(self):
op=''
j=s
k=1
for i in eT:

op+=chr((ord(i)-65+26-j)%26+65)
j+=k
k+=1
print(op)

pT=input("Enter string to be encrypted: ")


s=int(input("Enter starting encryption code: "))
eT=input("Enter String to be decrypted: ")
r = Rand(pT, s, eT)
print("The encrypted form of "+pT+" is ")
r.encrypt()
INS LAB 160117733185

print("The decrypted form of "+eT+" is ")


r.decrypt()

Output:

Conclusion: ​Hence, all the cipher substitution methods are implemented doing both encryption
and decryption of the given text in python.

S-ar putea să vă placă și