<s> """<STR_LIT>""" <EOL> import sys <EOL> if sys . version_info [ <NUM_LIT:0> ] == <NUM_LIT:2> and sys . version_info [ <NUM_LIT:1> ] == <NUM_LIT:1> : <EOL> from Crypto . Util . py21compat import * <EOL> from Crypto . Util . py3compat import * <EOL> MODE_ECB = <NUM_LIT:1> <EOL> MODE_CBC = <NUM_LIT:2> <EOL> MODE_CFB = <NUM_LIT:3> <EOL> MODE_PGP = <NUM_LIT:4> <EOL> MODE_OFB = <NUM_LIT:5> <EOL> MODE_CTR = <NUM_LIT:6> <EOL> MODE_OPENPGP = <NUM_LIT:7> <EOL> def _getParameter ( name , index , args , kwargs , default = None ) : <EOL> """<STR_LIT>""" <EOL> param = kwargs . get ( name ) <EOL> if len ( args ) > index : <EOL> if param : <EOL> raise ValueError ( "<STR_LIT>" % name ) <EOL> param = args [ index ] <EOL> return param or default <EOL> class BlockAlgo : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , factory , key , * args , ** kwargs ) : <EOL> self . mode = _getParameter ( '<STR_LIT>' , <NUM_LIT:0> , args , kwargs , default = MODE_ECB ) <EOL> self . block_size = factory . block_size <EOL> if self . mode != MODE_OPENPGP : <EOL> self . _cipher = factory . new ( key , * args , ** kwargs ) <EOL> self . IV = self . _cipher . IV <EOL> else : <EOL> self . _done_first_block = False <EOL> self . _done_last_block = False <EOL> self . IV = _getParameter ( '<STR_LIT>' , <NUM_LIT:1> , args , kwargs ) <EOL> if not self . IV : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> IV_cipher = factory . new ( key , MODE_CFB , <EOL> b ( '<STR_LIT:\x00>' ) * self . block_size , <EOL> segment_size = self . block_size * <NUM_LIT:8> ) <EOL> if len ( self . IV ) == self . block_size : <EOL> self . _encrypted_IV = IV_cipher . encrypt ( <EOL> self . IV + self . IV [ - <NUM_LIT:2> : ] + <EOL> b ( '<STR_LIT:\x00>' ) * ( self . block_size - <NUM_LIT:2> ) <EOL> ) [ : self . block_size + <NUM_LIT:2> ] <EOL> elif len ( self . IV ) == self . block_size + <NUM_LIT:2> : <EOL> self . _encrypted_IV = self . IV <EOL> self . IV = IV_cipher . decrypt ( self . IV + <EOL> b ( '<STR_LIT:\x00>' ) * ( self . block_size - <NUM_LIT:2> ) <EOL> ) [ : self . block_size + <NUM_LIT:2> ] <EOL> if self . IV [ - <NUM_LIT:2> : ] != self . IV [ - <NUM_LIT:4> : - <NUM_LIT:2> ] : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . IV = self . IV [ : - <NUM_LIT:2> ] <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" <EOL> % ( self . block_size , self . block_size + <NUM_LIT:2> ) ) <EOL> self . _cipher = factory . new ( key , MODE_CFB , <EOL> self . _encrypted_IV [ - self . block_size : ] , <EOL> segment_size = self . block_size * <NUM_LIT:8> ) <EOL> def encrypt ( self , plaintext ) : <EOL> """<STR_LIT>""" <EOL> if self . mode == MODE_OPENPGP : <EOL> padding_length = ( self . block_size - len ( plaintext ) % self . block_size ) % self . block_size <EOL> if padding_length > <NUM_LIT:0> : <EOL> if self . _done_last_block : <EOL> raise ValueError ( "<STR_LIT>" , <EOL> self . block_size ) <EOL> self . _done_last_block = True <EOL> padded = plaintext + b ( '<STR_LIT:\x00>' ) * padding_length <EOL> res = self . _cipher . encrypt ( padded ) [ : len ( plaintext ) ] <EOL> else : <EOL> res = self . _cipher . encrypt ( plaintext ) <EOL> if not self . _done_first_block : <EOL> res = self . _encrypted_IV + res <EOL> self . _done_first_block = True <EOL> return res <EOL> return self . _cipher . encrypt ( plaintext ) <EOL> def decrypt ( self , ciphertext ) : <EOL> """<STR_LIT>""" <EOL> if self . mode == MODE_OPENPGP : <EOL> padding_length = ( self . block_size - len ( ciphertext ) % self . block_size ) % self . block_size <EOL> if padding_length > <NUM_LIT:0> : <EOL> if self . _done_last_block : <EOL> raise ValueError ( "<STR_LIT>" , <EOL> self . block_size ) <EOL> self . _done_last_block = True <EOL> padded = ciphertext + b ( '<STR_LIT:\x00>' ) * padding_length <EOL> res = self . _cipher . decrypt ( padded ) [ : len ( ciphertext ) ] <EOL> else : <EOL> res = self . _cipher . decrypt ( ciphertext ) <EOL> return res <EOL> return self . _cipher . decrypt ( ciphertext ) </s>
