Friday, July 12, 2019

Encryption and decryption in Caeser Cipher using python program

#encryption method for caeser cipher .....
def enc(text,key):
    result= ""
    for i in range (len(text)):
        char= text[i]
        if((ord(char)>64 and ord(char)<92) or (ord(char)>96 and ord(char)<124)):
            if (char.isupper()):
                result += chr((ord(char) +key-65)%26+65)
            else:
                result += chr((ord(char) +key-97)%26+97)
        else:
            print("its symbol...")     
    return result;

#decryption method for caeser cipher 
def dec(text,key):
    result= ""
    for i in range (len(text)):
        char= text[i]
        if (char.isupper()):
            result += chr((ord(char) -key-65)%26+65)
        else:
            result += chr((ord(char) -97-key)%26+97)
    return result;

"""call for these two methods """

pt="Talwinder Mann XYZ ABC HHH"
key=9;
ct= enc(pt,key);

print(" plain text is "+pt)
print(" cipher text text is %s \n and key : %s"%(ct,key))

ppt=dec(ct,key)
print(" plain text is "+ppt)

Output for the program...

 Hacking of caeser cipher, try to crack the cipher text with all possible keys 1-26.

#hacking of caeser cipher, test all valid keys 1-26

for i in range (26):
    pt_temp=dec(ct,i)
    print (" using key :%s the Plain text are : %s"%(i,pt_temp))

Output of program test all possible keys, to decrypt the caeser cipher's encrypted text 


No comments:

Post a Comment

Write a program in python which determine determines that the number is prime or not. What is your favorite number?

Write a program in python which determine determines that the number is prime or not. What is your favorite number? Code   number = int(inpu...