import sys
#Suppose you are told that the one time pad
#encryption of the message "attack at dawn"
#is 6c73d5240a948c86981bc294814d
#(the plaintext letters are encoded
# as 8-bit ASCII and the given ciphertext
# is written in hex). What would be the one
#time pad encryption of the message "attack
#at dusk" under the same OTP key?
#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 printAscii(msg):
z = [chr(ord(x)) for x in msg]
x = "".join(z)
print x.encode('hex')
def main():
text = "attack at dawn"
enc = "6c73d5240a948c86981bc294814d".decode('hex')
key = strxor(text, enc)
text2 = "attack at dusk"
enc2 = strxor(text2, key)
print enc2.encode('hex')
main()
aW1wb3J0IHN5cwoKI1N1cHBvc2UgeW91IGFyZSB0b2xkIHRoYXQgdGhlIG9uZSB0aW1lIHBhZCAKI2VuY3J5cHRpb24gb2YgdGhlIG1lc3NhZ2UgImF0dGFjayBhdCBkYXduIiAKI2lzIDZjNzNkNTI0MGE5NDhjODY5ODFiYzI5NDgxNGQgCiModGhlIHBsYWludGV4dCBsZXR0ZXJzIGFyZSBlbmNvZGVkIAojICAgIGFzIDgtYml0IEFTQ0lJIGFuZCB0aGUgZ2l2ZW4gY2lwaGVydGV4dAojIGlzIHdyaXR0ZW4gaW4gaGV4KS4gV2hhdCB3b3VsZCBiZSB0aGUgb25lIAojdGltZSBwYWQgZW5jcnlwdGlvbiBvZiB0aGUgbWVzc2FnZSAiYXR0YWNrIAojYXQgZHVzayIgdW5kZXIgdGhlIHNhbWUgT1RQIGtleT8KCgojTVNHUyA9ICggLS0tICAxMSBzZWNyZXQgbWVzc2FnZXMgIC0tLSApCgpkZWYgc3RyeG9yKGEsIGIpOiAgICAgIyB4b3IgdHdvIHN0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