Skip to content

Instantly share code, notes, and snippets.

@theonewolf
Created August 2, 2012 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theonewolf/3238739 to your computer and use it in GitHub Desktop.
Save theonewolf/3238739 to your computer and use it in GitHub Desktop.
Vernam XOR Stream Cipher with the Mauborgne Constraint
def vernam_decrypt(ciphertext, key):
return bytearray(
[ ciphertext[i] ^ key[i]
for i in xrange(len(ciphertext))
])
#!/usr/bin/env python
from os import urandom
def vernam_genkey(length):
return bytearray(urandom(length))
def vernam_encrypt(plaintext, key):
return bytearray(
[ plaintext[i] ^ key[i]
for i in xrange(len(plaintext))
])
def vernam_decrypt(ciphertext, key):
return bytearray(
[ ciphertext[i] ^ key[i]
for i in xrange(len(ciphertext))
])
plaintext = bytearray('encrypt me')
key = vernam_genkey(len(plaintext))
print 'Before encrypting: ' + plaintext
ciphertext = vernam_encrypt(plaintext, key)
print 'After encrypting: ' + ciphertext
plaintext2 = vernam_decrypt(ciphertext, key)
print 'After decrypting: ' + plaintext2
def vernam_encrypt(plaintext, key):
return bytearray(
[ plaintext[i] ^ key[i]
for i in xrange(len(plaintext))
])
from os import urandom
def vernam_genkey(length):
return bytearray(urandom(length))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment