#!/usr/bin/python import hashlib, string, sys ROUNDS = 20000 def xor(a, b): l = min(len(a), len(b)) return ''.join([chr(ord(x) ^ ord(y)) for x, y in zip(a[:l], b[:l])]) def h(x): x = hashlib.sha256(x).digest() x = xor(x[:16], x[16:]) return x def verify(x): return all([ord(c) < 127 for c in x]) def crypt(msg, passwd): k = h(passwd) for i in xrange(ROUNDS): k = h(k) out = '' for i in xrange(0, len(msg), 16): out += xor(msg[i:i+16], k) k = h(k + str(len(msg))) return out def encrypt(msg, passwd): msg = crypt(msg, passwd) return msg.encode('base64') def decrypt(msg, passwd): msg = crypt(msg.decode('base64'), passwd) if verify(msg): return msg else: sys.stderr.write('Looks like a bad decrypt\n') sys.exit(1) def decrypt2(msg, passwd): msg = crypt2(msg.decode('base64'), passwd) if verify(msg): return msg else: sys.stderr.write('Looks like a bad decrypt\n') sys.exit(1) def crypt2(msg, k): out = '' for i in xrange(0, len(msg), 16): out += xor(msg[i:i+16], k) k = h(k + str(len(msg))) return out def crack(msg): plain = "From: Vlugge Japie " msg = msg.decode('base64') key = "" for pos in range(16): for w in range(256): passwd = chr(w) * 32 k = passwd out = '' for i in xrange(0, len(msg), 16): out += xor(msg[i:i+16], k) if out[pos] == plain[pos]: print "POS " + str(pos) + ": chr(" + str(w) + ")" key = key + chr(w) return key def h2(x): x = xor(x[:16], x[16:]) return x inp = open("msg002.enc").read() key = crack(inp) res = decrypt2(inp, key) print res