block = xor(block, self.get_round_key(0)) return block def aes128_reverse_keyschedule(key, round): for i in range(round, 0, -1): for j in range(15, 3, -1): key[j] ^= key[j - 4] for j in range(3, -1, -1): key[j] ^= aes_sbox[key[12 + (j + 1) % 4]] ^ (0 if j else aes_Rcon[i]) return key if __name__ == '__main__': print 'Running test...' import Crypto.Cipher.AES key = ''.join(map(chr, range(16))) plain = ''.join(map(chr, range(16, 32))) assert len(key) == len(plain) == 16 cipher = AES(key) cipher2 = Crypto.Cipher.AES.new(key) assert (list(cipher.encrypt(plain)) == map(ord, cipher2.encrypt(plain))) assert (list(cipher.decrypt(plain)) == map(ord, cipher2.decrypt(plain))) for round in range(11): assert aes128_reverse_keyschedule(cipher.get_round_key(round), round) == ary(key)