Created
August 2, 2012 17:04
-
-
Save theonewolf/3238739 to your computer and use it in GitHub Desktop.
Vernam XOR Stream Cipher with the Mauborgne Constraint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def vernam_decrypt(ciphertext, key): | |
return bytearray( | |
[ ciphertext[i] ^ key[i] | |
for i in xrange(len(ciphertext)) | |
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def vernam_encrypt(plaintext, key): | |
return bytearray( | |
[ plaintext[i] ^ key[i] | |
for i in xrange(len(plaintext)) | |
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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