o
    e`)  ã                   @   sl  d Z ddlZddlmZ ddlmZmZ ddlmZm	Z	m
Z
 ddlmZ zddlmZ W n ey;   ddlmZ Y nw d	Z	
d$deded dedededededefdd„Zdededed dedef
dd„Zdeded dedefdd„Zdededededeeef f
dd„Zdeded dedededededeeef fdd„Zd eeef d!edefd"d#„ZdS )%z,Anything related to encryption / decryption.é    N)Úmd5)ÚTupleÚUnioné   )Úb_Úord_Ústr_)ÚByteStringObject)ÚLiterals    (¿N^NuŠAd NVÿú.. ¶Ðh>€/©þdSizTÚpasswordÚrev)é   é   é   ÚkeylenÚowner_entryÚp_entryÚ	id1_entryÚmetadata_encryptÚreturnc                 C   s¦   t t| ƒttƒ dd… ƒ}t|ƒ}| |j¡ t d|¡}	| |	¡ | |j¡ |dkr4|s4| d¡ | ¡ }
|dkrMt	dƒD ]}t|
d|… ƒ ¡ }
q@|
d|… S )z€
    Implementation of algorithm 3.2 of the PDF standard security handler.

    See section 3.5.2 of the PDF 1.6 reference.
    Né    z<ir   s   ÿÿÿÿé2   )
r   r   Ú_encryption_paddingr   ÚupdateÚoriginal_bytesÚstructÚpackÚdigestÚrange)r   r   r   r   r   r   r   Úpassword_bytesÚmZp_entry_bytesÚmd5_hashÚ_© r#   ú@D:\Projects\ConvertPro\env\Lib\site-packages\PyPDF2/_security.pyÚ_alg326   s   

r%   Ú	owner_pwdÚuser_pwdc           
      C   sv   t | ||ƒ}t|ttƒ dd… ƒ}t||ƒ}|dkr9tddƒD ]}d}|D ]}	|tt|	ƒ|A ƒ7 }q&t||ƒ}q |S )z{
    Implementation of algorithm 3.3 of the PDF standard security handler,
    section 3.5.2 of the PDF 1.6 reference.
    Nr   r   r   é   Ú )Ú_alg33_1r   r   r   ÚRC4_encryptr   Úchrr   )
r&   r'   r   r   ÚkeyZuser_pwd_bytesÚvalÚiÚnew_keyZkey_charr#   r#   r$   Ú_alg33l   s   
r1   c                 C   sZ   t | ttƒ dd… ƒ}t|ƒ}| ¡ }|dkr%tdƒD ]}t|ƒ ¡ }q|d|… }|S )zSteps 1-4 of algorithm 3.3Nr   r   r   )r   r   r   r   r   r   )r   r   r   r   r    r!   r"   r-   r#   r#   r$   r*   Š   s   r*   c                 C   s,   d}d}t | |||||ƒ}t|tƒ}||fS )ú€
    Implementation of algorithm 3.4 of the PDF standard security handler.

    See section 3.5.2 of the PDF 1.6 reference.
    r   é   )r%   r+   r   )r   r   r   r   r   r   r-   ÚUr#   r#   r$   Ú_alg34¡   s
   
r5   c                 C   sŒ   t | |||||ƒ}tƒ }| t¡ | |j¡ | ¡ }	t||	ƒ}
tddƒD ]}d}|D ]}|tt	t
|ƒ|A ƒƒ7 }q+t||
ƒ}
q%|
d |fS )r2   r   r(   ó    s                   )r%   r   r   r   r   r   r+   r   r   r,   r   )r   r   r   r   r   r   r   r-   r    r!   r.   r/   r0   Úkr#   r#   r$   Ú_alg35º   s   

r8   r-   Ú	plaintextc                 C   sä   t tdƒƒ}d}tdƒD ]!}|||  t| |t| ƒ  ƒ d }|| || ||< ||< qd\}}g }|D ]6}|d d }|||  d }|| || ||< ||< ||| ||  d  }| ttt|ƒ|A ƒƒ¡ q6d |¡S )Né   r   )r   r   r   r6   )Úlistr   r   ÚlenÚappendr   r,   Újoin)r-   r9   ÚSÚjr/   ÚretvalZplaintext_charÚtr#   r#   r$   r+   ì   s   $
r+   )T)Ú__doc__r   Úhashlibr   Útypingr   r   Ú_utilsr   r   r   Zgenericr	   r
   ÚImportErrorZtyping_extensionsr   ÚstrÚintÚboolÚbytesr%   r1   r*   r5   r8   r+   r#   r#   r#   r$   Ú<module>   s|   ýÿùÿþýüûúù
ø"6ÿþýü

ûÿþýüûúù

ø"2