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 6c73d5240a948c86981bc294814d
  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 = "6c73d5240a948c86981bc294814d".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.01s 7220KB
stdin
Standard input is empty
stdout
6c73d5240a948c86981bc2808548