fork download
  1. import sys
  2.  
  3. #Suppose you are told that the one time pad
  4. #encryption of the message "attack at dawn"
  5. #is 09e1c5f70a65ac519458e7e53f36
  6. #(the plaintext letters are encoded
  7. # as 8-bit ASCII and the given ciphertext
  8. # is written in hex). What would be the one
  9. #time pad encryption of the message "attack
  10. #at dusk" under the same OTP key?
  11.  
  12.  
  13. #MSGS = ( --- 11 secret messages --- )
  14.  
  15. def strxor(a, b): # xor two strings of different lengths
  16. if len(a) > len(b):
  17. return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a[:len(b)], b)])
  18. else:
  19. return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a, b[:len(a)])])
  20.  
  21. def random(size=16):
  22. return open("/dev/urandom").read(size)
  23.  
  24. def encrypt(key, msg):
  25. c = strxor(key, msg)
  26. print
  27. print c.encode('hex')
  28. return c
  29.  
  30. def printAscii(msg):
  31. z = [chr(ord(x)) for x in msg]
  32. x = "".join(z)
  33. print x.encode('hex')
  34.  
  35. def main():
  36. text = "attack at dawn"
  37.  
  38.  
  39. enc = "09e1c5f70a65ac519458e7e53f36".decode('hex')
  40. key = strxor(text, enc)
  41.  
  42.  
  43. text2 = "attack at dusk"
  44. enc2 = strxor(text2, key)
  45.  
  46. print enc2.encode('hex')
  47.  
  48.  
  49.  
  50. main()
Success #stdin #stdout 0.02s 7200KB
stdin
import sys

MSGS = ( ---  11 secret messages  --- )

def strxor(a, b):     # xor two strings of different lengths
    if len(a) > len(b):
        return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a[:len(b)], b)])
    else:
        return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a, b[:len(a)])])

def random(size=16):
    return open("/dev/urandom").read(size)

def encrypt(key, msg):
    c = strxor(key, msg)
    print
    print c.encode('hex')
    return c

def main():
    key = random(1024)
    ciphertexts = [encrypt(key, msg) for msg in MSGS]
stdout
09e1c5f70a65ac519458e7f13b33