Advertisement
Guest User

Untitled

a guest
Oct 10th, 2012
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. import sys, binascii, random
  2.  
  3. def bytes_xor(a,b):
  4.     # xor two bytestrings of different lengths,
  5.     # truncating the longest one if necessary
  6.     k = []
  7.     if len(a) > len(b):
  8.         for (x, y) in zip(a[:len(b)], b):
  9.             k.append(x ^ y)
  10.     else:
  11.         for (x, y) in zip(a, b[:len(a)]):
  12.             k.append(x ^ y)          
  13.     return bytes(k)
  14.  
  15. def random_key(size=16):
  16.     # make a random bytestring of specified length
  17.     ints = []
  18.     for i in range(size):
  19.         ints.append(random.randint(0x00,0xFF))
  20.     return bytes(ints)
  21.  
  22. def ascii_2_bytes(msg):
  23.     return bytearray(msg, 'ascii')
  24.  
  25. def encrypt(key, msg):
  26.     # returns the hexadecimal encoding of the resulting cipher
  27.     cipher = bytes_xor(key, ascii_2_bytes(msg))
  28.     return (str(binascii.hexlify(cipher))[2:-1])
  29.  
  30. if __name__ == '__main__':
  31.  
  32.     key = random_key(1024)
  33.     secret_messages = [ ... ] # you're not having them that easily
  34.  
  35.     for msg in secret_messages:
  36.         print (encrypt(key, msg))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement