<s> from __future__ import absolute_import , division , print_function <EOL> import binascii <EOL> from cryptography . hazmat . backends import default_backend <EOL> from cryptography . hazmat . primitives . ciphers import algorithms , base , modes <EOL> def encrypt ( mode , key , iv , plaintext ) : <EOL> cipher = base . Cipher ( <EOL> algorithms . CAST5 ( binascii . unhexlify ( key ) ) , <EOL> mode ( binascii . unhexlify ( iv ) ) , <EOL> default_backend ( ) <EOL> ) <EOL> encryptor = cipher . encryptor ( ) <EOL> ct = encryptor . update ( binascii . unhexlify ( plaintext ) ) <EOL> ct += encryptor . finalize ( ) <EOL> return binascii . hexlify ( ct ) <EOL> def build_vectors ( mode , filename ) : <EOL> vector_file = open ( filename , "<STR_LIT:r>" ) <EOL> count = <NUM_LIT:0> <EOL> output = [ ] <EOL> key = None <EOL> iv = None <EOL> plaintext = None <EOL> for line in vector_file : <EOL> line = line . strip ( ) <EOL> if line . startswith ( "<STR_LIT>" ) : <EOL> if count != <NUM_LIT:0> : <EOL> output . append ( "<STR_LIT>" . format ( <EOL> encrypt ( mode , key , iv , plaintext ) ) <EOL> ) <EOL> output . append ( "<STR_LIT>" . format ( count ) ) <EOL> count += <NUM_LIT:1> <EOL> name , key = line . split ( "<STR_LIT>" ) <EOL> output . append ( "<STR_LIT>" . format ( key ) ) <EOL> elif line . startswith ( "<STR_LIT>" ) : <EOL> name , iv = line . split ( "<STR_LIT>" ) <EOL> iv = iv [ <NUM_LIT:0> : <NUM_LIT:16> ] <EOL> output . append ( "<STR_LIT>" . format ( iv ) ) <EOL> elif line . startswith ( "<STR_LIT>" ) : <EOL> name , plaintext = line . split ( "<STR_LIT>" ) <EOL> output . append ( "<STR_LIT>" . format ( plaintext ) ) <EOL> output . append ( "<STR_LIT>" . format ( encrypt ( mode , key , iv , plaintext ) ) ) <EOL> return "<STR_LIT:\n>" . join ( output ) <EOL> def write_file ( data , filename ) : <EOL> with open ( filename , "<STR_LIT:w>" ) as f : <EOL> f . write ( data ) <EOL> cbc_path = "<STR_LIT>" <EOL> write_file ( build_vectors ( modes . CBC , cbc_path ) , "<STR_LIT>" ) <EOL> ofb_path = "<STR_LIT>" <EOL> write_file ( build_vectors ( modes . OFB , ofb_path ) , "<STR_LIT>" ) <EOL> cfb_path = "<STR_LIT>" <EOL> write_file ( build_vectors ( modes . CFB , cfb_path ) , "<STR_LIT>" ) <EOL> ctr_path = "<STR_LIT>" <EOL> write_file ( build_vectors ( modes . CTR , ctr_path ) , "<STR_LIT>" ) </s>
