<s> """<STR_LIT>""" <EOL> from array import array <EOL> MODE_ECB = <NUM_LIT:1> <EOL> MODE_CBC = <NUM_LIT:2> <EOL> block_size = <NUM_LIT:16> <EOL> key_size = None <EOL> def new ( key , mode = MODE_CBC , IV = None ) : <EOL> if mode == MODE_ECB : <EOL> return ECBMode ( AES ( key ) ) <EOL> elif mode == MODE_CBC : <EOL> if IV is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return CBCMode ( AES ( key ) , IV ) <EOL> else : <EOL> raise NotImplementedError <EOL> class AES ( object ) : <EOL> block_size = <NUM_LIT:16> <EOL> def __init__ ( self , key ) : <EOL> self . setkey ( key ) <EOL> def setkey ( self , key ) : <EOL> """<STR_LIT>""" <EOL> self . key = key <EOL> self . key_size = len ( key ) <EOL> if self . key_size == <NUM_LIT:16> : <EOL> self . rounds = <NUM_LIT:10> <EOL> elif self . key_size == <NUM_LIT> : <EOL> self . rounds = <NUM_LIT:12> <EOL> elif self . key_size == <NUM_LIT:32> : <EOL> self . rounds = <NUM_LIT> <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . expand_key ( ) <EOL> def expand_key ( self ) : <EOL> """<STR_LIT>""" <EOL> exkey = array ( '<STR_LIT:B>' , self . key ) <EOL> if self . key_size == <NUM_LIT:16> : <EOL> extra_cnt = <NUM_LIT:0> <EOL> elif self . key_size == <NUM_LIT> : <EOL> extra_cnt = <NUM_LIT:2> <EOL> else : <EOL> extra_cnt = <NUM_LIT:3> <EOL> word = exkey [ - <NUM_LIT:4> : ] <EOL> for i in xrange ( <NUM_LIT:1> , <NUM_LIT:11> ) : <EOL> word = word [ <NUM_LIT:1> : <NUM_LIT:4> ] + word [ <NUM_LIT:0> : <NUM_LIT:1> ] <EOL> for j in xrange ( <NUM_LIT:4> ) : <EOL> word [ j ] = aes_sbox [ word [ j ] ] <EOL> word [ <NUM_LIT:0> ] = word [ <NUM_LIT:0> ] ^ aes_Rcon [ i ] <EOL> for z in xrange ( <NUM_LIT:4> ) : <EOL> for j in xrange ( <NUM_LIT:4> ) : <EOL> word [ j ] ^= exkey [ - self . key_size + j ] <EOL> exkey . extend ( word ) <EOL> if len ( exkey ) >= ( self . rounds + <NUM_LIT:1> ) * self . block_size : <EOL> break <EOL> if self . key_size == <NUM_LIT:32> : <EOL> for j in xrange ( <NUM_LIT:4> ) : <EOL> word [ j ] = aes_sbox [ word [ j ] ] ^ exkey [ - self . key_size + j ] <EOL> exkey . extend ( word ) <EOL> for z in xrange ( extra_cnt ) : <EOL> for j in xrange ( <NUM_LIT:4> ) : <EOL> word [ j ] ^= exkey [ - self . key_size + j ] <EOL> exkey . extend ( word ) <EOL> self . exkey = exkey <EOL> def add_round_key ( self , block , round ) : <EOL> """<STR_LIT>""" <EOL> offset = round * <NUM_LIT:16> <EOL> exkey = self . exkey <EOL> for i in xrange ( <NUM_LIT:16> ) : <EOL> block [ i ] ^= exkey [ offset + i ] <EOL> def sub_bytes ( self , block , sbox ) : <EOL> """<STR_LIT>""" <EOL> for i in xrange ( <NUM_LIT:16> ) : <EOL> block [ i ] = sbox [ block [ i ] ] <EOL> def shift_rows ( self , b ) : <EOL> """<STR_LIT>""" <EOL> b [ <NUM_LIT:1> ] , b [ <NUM_LIT:5> ] , b [ <NUM_LIT:9> ] , b [ <NUM_LIT> ] = b [ <NUM_LIT:5> ] , b [ <NUM_LIT:9> ] , b [ <NUM_LIT> ] , b [ <NUM_LIT:1> ] <EOL> b [ <NUM_LIT:2> ] , b [ <NUM_LIT:6> ] , b [ <NUM_LIT:10> ] , b [ <NUM_LIT> ] = b [ <NUM_LIT:10> ] , b [ <NUM_LIT> ] , b [ <NUM_LIT:2> ] , b [ <NUM_LIT:6> ] <EOL> b [ <NUM_LIT:3> ] , b [ <NUM_LIT:7> ] , b [ <NUM_LIT:11> ] , b [ <NUM_LIT:15> ] = b [ <NUM_LIT:15> ] , b [ <NUM_LIT:3> ] , b [ <NUM_LIT:7> ] , b [ <NUM_LIT:11> ] <EOL> def shift_rows_inv ( self , b ) : <EOL> """<STR_LIT>""" <EOL> b [ <NUM_LIT:5> ] , b [ <NUM_LIT:9> ] , b [ <NUM_LIT> ] , b [ <NUM_LIT:1> ] = b [ <NUM_LIT:1> ] , b [ <NUM_LIT:5> ] , b [ <NUM_LIT:9> ] , b [ <NUM_LIT> ] <EOL> b [ <NUM_LIT:10> ] , b [ <NUM_LIT> ] , b [ <NUM_LIT:2> ] , b [ <NUM_LIT:6> ] = b [ <NUM_LIT:2> ] , b [ <NUM_LIT:6> ] , b [ <NUM_LIT:10> ] , b [ <NUM_LIT> ] <EOL> b [ <NUM_LIT:15> ] , b [ <NUM_LIT:3> ] , b [ <NUM_LIT:7> ] , b [ <NUM_LIT:11> ] = b [ <NUM_LIT:3> ] , b [ <NUM_LIT:7> ] , b [ <NUM_LIT:11> ] , b [ <NUM_LIT:15> ] <EOL> def mix_columns ( self , block ) : <EOL> """<STR_LIT>""" <EOL> mul_by_2 = gf_mul_by_2 <EOL> mul_by_3 = gf_mul_by_3 <EOL> for i in xrange ( <NUM_LIT:4> ) : <EOL> col = i * <NUM_LIT:4> <EOL> v0 , v1 , v2 , v3 = ( block [ col ] , block [ col + <NUM_LIT:1> ] , block [ col + <NUM_LIT:2> ] , <EOL> block [ col + <NUM_LIT:3> ] ) <EOL> block [ col ] = mul_by_2 [ v0 ] ^ v3 ^ v2 ^ mul_by_3 [ v1 ] <EOL> block [ col + <NUM_LIT:1> ] = mul_by_2 [ v1 ] ^ v0 ^ v3 ^ mul_by_3 [ v2 ] <EOL> block [ col + <NUM_LIT:2> ] = mul_by_2 [ v2 ] ^ v1 ^ v0 ^ mul_by_3 [ v3 ] <EOL> block [ col + <NUM_LIT:3> ] = mul_by_2 [ v3 ] ^ v2 ^ v1 ^ mul_by_3 [ v0 ] <EOL> def mix_columns_inv ( self , block ) : <EOL> """<STR_LIT>""" <EOL> mul_9 = gf_mul_by_9 <EOL> mul_11 = gf_mul_by_11 <EOL> mul_13 = gf_mul_by_13 <EOL> mul_14 = gf_mul_by_14 <EOL> for i in xrange ( <NUM_LIT:4> ) : <EOL> col = i * <NUM_LIT:4> <EOL> v0 , v1 , v2 , v3 = ( block [ col ] , block [ col + <NUM_LIT:1> ] , block [ col + <NUM_LIT:2> ] , <EOL> block [ col + <NUM_LIT:3> ] ) <EOL> block [ col ] = mul_14 [ v0 ] ^ mul_9 [ v3 ] ^ mul_13 [ v2 ] ^ mul_11 [ v1 ] <EOL> block [ col + <NUM_LIT:1> ] = mul_14 [ v1 ] ^ mul_9 [ v0 ] ^ mul_13 [ v3 ] ^ mul_11 [ v2 ] <EOL> block [ col + <NUM_LIT:2> ] = mul_14 [ v2 ] ^ mul_9 [ v1 ] ^ mul_13 [ v0 ] ^ mul_11 [ v3 ] <EOL> block [ col + <NUM_LIT:3> ] = mul_14 [ v3 ] ^ mul_9 [ v2 ] ^ mul_13 [ v1 ] ^ mul_11 [ v0 ] <EOL> def encrypt_block ( self , block ) : <EOL> """<STR_LIT>""" <EOL> self . add_round_key ( block , <NUM_LIT:0> ) <EOL> for round in xrange ( <NUM_LIT:1> , self . rounds ) : <EOL> self . sub_bytes ( block , aes_sbox ) <EOL> self . shift_rows ( block ) <EOL> self . mix_columns ( block ) <EOL> self . add_round_key ( block , round ) <EOL> self . sub_bytes ( block , aes_sbox ) <EOL> self . shift_rows ( block ) <EOL> self . add_round_key ( block , self . rounds ) <EOL> def decrypt_block ( self , block ) : <EOL> """<STR_LIT>""" <EOL> self . add_round_key ( block , self . rounds ) <EOL> for round in xrange ( self . rounds - <NUM_LIT:1> , <NUM_LIT:0> , - <NUM_LIT:1> ) : <EOL> self . shift_rows_inv ( block ) <EOL> self . sub_bytes ( block , aes_inv_sbox ) <EOL> self . add_round_key ( block , round ) <EOL> self . mix_columns_inv ( block ) <EOL> self . shift_rows_inv ( block ) <EOL> self . sub_bytes ( block , aes_inv_sbox ) <EOL> self . add_round_key ( block , <NUM_LIT:0> ) <EOL> class ECBMode ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cipher ) : <EOL> self . cipher = cipher <EOL> self . block_size = cipher . block_size <EOL> def ecb ( self , data , block_func ) : <EOL> """<STR_LIT>""" <EOL> if len ( data ) % self . block_size != <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> block_size = self . block_size <EOL> data = array ( '<STR_LIT:B>' , data ) <EOL> for offset in xrange ( <NUM_LIT:0> , len ( data ) , block_size ) : <EOL> block = data [ offset : offset + block_size ] <EOL> block_func ( block ) <EOL> data [ offset : offset + block_size ] = block <EOL> return data . tostring ( ) <EOL> def encrypt ( self , data ) : <EOL> """<STR_LIT>""" <EOL> return self . ecb ( data , self . cipher . encrypt_block ) <EOL> def decrypt ( self , data ) : <EOL> """<STR_LIT>""" <EOL> return self . ecb ( data , self . cipher . decrypt_block ) <EOL> class CBCMode ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cipher , IV ) : <EOL> self . cipher = cipher <EOL> self . block_size = cipher . block_size <EOL> self . IV = array ( '<STR_LIT:B>' , IV ) <EOL> def encrypt ( self , data ) : <EOL> """<STR_LIT>""" <EOL> block_size = self . block_size <EOL> if len ( data ) % block_size != <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> data = array ( '<STR_LIT:B>' , data ) <EOL> IV = self . IV <EOL> for offset in xrange ( <NUM_LIT:0> , len ( data ) , block_size ) : <EOL> block = data [ offset : offset + block_size ] <EOL> for i in xrange ( block_size ) : <EOL> block [ i ] ^= IV [ i ] <EOL> self . cipher . encrypt_block ( block ) <EOL> data [ offset : offset + block_size ] = block <EOL> IV = block <EOL> self . IV = IV <EOL> return data . tostring ( ) <EOL> def decrypt ( self , data ) : <EOL> """<STR_LIT>""" <EOL> block_size = self . block_size <EOL> if len ( data ) % block_size != <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> data = array ( '<STR_LIT:B>' , data ) <EOL> IV = self . IV <EOL> for offset in xrange ( <NUM_LIT:0> , len ( data ) , block_size ) : <EOL> ctext = data [ offset : offset + block_size ] <EOL> block = ctext [ : ] <EOL> self . cipher . decrypt_block ( block ) <EOL> for i in xrange ( block_size ) : <EOL> block [ i ] ^= IV [ i ] <EOL> data [ offset : offset + block_size ] = block <EOL> IV = ctext <EOL> self . IV = IV <EOL> return data . tostring ( ) <EOL> def galois_multiply ( a , b ) : <EOL> """<STR_LIT>""" <EOL> p = <NUM_LIT:0> <EOL> while b : <EOL> if b & <NUM_LIT:1> : <EOL> p ^= a <EOL> a <<= <NUM_LIT:1> <EOL> if a & <NUM_LIT> : <EOL> a ^= <NUM_LIT> <EOL> b >>= <NUM_LIT:1> <EOL> return p & <NUM_LIT> <EOL> gf_mul_by_2 = array ( '<STR_LIT:B>' , [ galois_multiply ( x , <NUM_LIT:2> ) for x in range ( <NUM_LIT> ) ] ) <EOL> gf_mul_by_3 = array ( '<STR_LIT:B>' , [ galois_multiply ( x , <NUM_LIT:3> ) for x in range ( <NUM_LIT> ) ] ) <EOL> gf_mul_by_9 = array ( '<STR_LIT:B>' , [ galois_multiply ( x , <NUM_LIT:9> ) for x in range ( <NUM_LIT> ) ] ) <EOL> gf_mul_by_11 = array ( '<STR_LIT:B>' , [ galois_multiply ( x , <NUM_LIT:11> ) for x in range ( <NUM_LIT> ) ] ) <EOL> gf_mul_by_13 = array ( '<STR_LIT:B>' , [ galois_multiply ( x , <NUM_LIT> ) for x in range ( <NUM_LIT> ) ] ) <EOL> gf_mul_by_14 = array ( '<STR_LIT:B>' , [ galois_multiply ( x , <NUM_LIT> ) for x in range ( <NUM_LIT> ) ] ) <EOL> aes_sbox = array ( '<STR_LIT:B>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . decode ( '<STR_LIT>' ) <EOL> ) <EOL> aes_inv_sbox = array ( '<STR_LIT:B>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . decode ( '<STR_LIT>' ) <EOL> ) <EOL> aes_Rcon = array ( '<STR_LIT:B>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . decode ( '<STR_LIT>' ) <EOL> ) </s>
