Thursday, April 25, 2019

Encryption and Decryption using Caesar Cipher with Odd Even System - System Security | Python3

-> Encryption procedure: If the alphabet index is even then increase the value by one else decrease the key value by one.
-> Decryption Procedure: If the alphabet index is even then decrypting the value by one else increase the key value by one.
-> Code:
a=input("Enter the msg: ")
key=int(input("Enter the key: "))
ct=[]
pt=[]

for i in a:
    if ord(i)>=97 and ord(i)<=122:
        if(ord(i)%2) == 0:    
            d=(((ord(i)+key)-97)%26)+97
            ct.append(chr(d))
        else:
            d=(((ord(i)-key)-97)%26)+97
            ct.append(chr(d))
    elif ord(i)>=62 and ord(i)<=90:
        if(ord(i)%2) == 0:    
            d=(((ord(i)+key)-62)%26)+62
            ct.append(chr(d))
        else:
            d=(((ord(i)-key)-62)%26)+62
            ct.append(chr(d))
    else:
        ct.append(i)
        
print ("Encrypted Data")  
str1 = ''.join(ct)
print (str1)

for i in ct:
    if ord(i)>=97 and ord(i)<=122:
        if(ord(i)%2) == 0:    
            d=(((ord(i)+key)-97)%26)+97
            pt.append(chr(d))
        else:
            d=(((ord(i)-key)-97)%26)+97
            pt.append(chr(d))   
    elif ord(i)>=62 and ord(i)<=90:
        if(ord(i)%2) == 0:    
            d=(((ord(i)+key)-62)%26)+62
            pt.append(chr(d))
        else:
            d=(((ord(i)-key)-62)%26)+62
            pt.append(chr(d))
    else:
        pt.append(i)
            
print ("Decrypted Data")    
str2 = ''.join(pt)
print (str2)

Screenshot:


No comments:

Post a Comment