#!/usr/bin/env python
# -*- coding: utf-8 -*-

from binascii import unhexlify, hexlify
from Crypto.Cipher import XOR, AES

key = unhexlify("1058B9F9879504918ECC03F0D0233EE2D1426793DA8D95C19AA3150320C01537")
block_size = 16
known_plaintext = ('0000%.08d eur', '0000%.08d usd', '0000%.08d rur')

def main():
    fd = open('transaction.enc', 'rb')
    fd.seek(block_size*2)
    buf1 = fd.read(block_size)
    fd.seek(block_size*4)
    buf2 = fd.read(block_size)
    fd.seek(block_size*6)
    buf3 = fd.read(block_size)
    fd.close()
    for tmpl in known_plaintext:
        for x in range(0, 100000000):
            c = XOR.new(buf1)
            res1 = c.decrypt(tmpl % x)
            c = XOR.new(buf2)
            test = c.decrypt(enrij(enrij(res1)))
            if test.find(' usd') > 0 or \
              test.find(' eur') > 0 or test.find(' rur') > 0:
                print 'IV candidate: %s' % hexlify(rij(rij(rij(res1))))

def rij(buf):
    c = AES.new(key)
    return c.decrypt(buf)

def enrij(buf):
    c = AES.new(key)
    return c.encrypt(buf)

if __name__ == "__main__":
    main()

