HEX
Server: Apache
System: Linux 185.122.168.184.host.secureserver.net 5.14.0-570.60.1.el9_6.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Nov 5 05:00:59 EST 2025 x86_64
User: barbeatleanalyti (1024)
PHP: 8.1.33
Disabled: NONE
Upload Files
File: //var/opt/nydus/ops/oscrypto/_win/__pycache__/symmetric.cpython-39.pyc
a

�,�h�x�@spddlmZmZmZmZddlmZddlmZm	Z	m
Z
mZmZm
Z
mZmZmZmZddlmZddlmZddlmZmZe�Zed	kr�dd
lmZmZmZmZmZnddl m!Z!m"Z"mZm#Z#m$Z$gd�Z%d
d�Z&dd�Z'dd�Z(dd�Z)dd�Z*dd�Z+dd�Z,dd�Z-dd�Z.dd �Z/d!d"�Z0d#d$�Z1d%d&�Z2d'd(�Z3d)d*�Z4d+d,�Z5d-d.�Z6d/d0�Z7d1d2�Z8d3d4�Z9d5S)6�)�unicode_literals�division�absolute_import�print_function�)�pretty_message)
�buffer_from_bytes�bytes_from_buffer�deref�new�null�pointer_set�struct�struct_bytes�unwrap�write_to_buffer�)�
rand_bytes)�backend)�	type_name�byte_cls�	winlegacy)�advapi32�
Advapi32Const�handle_error�open_context_handle�close_context_handle)�bcrypt�BcryptConstr�open_alg_handle�close_alg_handle)�aes_cbc_no_padding_decrypt�aes_cbc_no_padding_encrypt�aes_cbc_pkcs7_decrypt�aes_cbc_pkcs7_encrypt�des_cbc_pkcs5_decrypt�des_cbc_pkcs5_encrypt�rc2_cbc_pkcs5_decrypt�rc2_cbc_pkcs5_encrypt�rc4_decrypt�rc4_encrypt�tripledes_cbc_pkcs5_decrypt�tripledes_cbc_pkcs5_encryptcCs�t|�dvrttdt|����|s,td�}nt|�dkrJttdt|����t|�ddkrlttdt|����|td|||d�fS)	a�
    Encrypts plaintext using AES in CBC mode with a 128, 192 or 256 bit key and
    no padding. This means the ciphertext must be an exact multiple of 16 bytes
    long.

    :param key:
        The encryption key - a byte string either 16, 24 or 32 bytes long

    :param data:
        The plaintext - a byte string

    :param iv:
        The initialization vector - either a byte string 16-bytes long or None
        to generate an IV

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A tuple of two byte strings (iv, ciphertext)
    ���� �o
            key must be either 16, 24 or 32 bytes (128, 192 or 256 bits)
            long - is %s
            r.�:
            iv must be 16 bytes long - is %s
            rzJ
            data must be a multiple of 16 bytes long - is %s
            �aesF��len�
ValueErrorrr�_encrypt��key�data�iv�r<�H/opt/nydus/tmp/pip-target-wkfpz8uv/lib/python/oscrypto/_win/symmetric.pyr"-s$�
��r"cCsLt|�dvrttdt|����t|�dkr<ttdt|����td|||d�S)a[
    Decrypts AES ciphertext in CBC mode using a 128, 192 or 256 bit key and no
    padding.

    :param key:
        The encryption key - a byte string either 16, 24 or 32 bytes long

    :param data:
        The ciphertext - a byte string

    :param iv:
        The initialization vector - a byte string 16-bytes long

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the plaintext
    r-r1r.r2r3F�r5r6r�_decryptr8r<r<r=r!ds��r!cCs^t|�dvrttdt|����|s,td�}nt|�dkrJttdt|����|td|||d�fS)a�
    Encrypts plaintext using AES in CBC mode with a 128, 192 or 256 bit key and
    PKCS#7 padding.

    :param key:
        The encryption key - a byte string either 16, 24 or 32 bytes long

    :param data:
        The plaintext - a byte string

    :param iv:
        The initialization vector - either a byte string 16-bytes long or None
        to generate an IV

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A tuple of two byte strings (iv, ciphertext)
    r-r1r.r2r3Tr4r8r<r<r=r$�s�
�r$cCsLt|�dvrttdt|����t|�dkr<ttdt|����td|||d�S)aG
    Decrypts AES ciphertext in CBC mode using a 128, 192 or 256 bit key

    :param key:
        The encryption key - a byte string either 16, 24 or 32 bytes long

    :param data:
        The ciphertext - a byte string

    :param iv:
        The initialization vector - a byte string 16-bytes long

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the plaintext
    r-r1r.r2r3Tr>r8r<r<r=r#�s��r#cCs:t|�dkst|�dkr*ttdt|����td||dd�S)a�
    Encrypts plaintext using RC4 with a 40-128 bit key

    :param key:
        The encryption key - a byte string 5-16 bytes long

    :param data:
        The plaintext - a byte string

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the ciphertext
    �r.�Q
            key must be 5 to 16 bytes (40 to 128 bits) long - is %s
            �rc4N)r5r6rr7�r9r:r<r<r=r*�s�r*cCs:t|�dkst|�dkr*ttdt|����td||dd�S)a�
    Decrypts RC4 ciphertext using a 40-128 bit key

    :param key:
        The encryption key - a byte string 5-16 bytes long

    :param data:
        The ciphertext - a byte string

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the plaintext
    r@r.rArBNr>rCr<r<r=r)s�r)cCsjt|�dkst|�dkr*ttdt|����|s8td�}nt|�dkrVttdt|����|td|||d�fS)ab
    Encrypts plaintext using RC2 with a 64 bit key

    :param key:
        The encryption key - a byte string 8 bytes long

    :param data:
        The plaintext - a byte string

    :param iv:
        The 8-byte initialization vector to use - a byte string - set as None
        to generate an appropriate one

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A tuple of two byte strings (iv, ciphertext)
    r@r.rA��9
            iv must be 8 bytes long - is %s
            �rc2Tr4r8r<r<r=r(#s�
�r(cCsXt|�dkst|�dkr*ttdt|����t|�dkrHttdt|����td|||d�S)a"
    Decrypts RC2 ciphertext using a 64 bit key

    :param key:
        The encryption key - a byte string 8 bytes long

    :param data:
        The ciphertext - a byte string

    :param iv:
        The initialization vector used for encryption - a byte string

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the plaintext
    r@r.rArDrErFTr>r8r<r<r=r'Os��r'cCs~t|�dkr*t|�dkr*ttdt|����|s8td�}nt|�dkrVttdt|����d}t|�dkrjd}|t||||d�fS)	a�
    Encrypts plaintext using 3DES in either 2 or 3 key mode

    :param key:
        The encryption key - a byte string 16 or 24 bytes long (2 or 3 key mode)

    :param data:
        The plaintext - a byte string

    :param iv:
        The 8-byte initialization vector to use - a byte string - set as None
        to generate an appropriate one

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A tuple of two byte strings (iv, ciphertext)
    r.r/�W
            key must be 16 bytes (2 key) or 24 bytes (3 key) long - is %s
            rDrE�tripledes_3key�tripledes_2keyTr4�r9r:r;�cipherr<r<r=r,xs �
�r,cCslt|�dkr*t|�dkr*ttdt|����t|�dkrHttdt|����d}t|�dkr\d}t||||d�S)	aC
    Decrypts 3DES ciphertext in either 2 or 3 key mode

    :param key:
        The encryption key - a byte string 16 or 24 bytes long (2 or 3 key mode)

    :param data:
        The ciphertext - a byte string

    :param iv:
        The initialization vector used for encryption - a byte string

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the plaintext
    r.r/rGrDrErHrITr>rJr<r<r=r+�s��r+cCs^t|�dkrttdt|����|s,td�}nt|�dkrJttdt|����|td|||d�fS)a�
    Encrypts plaintext using DES with a 56 bit key

    :param key:
        The encryption key - a byte string 8 bytes long (includes error
        correction bits)

    :param data:
        The plaintext - a byte string

    :param iv:
        The 8-byte initialization vector to use - a byte string - set as None
        to generate an appropriate one

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A tuple of two byte strings (iv, ciphertext)
    rD�T
            key must be 8 bytes (56 bits + 8 parity bits) long - is %s
            rE�desTr4r8r<r<r=r&�s�
�r&cCsLt|�dkrttdt|����t|�dkr<ttdt|����td|||d�S)aK
    Decrypts DES ciphertext using a 56 bit key

    :param key:
        The encryption key - a byte string 8 bytes long (includes error
        correction bits)

    :param data:
        The ciphertext - a byte string

    :param iv:
        The initialization vector used for encryption - a byte string

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the plaintext
    rDrLrErMTr>r8r<r<r=r%s��r%cCs�d}|dkr(tjtjtjd�t|�}ntjtjtjtjtj	d�|}tj
}t|dd�}tt
d�}t|�}tj|_tj|_d|_||_tt
d	�}t|�}	||	_t|�|	_t|�|}
d}|td
dg�vr�t|�dkr�tj}tt
d
�}t
�||
t|
�t�||�}
t|
�t|�}|d
k�rFtt
dt|�d�}t
�|tj|d�}
t|
�|dk�r�t
�|tj |d�}
t|
�tt
dtj!�}t
�|tj"|d�}
t|
�tt
dtj#�}t
�|tj$|d�}
t|
�||fS)a
    Creates an HCRYPTPROV and HCRYPTKEY for symmetric encryption/decryption. The
    HCRYPTPROV must be released by close_context_handle() and the
    HCRYPTKEY must be released by advapi32.CryptDestroyKey() when done.

    :param cipher:
        A unicode string of "aes", "des", "tripledes_2key", "tripledes_3key",
        "rc2", "rc4"

    :param key:
        A byte string of the symmetric key

    :param iv:
        The initialization vector - a byte string - unused for RC4

    :return:
        A tuple of (HCRYPTPROV, HCRYPTKEY)
    Nr3r-)rMrIrHrFrBF)Zverify_onlyZ
BLOBHEADERr�PLAINTEXTKEYBLOBrFrBr@zHCRYPTKEY *�DWORD *rD)%rZCALG_AES_128ZCALG_AES_192ZCALG_AES_256r5ZCALG_DESZ
CALG_3DES_112Z	CALG_3DESZCALG_RC2ZCALG_RC4ZMS_ENH_RSA_AES_PROVrrrrrNZbTypeZCUR_BLOB_VERSIONZbVersion�reservedZaiKeyAlg�hdrZ	dwKeySizer�setZ
CRYPT_NO_SALTrZCryptImportKeyrrZCryptSetKeyParamZKP_EFFECTIVE_KEYLENZKP_IVZCRYPT_MODE_CBCZKP_MODEZ
PKCS5_PADDINGZ
KP_PADDING)rKr9r;�context_handleZalgorithm_id�providerZblob_header_pointerZblob_header�blob_struct_pointer�blob_struct�blob�flags�key_handle_pointer�res�
key_handle�bufr<r<r=�_advapi32_create_handles,s�����



�
�
���r]cCsd}tjtjtjtjtjtjd�|}z�t|�}tj}t	t
d�}t|�}tj|_
tj|_t|�|_t|�|}|dkr�tt
dt|�d�}t
�|tj|dd�}	t|	�tt
d	�}
t
�|t�||
t�d|t|�d�	}	t|	�t|
�W|r�t|�Sn|�rt|�0dS)
ao
    Creates a BCRYPT_KEY_HANDLE for symmetric encryption/decryption. The
    handle must be released by bcrypt.BCryptDestroyKey() when done.

    :param cipher:
        A unicode string of "aes", "des", "tripledes_2key", "tripledes_3key",
        "rc2", "rc4"

    :param key:
        A byte string of the symmetric key

    :return:
        A BCRYPT_KEY_HANDLE
    N)r3rMrIrHrFrBZBCRYPT_KEY_DATA_BLOB_HEADERrFrOrD�rzBCRYPT_KEY_HANDLE *)rZBCRYPT_AES_ALGORITHMZBCRYPT_DES_ALGORITHMZBCRYPT_3DES_112_ALGORITHMZBCRYPT_3DES_ALGORITHMZBCRYPT_RC2_ALGORITHMZBCRYPT_RC4_ALGORITHMrZBCRYPT_KEY_DATA_BLOBrrrZBCRYPT_KEY_DATA_BLOB_MAGICZdwMagicZBCRYPT_KEY_DATA_BLOB_VERSION1Z	dwVersionr5Z	cbKeyDatarrZBCryptSetPropertyZBCRYPT_EFFECTIVE_KEY_LENGTHrZBCryptImportKeyrr )rKr9Z
alg_handleZalg_constantZ	blob_typerUrVrWr\rZrYr<r<r=�_bcrypt_create_key_handle�sd��	

�
���r_cCs�t|t�sttdt|����t|t�s8ttdt|����|dkr\t|t�s\ttdt|����|dkr�|s�|dkr�t|�ddks�td��td	kr�t|||||�St	|||||�S)
a�
    Encrypts plaintext

    :param cipher:
        A unicode string of "aes", "des", "tripledes_2key", "tripledes_3key",
        "rc2", "rc4"

    :param key:
        The encryption key - a byte string 5-16 bytes long

    :param data:
        The plaintext - a byte string

    :param iv:
        The initialization vector - a byte string - unused for RC4

    :param padding:
        Boolean, if padding should be used - unused for RC4

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the ciphertext
    �;
            key must be a byte string, not %s
            �<
            data must be a byte string, not %s
            rB�:
            iv must be a byte string, not %s
            r3r.r�padding must be specifiedr)
�
isinstancer�	TypeErrorrrr5r6�_backend�_advapi32_encrypt�_bcrypt_encrypt�rKr9r:r;�paddingr<r<r=r7�s*
�
��r7c	Cs$d}d}�z�t|||�\}}ttdt|��}t�|t�ddt�|d�}t|�t|�}	t|	�}
t	|
|�t
|t|��t�|t�dd|
||	�}t|�t|
t|��}|dkr�|s�t|�t|�dkr�|dd�}|W|r�t�|�|r�t
|�S|�r t
|�n |�rt�|�|�rt
|�0dS)a�
    Encrypts plaintext via CryptoAPI

    :param cipher:
        A unicode string of "aes", "des", "tripledes_2key", "tripledes_3key",
        "rc2", "rc4"

    :param key:
        The encryption key - a byte string 5-16 bytes long

    :param data:
        The plaintext - a byte string

    :param iv:
        The initialization vector - a byte string - unused for RC4

    :param padding:
        Boolean, if padding should be used - unused for RC4

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the ciphertext
    NrOTrr3r.i�)r]rrr5ZCryptEncryptrrr
rrr
r	�CryptDestroyKeyr)rKr9r:r;rjrSr[�out_lenrZ�
buffer_len�buffer�outputr<r<r=rg"s\�	
�	 
�
�
rgc
Cs�d}z�t||�}|durd}nt|�}d}|dur8tj}ttd�}t�||t|�t�t�dt�d||�
}	t|	�t	|�}
t
|
�}|r�t
|�nt�}t�||t|�t�||||
||�
}	t|	�t|t	|��W|r�t�|�Sn|r�t�|�0dS)a�
    Encrypts plaintext via CNG

    :param cipher:
        A unicode string of "aes", "des", "tripledes_2key", "tripledes_3key",
        "rc2", "rc4"

    :param key:
        The encryption key - a byte string 5-16 bytes long

    :param data:
        The plaintext - a byte string

    :param iv:
        The initialization vector - a byte string - unused for RC4

    :param padding:
        Boolean, if padding should be used - unused for RC4

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the ciphertext
    NrT�ULONG *)
r_r5r�BCRYPT_BLOCK_PADDINGrrZ
BCryptEncryptrrr
rr	�BCryptDestroyKey�
rKr9r:r;rjr[Ziv_lenrXrlrZrmrnZ	iv_bufferr<r<r=rhqs^

��
��rhcCs�t|t�sttdt|����t|t�s8ttdt|����|dkr\t|t�s\ttdt|����|tddg�vrx|sxtd��tdkr�t|||||�St	|||||�S)a�
    Decrypts AES/RC4/RC2/3DES/DES ciphertext

    :param cipher:
        A unicode string of "aes", "des", "tripledes_2key", "tripledes_3key",
        "rc2", "rc4"

    :param key:
        The encryption key - a byte string 5-16 bytes long

    :param data:
        The ciphertext - a byte string

    :param iv:
        The initialization vector - a byte string - unused for RC4

    :param padding:
        Boolean, if padding should be used - unused for RC4

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the plaintext
    r`rarBrbr3rcr)
rdrrerrrRr6rf�_advapi32_decrypt�_bcrypt_decryptrir<r<r=r?�s(
�
��r?c
	Cs�d}d}z�t|||�\}}|dkr>|s>t|�ddkr>td��t|�}ttdt|��}t�|t�|dkrp|spdndd||�}	t|	�t	|t
|��W|r�t�|�|r�t|�S|r�t|�n|r�t�|�|r�t|�0dS)	a�
    Decrypts AES/RC4/RC2/3DES/DES ciphertext via CryptoAPI

    :param cipher:
        A unicode string of "aes", "des", "tripledes_2key", "tripledes_3key",
        "rc2", "rc4"

    :param key:
        The encryption key - a byte string 5-16 bytes long

    :param data:
        The ciphertext - a byte string

    :param iv:
        The initialization vector - a byte string - unused for RC4

    :param padding:
        Boolean, if padding should be used - unused for RC4

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the plaintext
    Nr3r.rz9Invalid data - ciphertext length must be a multiple of 16rOFT)
r]r5r6rrrZCryptDecryptrrr	r
rkr)
rKr9r:r;rjrSr[rnrlrZr<r<r=rts>�

�
�
rtc
Cs�d}z�t||�}|durd}nt|�}d}|dur8tj}ttd�}t�||t|�t�t�dt�d||�
}	t|	�t	|�}
t
|
�}|r�t
|�nt�}t�||t|�t�||||
||�
}	t|	�t|t	|��W|r�t�|�Sn|r�t�|�0dS)a�
    Decrypts AES/RC4/RC2/3DES/DES ciphertext via CNG

    :param cipher:
        A unicode string of "aes", "des", "tripledes_2key", "tripledes_3key",
        "rc2", "rc4"

    :param key:
        The encryption key - a byte string 5-16 bytes long

    :param data:
        The ciphertext - a byte string

    :param iv:
        The initialization vector - a byte string - unused for RC4

    :param padding:
        Boolean, if padding should be used - unused for RC4

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the plaintext
    NrTrp)
r_r5rrqrrZ
BCryptDecryptrrr
rr	rrrsr<r<r=ru>s^

��
��ruN):�
__future__rrrr�_errorsrZ_ffirr	r
rrr
rrrr�utilr�r�_typesrrrfZ	_advapi32rrrrrZ_cngrrrr �__all__r"r!r$r#r*r)r(r'r,r+r&r%r]r_r7rgrhr?rtrur<r<r<r=�<module>s<07+.*,)0--*oG@OS==