Friday, 10 April 2020

Python Program-9

(9) Implement classical ciphers using python. 1) Caesar cipher 2) Monoalphabetic cipher


Program Code-1:caesar cipher

#Lab-9-Implement classical ciphers using python.
#caesar cipher
def encrypt(text,s):
    result = ""
# transverse the plain text
for i in range(len(text)):
    char = text[i]
# Encrypt uppercase characters in plain text
    if (char.isupper()):
        result += chr((ord(char) + s-65) % 26 +65)
# Encrypt lowercase characters in plain text
    else:
        result += chr((ord(char) + s - 97) % 26 +97)
        return result
    
#check the above function
text = "CEASER CIPHER DEMO";
s = 4;

print ("Plain Text : " + text);
print ("Shift pattern : " + str(s));
print ("Cipher: " + encrypt(text,s));


Output-1:caesar cipher


Program Code-2:Monoalphabetic cipher

#Lab-9-Implement classical ciphers using python.
#Monoalphabetic cipher
keys={'a':'z','b':'y','c':'x','d':'w','e':'v','f':'u','g':'t','h':'s','i':'r','j':'q','k':'p','l':'o','m':'n'}

reverse_keys={}
for key,value in keys.items():
    reverse_keys[value]=key
def encrypt(text):
    text=str(text)
    encrypting=[]
    for l in text:
        encrypting.append(keys.get(l,l))
    print(''.join(encrypting))
def decipher(text):
    text=str(text)
    decrypted=[]
    for l in text:
        decrypted.append(reverse_keys.get(l,l))
    print(''.join(decrypted))
print(encrypt("Prime"))
print(decipher("gtu12"))

Output-2:Monoalphabetic cipher

No comments:

Post a Comment