Vernam Cypher in #Python

The Vernam Cypher is a type of 'one-time pad' symmetric cypher which makes use of the NEQ (not equivalence, or XOR Exclusive-OR bitwise operation).  Vernam Cypher is almost unbreakable using any brute force or statistical analysis using current technology, provided that the key remains secret and is only used for encryption and decryption once (hence 'one-time pad').

The cypher works by finding the Exclusive OR of each character with a key string.  Here it is in Python.

# Vernam cypher demo
# T Street 2015-10-16

def makeVernamCypher( text, key ):
    """ Returns the Vernam Cypher for given string and key """
    answer = "" # the Cypher text
    p = 0 # pointer for the key
    for char in text:
        answer += chr(ord(char) ^ ord(key[p]))
        p += 1
        if p==len(key):
            p = 0
    return answer

                      
MY_KEY = "cvwopslweinedvq9fnasdlkfn2"
while True:
    print("\n\n---Vernam Cypher---")
    PlainText = input("Enter text to encrypt: ")
    # Encrypt
    Cypher = makeVernamCypher(PlainText, MY_KEY)
    print("Cypher text: "+Cypher)
    # Decrypt
    decrypt = makeVernamCypher(Cypher, MY_KEY)
    print("Decrypt: "+decrypt)


Demo shows the text has been encrypted and then decrypted again.

Join us again soon for more nerdy stuff.




#Cyper #Vernam #Encryption