<s> import binascii <EOL> import json <EOL> import os <EOL> from Crypto . Cipher import AES <EOL> from keen import Padding <EOL> __author__ = '<STR_LIT>' <EOL> OLD_BLOCK_SIZE = <NUM_LIT:32> <EOL> def pad_aes256 ( s ) : <EOL> """<STR_LIT>""" <EOL> if len ( s ) % AES . block_size == <NUM_LIT:0> : <EOL> return s <EOL> return Padding . appendPadding ( s , blocksize = AES . block_size ) <EOL> def unpad_aes256 ( s ) : <EOL> """<STR_LIT>""" <EOL> if not s : <EOL> return s <EOL> try : <EOL> return Padding . removePadding ( s , blocksize = AES . block_size ) <EOL> except AssertionError : <EOL> return s <EOL> def old_pad ( s ) : <EOL> """<STR_LIT>""" <EOL> if len ( s ) % OLD_BLOCK_SIZE == <NUM_LIT:0> : <EOL> return s <EOL> return Padding . appendPadding ( s , blocksize = OLD_BLOCK_SIZE ) <EOL> def old_unpad ( s ) : <EOL> """<STR_LIT>""" <EOL> if not s : <EOL> return s <EOL> try : <EOL> return Padding . removePadding ( s , blocksize = OLD_BLOCK_SIZE ) <EOL> except AssertionError : <EOL> return s <EOL> def encode_aes256 ( key , plaintext ) : <EOL> """<STR_LIT>""" <EOL> if len ( key ) != <NUM_LIT:64> : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> iv = os . urandom ( AES . block_size ) <EOL> cipher = AES . new ( binascii . unhexlify ( key . encode ( '<STR_LIT:ascii>' ) ) , mode = AES . MODE_CBC , IV = iv ) <EOL> ciphertext = cipher . encrypt ( pad_aes256 ( plaintext ) ) <EOL> iv_plus_encrypted = binascii . hexlify ( iv ) + binascii . hexlify ( ciphertext ) <EOL> return iv_plus_encrypted <EOL> def decode_aes256 ( key , iv_plus_encrypted ) : <EOL> """<STR_LIT>""" <EOL> iv_size = <NUM_LIT:2> * AES . block_size <EOL> hexed_iv = iv_plus_encrypted [ : iv_size ] <EOL> hexed_ciphertext = iv_plus_encrypted [ iv_size : ] <EOL> iv = binascii . unhexlify ( hexed_iv ) <EOL> ciphertext = binascii . unhexlify ( hexed_ciphertext ) <EOL> cipher = AES . new ( binascii . unhexlify ( key . encode ( '<STR_LIT:ascii>' ) ) , mode = AES . MODE_CBC , IV = iv ) <EOL> plaintext = cipher . decrypt ( ciphertext ) <EOL> return unpad_aes256 ( plaintext ) <EOL> def old_encode_aes ( key , plaintext ) : <EOL> """<STR_LIT>""" <EOL> iv = os . urandom ( <NUM_LIT:16> ) <EOL> cipher = AES . new ( old_pad ( key ) , mode = AES . MODE_CBC , IV = iv ) <EOL> ciphertext = cipher . encrypt ( old_pad ( plaintext ) ) <EOL> iv_plus_encrypted = binascii . hexlify ( iv ) + binascii . hexlify ( ciphertext ) <EOL> return iv_plus_encrypted <EOL> def old_decode_aes ( key , iv_plus_encrypted ) : <EOL> """<STR_LIT>""" <EOL> hexed_iv = iv_plus_encrypted [ : <NUM_LIT:32> ] <EOL> hexed_ciphertext = iv_plus_encrypted [ <NUM_LIT:32> : ] <EOL> iv = binascii . unhexlify ( hexed_iv ) <EOL> ciphertext = binascii . unhexlify ( hexed_ciphertext ) <EOL> cipher = AES . new ( old_pad ( key ) , mode = AES . MODE_CBC , IV = iv ) <EOL> plaintext = cipher . decrypt ( ciphertext ) <EOL> return old_unpad ( plaintext ) <EOL> def encrypt ( api_key , options ) : <EOL> options_string = json . dumps ( options ) <EOL> if len ( api_key ) == <NUM_LIT:64> : <EOL> return encode_aes256 ( api_key , options_string ) <EOL> else : <EOL> return old_encode_aes ( api_key , options_string ) <EOL> def decrypt ( api_key , scoped_key ) : <EOL> if len ( api_key ) == <NUM_LIT:64> : <EOL> json_string = decode_aes256 ( api_key , scoped_key ) <EOL> else : <EOL> json_string = old_decode_aes ( api_key , scoped_key ) <EOL> try : <EOL> return json . loads ( json_string ) <EOL> except TypeError : <EOL> return json . loads ( json_string . decode ( ) ) </s>
