Monday, February 13, 2017

Message Encryption and Decryption Python

Tags

Suppose you want to send a message to a friend,but you don't want other people to be able to read it
To do this you must disguise the text in some fashion.This Process is called Encryption.The reverse process is called Decryption.

Code::

def encrypt(msg):
    result=''
    for c in msg:
        result=result+" "+str(ord(c))
    return result

def decrypt(msg):
    result=''
    for c in msg.split():
        result=result+chr(int(c))
    return result


msg=input("Enter Secret Message: ")
emsg=encrypt(msg)
print("Encrypted Message: "+emsg)

print("Decrypted Message: "+decrypt(emsg))

Output::


Another method is using rotate 13 i.e convert the entered character to character 13 place ahead that character.


Code::                                                                                                                                       

def char13(c):
    alph="abcdefghijklmnopqrstuvwxyz"
    index=alph.find(c)
    index=(index+13)%26
    return alph[index]

def encrypt(text):
    result=''
    for c in text.lower():
        if 'a'<=c<='z':result=result+char13(c)
        else: result=result+c
    return result

msg=input("Enter Secret Message: ")
emsg=encrypt(msg)
print("Encrypted Message: "+emsg)
print("Decrypted Message: "+encrypt(emsg))


Output::