
    Eh41                         d Z ddlZej                  d   dk(  rej                  d   dk(  rddl ddl dZdZdZdZdZ	d	Z
d
ZddZ G d d      Zy)z4Module with definitions common to all block ciphers.    N      )*               c                 v    |j                  |       }t        |      |kD  r|rt        d| z        ||   }|xs |S )zFFind a parameter in tuple and dictionary arguments a function receivesz!Parameter '%s' is specified twice)getlen
ValueError)nameindexargskwargsdefaultparams         X/var/www/html/bid_assistant/venv/lib/python3.12/site-packages/Crypto/Cipher/blockalgo.py_getParameterr   |   sC    JJtE
4y@4GHHUG    c                   "    e Zd ZdZd Zd Zd Zy)	BlockAlgoz)Class modelling an abstract block cipher.c                 (   t        dd||t              | _        |j                  | _        | j                  t        k7  r7 |j
                  |g|i || _        | j                  j                  | _        y d| _        d| _	        t        dd||      | _        | j                  st        d      |j                  |t        t        d      | j                  z  | j                  d	z  
      }t        | j                        | j                  k(  r]|j                  | j                  | j                  dd  z   t        d      | j                  dz
  z  z         d | j                  dz    | _        nt        | j                        | j                  dz   k(  r| j                  | _        |j!                  | j                  t        d      | j                  dz
  z  z         d | j                  dz    | _        | j                  dd  | j                  dd k7  rt        d      | j                  d d | _        n't        d| j                  | j                  dz   fz        |j                  |t        | j                  | j                   d  | j                  d	z  
      | _        y )Nmoder   )r   Fivr   zMODE_OPENPGP requires an IV    )segment_sizer   z%Failed integrity check for OPENPGP IVz4Length of IV must be %d or %d bytes for MODE_OPENPGP)r   MODE_ECBr   
block_sizeMODE_OPENPGPnew_cipherIV_done_first_block_done_last_blockr   MODE_CFBbr   encrypt_encrypted_IVdecrypt)selffactorykeyr   r   	IV_ciphers         r   __init__zBlockAlgo.__init__   sD   !&!T68L	!,,99$&7;;s<T<V<DLllooDG &+D"$)D!#D!T6:DG77 !>??  CfIdoo-!%!2 $ 4I
 477|t.%.%6%6GGdggbcl*fItq012& )tq(&*" TWW!22%)WW"#++DGGfItq01-2 (tq(* 7723<4772b>1$%LMM''#2, !W(9:"; < < #;;sH""DOO#3#45!__Q. ' 0DLr   c                    | j                   t        k(  r| j                  t        |      | j                  z  z
  | j                  z  }|dkD  rb| j                  rt        d| j                        d| _        |t        d      |z  z   }| j                  j                  |      dt        |       }n| j                  j                  |      }| j                  s| j                  |z   }d| _	        |S | j                  j                  |      S )aX  Encrypt data with the key and the parameters set at initialization.
        
        The cipher object is stateful; encryption of a long block
        of data can be broken up in two or more calls to `encrypt()`.
        That is, the statement:
            
            >>> c.encrypt(a) + c.encrypt(b)

        is always equivalent to:

             >>> c.encrypt(a+b)

        That also means that you cannot reuse an object for encrypting
        or decrypting other data with the same key.

        This function does not perform any padding.
       
         - For `MODE_ECB`, `MODE_CBC`, and `MODE_OFB`, *plaintext* length
           (in bytes) must be a multiple of *block_size*.

         - For `MODE_CFB`, *plaintext* length (in bytes) must be a multiple
           of *segment_size*/8.

         - For `MODE_CTR`, *plaintext* can be of any length.

         - For `MODE_OPENPGP`, *plaintext* must be a multiple of *block_size*,
           unless it is the last chunk of the message.

        :Parameters:
          plaintext : byte string
            The piece of data to encrypt.
        :Return:
            the encrypted data, as a byte string. It is as long as
            *plaintext* with one exception: when encrypting the first message
            chunk with `MODE_OPENPGP`, the encypted IV is prepended to the
            returned ciphertext.
        r   FOnly the last chunk is allowed to have length not multiple of %d bytesTr   N)r   r$   r#   r   r)   r   r+   r&   r,   r(   r-   )r/   	plaintextpadding_lengthpaddedress        r   r,   zBlockAlgo.encrypt   s    N 99$"ooI0PPTXTcTccNa (($%m) )(,%"QvY~%==ll**62?C	NCll**95))((3.)-&J||##I..r   c                    | j                   t        k(  r| j                  t        |      | j                  z  z
  | j                  z  }|dkD  rc| j                  rt        d| j                        d| _        |t        d      |z  z   }| j                  j                  |      dt        |       }|S | j                  j                  |      }|S | j                  j                  |      S )a  Decrypt data with the key and the parameters set at initialization.
        
        The cipher object is stateful; decryption of a long block
        of data can be broken up in two or more calls to `decrypt()`.
        That is, the statement:
            
            >>> c.decrypt(a) + c.decrypt(b)

        is always equivalent to:

             >>> c.decrypt(a+b)

        That also means that you cannot reuse an object for encrypting
        or decrypting other data with the same key.

        This function does not perform any padding.
       
         - For `MODE_ECB`, `MODE_CBC`, and `MODE_OFB`, *ciphertext* length
           (in bytes) must be a multiple of *block_size*.

         - For `MODE_CFB`, *ciphertext* length (in bytes) must be a multiple
           of *segment_size*/8.

         - For `MODE_CTR`, *ciphertext* can be of any length.

         - For `MODE_OPENPGP`, *plaintext* must be a multiple of *block_size*,
           unless it is the last chunk of the message.

        :Parameters:
          ciphertext : byte string
            The piece of data to decrypt.
        :Return: the decrypted data (byte string, as long as *ciphertext*).
        r   r5   Tr   N)	r   r$   r#   r   r)   r   r+   r&   r.   )r/   
ciphertextr7   r8   r9   s        r   r.   zBlockAlgo.decrypt   s    D 99$"ooJ$//0QQUYUdUddNa (($%m) )(,%#ai&>>ll**623CC
OD J ll**:6J||##J//r   N)__name__
__module____qualname____doc__r3   r,   r.    r   r   r   r      s    310f9/v10r   r   )N)r?   sysversion_infoCrypto.Util.py21compatCrypto.Util.py3compatr"   MODE_CBCr*   MODE_PGPMODE_OFBMODE_CTRr$   r   r   r@   r   r   <module>rI      sz   , ; 
A! 0 0 3q 8( #  $   0  b0 b0r   