Warning: session_start(): Session cannot be started after headers have already been sent in /home/tvrreohg/public_html/manga.php on line 13
3 ©«jC˜ã)@sšddlZddlZddlZddlmZddlmZmZmZm Z m Z m Z ddl m Z ddlmZddlmZmZmZmZmZmZmZGdd „d ƒZGd d „d eƒZy°dd lmZmZdd lmZGdd„deƒZGdd„deƒZ e!e!e!dœdd„Z"e!e!e!dœdd„Z#e!e!e!dœdd„Z$e!e!e!dœdd„Z%e!e!e!e!dœdd„Z&e!e!e!e!dœdd„Z'Wn¦e(k �rôGd d„deƒZGd!d„deƒZ e!e!e!dœd"d„Z"e!e!e!dœd#d„Z#e!e!e!dœd$d„Z$e!e!e!dœd%d„Z%e!e!e!e!dœd&d„Z&e!e!e!e!dœd'd„Z'YnXGd(d)„d)ƒZ)e!d*d+d,d-d,d.d/d0d1dd,d2d3d4dd5d6d6dd7d8d9d:d;dd?d1d@dAdBg ƒZ*e!e!dCœdDdE„Z+GdFdG„dGƒZ,GdHdI„dIƒZ-GdJdK„dKeƒZ.GdLdM„dMƒZ/dS)NéN)ÚIntEnum)ÚAnyÚDictÚOptionalÚTupleÚUnionÚcasté)Úlogger_warning)ÚDependencyError)Ú ArrayObjectÚByteStringObjectÚDictionaryObjectÚ PdfObjectÚ StreamObjectÚTextStringObjectÚcreate_string_objectc@s,eZdZeedœdd„Zeedœdd„ZdS)Ú CryptBase)ÚdataÚreturncCs|S)N©)Úselfrrrú4/tmp/pip-build-vnmlutbd/PyPDF2/PyPDF2/_encryption.pyÚencrypt0szCryptBase.encryptcCs|S)Nr)rrrrrÚdecrypt3szCryptBase.decryptN)Ú__name__Ú __module__Ú __qualname__Úbytesrrrrrrr/src@s eZdZdS)Ú CryptIdentityN)rrrrrrrr7sr)ÚAESÚARC4)Úpadc@s<eZdZeddœdd„Zeedœdd„Zeedœdd „ZdS) ÚCryptRC4N)ÚkeyrcCs ||_dS)N)r$)rr$rrrÚ__init__@szCryptRC4.__init__)rrcCstj|jƒj|ƒS)N)r!Ú ARC4Cipherr$r)rrrrrrCszCryptRC4.encryptcCstj|jƒj|ƒS)N)r!r&r$r)rrrrrrFszCryptRC4.decrypt)rrrrr%rrrrrrr#?sr#c@s<eZdZeddœdd„Zeedœdd„Zeedœdd „ZdS) ÚCryptAESN)r$rcCs ||_dS)N)r$)rr$rrrr%JszCryptAES.__init__)rrcslttdd„tdƒDƒƒƒ}dt|ƒd‰|tt‡fdd„tˆƒDƒƒƒ7}tj|jtj|ƒ}||j|ƒS)Ncss|]}tjddƒVqdS)réÿN)ÚrandomÚrandint)Ú.0Ú_rrrú Nsz#CryptAES.encrypt..éc3s|] }ˆVqdS)Nr)r+r,)Úprrr-Ps) rÚ bytearrayÚrangeÚlenr Únewr$ÚMODE_CBCr)rrÚivÚaesr)r/rrMs "zCryptAES.encryptcCsp|dd…}|dd…}tj|jtj|ƒ}t|ƒdr@t|dƒ}|j|ƒ}t|ƒdkrZ|S|d|d …SdS)Nr.rr éÿÿÿÿ)r r3r$r4r2r"r)rrr5r6ÚdrrrrTs      zCryptAES.decrypt)rrrrr%rrrrrrr'Isr')r$rrcCstj|ƒj|ƒS)N)r!r&r)r$rrrrÚ RC4_encrypt`sr9cCstj|ƒj|ƒS)N)r!r&r)r$rrrrÚ RC4_decryptcsr:cCstj|tjƒj|ƒS)N)r r3ÚMODE_ECBr)r$rrrrÚAES_ECB_encryptfsr<cCstj|tjƒj|ƒS)N)r r3r;r)r$rrrrÚAES_ECB_decryptisr=)r$r5rrcCstj|tj|ƒj|ƒS)N)r r3r4r)r$r5rrrrÚAES_CBC_encryptlsr>cCstj|tj|ƒj|ƒS)N)r r3r4r)r$r5rrrrÚAES_CBC_decryptosr?c@s<eZdZeddœdd„Zeedœdd„Zeedœdd „ZdS) r#N)r$rcCslttdƒƒ|_d}xTtdƒD]H}||j|||t|ƒd}|j||j||j|<|j|<qWdS)Nér)Úlistr1ÚSr2)rr$ÚjÚirrrr%us "zCryptRC4.__init__)rrcCs¬t|jƒ}tdd„tt|ƒƒDƒƒ}d\}}xrtt|ƒƒD]b}|dd}|||d}||||||<||<|||||d}|||A||<q:Wtt|ƒƒS)Ncss|] }dVqdS)rNr)r+r,rrrr-~sz#CryptRC4.encrypt..rr r@)rr)rArBr1r2rr0)rrrBÚoutrDrCÚkÚxrrrr|s  zCryptRC4.encryptcCs |j|ƒS)N)r)rrrrrrˆszCryptRC4.decrypt)rrrrr%rrrrrrr#ts c@s<eZdZeddœdd„Zeedœdd„Zeedœdd „ZdS) r'N)r$rcCsdS)Nr)rr$rrrr%ŒszCryptAES.__init__)rrcCs tdƒ‚dS)Nz*PyCryptodome is required for AES algorithm)r )rrrrrr�szCryptAES.encryptcCs tdƒ‚dS)Nz*PyCryptodome is required for AES algorithm)r )rrrrrr’szCryptAES.decrypt)rrrrr%rrrrrrr'‹scCst|ƒj|ƒS)N)r#r)r$rrrrr9•scCst|ƒj|ƒS)N)r#r)r$rrrrr:˜scCs tdƒ‚dS)Nz*PyCryptodome is required for AES algorithm)r )r$rrrrr<›scCs tdƒ‚dS)Nz*PyCryptodome is required for AES algorithm)r )r$rrrrr=žscCs tdƒ‚dS)Nz*PyCryptodome is required for AES algorithm)r )r$r5rrrrr>¡scCs tdƒ‚dS)Nz*PyCryptodome is required for AES algorithm)r )r$r5rrrrr?¤sc@s@eZdZeeeddœdd„Zeedœdd„Zeedœdd „ZdS) Ú CryptFilterN)ÚstmCryptÚstrCryptÚefCryptrcCs||_||_||_dS)N)rIrJrK)rrIrJrKrrrr%©szCryptFilter.__init__)ÚobjrcCstS)N)ÚNotImplemented)rrLrrrÚencrypt_object°szCryptFilter.encrypt_objectcCs¬t|ttfƒr&|jj|jƒ}t|ƒ}n‚t|tƒrB|jj|j ƒ|_ nft|t ƒrvxZt |j ƒƒD]\}}|j |ƒ||<qZWn2t|tƒr¨x&tt|ƒƒD]}|j ||ƒ||<qŽW|S)N)Ú isinstancer rrJrÚoriginal_bytesrrrIÚ_datarrAÚitemsÚdecrypt_objectr r1r2)rrLrZdictkeyÚvaluerDrrrrS´s    zCryptFilter.decrypt_object)rrrrr%rrNrSrrrrrH¨srHé(é¿éNé^éuéŠéAédéVr(éúéé.é¶éÐéhé>é€é/é é©éþéSéiéz)rrcCs|tdd…S)Né )Ú_PADDING)rrrrÚ_paddingésroc @s¸eZdZeeeeeeeeedœdd„ƒZeeeeedœdd„ƒZeeeeedœdd „ƒZ eeeeed œd d „ƒZ eeeeeeeeeed œ dd„ƒZ eeeeeeeeeedœ dd„ƒZ dS)ÚAlgV4)ÚpasswordÚrevÚkey_sizeÚo_entryÚPÚ id1_entryÚmetadata_encryptedrc Csžt|ƒ}tj|ƒ}|j|ƒ|jtjd|ƒƒ|j|ƒ|dkrR|dkrR|jdƒ|jƒ} |d} |dkr’x&tdƒD]} tj| d| …ƒjƒ} qtW| d| …S) uÍ Algorithm 2: Computing an encryption key. a) Pad or truncate the password string to exactly 32 bytes. If the password string is more than 32 bytes long, use only its first 32 bytes; if it is less than 32 bytes long, pad it by appending the required number of additional bytes from the beginning of the following padding string: < 28 BF 4E 5E 4E 75 8A 41 64 00 4E 56 FF FA 01 08 2E 2E 00 B6 D0 68 3E 80 2F 0C A9 FE 64 53 69 7A > That is, if the password string is n bytes long, append the first 32 - n bytes of the padding string to the end of the password string. If the password string is empty (zero-length), meaning there is no user password, substitute the entire padding string in its place. b) Initialize the MD5 hash function and pass the result of step (a) as input to this function. c) Pass the value of the encryption dictionary’s O entry to the MD5 hash function. ("Algorithm 3: Computing the encryption dictionary’s O (owner password) value" shows how the O value is computed.) d) Convert the integer value of the P entry to a 32-bit unsigned binary number and pass these bytes to the MD5 hash function, low-order byte first. e) Pass the first element of the file’s file identifier array (the value of the ID entry in the document’s trailer dictionary; see Table 15) to the MD5 hash function. f) (Security handlers of revision 4 or greater) If document metadata is not being encrypted, pass 4 bytes with the value 0xFFFFFFFF to the MD5 hash function. g) Finish the hash. h) (Security handlers of revision 3 or greater) Do the following 50 times: Take the output from the previous MD5 hash and pass the first n bytes of the output as input into a new MD5 hash, where n is the number of bytes of the encryption key as defined by the value of the encryption dictionary’s Length entry. i) Set the encryption key to the first n bytes of the output from the final MD5 hash, where n shall always be 5 for security handlers of revision 2 but, for security handlers of revision 3 or greater, shall depend on the value of the encryption dictionary’s Length entry. zxtdƒD]}tj|ƒjƒ}q(W|d|d…}|S)uJ Algorithm 3: Computing the encryption dictionary’s O (owner password) value. a) Pad or truncate the owner password string as described in step (a) of "Algorithm 2: Computing an encryption key". If there is no owner password, use the user password instead. b) Initialize the MD5 hash function and pass the result of step (a) as input to this function. c) (Security handlers of revision 3 or greater) Do the following 50 times: Take the output from the previous MD5 hash and pass it as input into a new MD5 hash. d) Create an RC4 encryption key using the first n bytes of the output from the final MD5 hash, where n shall always be 5 for security handlers of revision 2 but, for security handlers of revision 3 or greater, shall depend on the value of the encryption dictionary’s Length entry. e) Pad or truncate the user password string as described in step (a) of "Algorithm 2: Computing an encryption key". f) Encrypt the result of step (e), using an RC4 encryption function with the encryption key obtained in step (d). g) (Security handlers of revision 3 or greater) Do the following 19 times: Take the output from the previous invocation of the RC4 function and pass it as input to a new invocation of the function; use an encryption key generated by taking each byte of the encryption key obtained in step (d) and performing an XOR (exclusive or) operation between that byte and the single-byte value of the iteration counter (from 1 to 19). h) Store the output from the final invocation of the RC4 function as the value of the O entry in the encryption dictionary. ryrzNr_)ror{r|r€r1)r…rrrsr�Z o_hash_digestr,Úrc4_keyrrrÚcompute_O_value_key2s!zAlgV4.compute_O_value_key)r†Ú user_passwordrrrcsVt|ƒ}t||ƒ}|dkrRx6tddƒD](‰tt‡fdd„|Dƒƒƒ}t||ƒ}q&W|S)z See :func:`compute_O_value_key`.ryr éc3s|]}|ˆAVqdS)Nr)r+rG)rDrrr-dsz(AlgV4.compute_O_value..)ror9r1rr0)r†rˆrrr�Úrc4_encr$r)rDrÚcompute_O_value]s zAlgV4.compute_O_value)r$rrrvrcsx|dkrt|tƒ}|Stjtƒ}|j|ƒt||jƒƒ}x6tddƒD](‰tt‡fdd„|Dƒƒƒ}t||ƒ}qDWt |ƒS)u] Algorithm 4: Computing the encryption dictionary’s U (user password) value. (Security handlers of revision 2) a) Create an encryption key based on the user password string, as described in "Algorithm 2: Computing an encryption key". b) Encrypt the 32-byte padding string shown in step (a) of "Algorithm 2: Computing an encryption key", using an RC4 encryption function with the encryption key from the preceding step. c) Store the result of step (b) as the value of the U entry in the encryption dictionary. ér r‰c3s|]}|ˆAVqdS)Nr)r+rG)rDrrr-˜sz(AlgV4.compute_U_value..) r9rnr{r|r}r€r1rr0ro)r$rrrvrTr‚rŠr†r)rDrÚcompute_U_valuehs   zAlgV4.compute_U_value) rˆrrrsrtÚu_entryrurvrwrc CsTtj|||||||ƒ}tj|||ƒ} |dkrD| dd…} |dd…}| |krPd}|S)u Algorithm 6: Authenticating the user password. a) Perform all but the last step of "Algorithm 4: Computing the encryption dictionary’s U (user password) value (Security handlers of revision 2)" or "Algorithm 5: Computing the encryption dictionary’s U (user password) value (Security handlers of revision 3 or greater)" using the supplied password string. b) If the result of step (a) is equal to the value of the encryption dictionary’s U entry (comparing on the first 16 bytes in the case of security handlers of revision 3 or greater), the password supplied is the correct user password. The key obtained in step (a) (that is, in the first step of "Algorithm 4: Computing the encryption dictionary’s U (user password) value (Security handlers of revision 2)" or "Algorithm 5: Computing the encryption dictionary’s U (user password) value (Security handlers of revision 3 or greater)") shall be used to decrypt the document. ryNr.ó)rpr„r�) rˆrrrsrtrŽrurvrwr$Úu_valuerrrÚverify_user_passwordœs  zAlgV4.verify_user_password) r…rrrsrtrŽrurvrwrc sxtj|||ƒ}|dkr"t||ƒ} n>|} x8tdddƒD](‰tt‡fdd„|Dƒƒƒ} t| | ƒ} q4Wtj| |||||||ƒS)u Algorithm 7: Authenticating the owner password. a) Compute an encryption key from the supplied password string, as described in steps (a) to (d) of "Algorithm 3: Computing the encryption dictionary’s O (owner password) value". b) (Security handlers of revision 2 only) Decrypt the value of the encryption dictionary’s O entry, using an RC4 encryption function with the encryption key computed in step (a). (Security handlers of revision 3 or greater) Do the following 20 times: Decrypt the value of the encryption dictionary’s O entry (first iteration) or the output from the previous iteration (all subsequent iterations), using an RC4 encryption function with a different encryption key at each iteration. The key shall be generated by taking the original key (obtained in step (a)) and performing an XOR (exclusive or) operation between each byte of the key and the single-byte value of the iteration counter (from 19 to 0). c) The result of step (b) purports to be the user password. Authenticate this user password using "Algorithm 6: Authenticating the user password". If it is correct, the password supplied is the correct owner password. rŒér c3s|]}|ˆAVqdS)Nr)r+rG)rDrrr-àsz.AlgV4.verify_owner_password..r7r7)rpr‡r:r1rr0r‘) r…rrrsrtrŽrurvrwr†rˆr$r)rDrÚverify_owner_password¿s  zAlgV4.verify_owner_passwordN) rrrÚ staticmethodrÚintÚboolr„r‡r‹r�r‘r“rrrrrpísF;* 3rpc @sðeZdZeeeeeeedœdd„ƒZeeeeeedœdd„ƒZeeeeeedœdd „ƒZeeeee e d œd d „ƒZ eeeeee e e e fd œdd„ƒZ eeeeeefdœdd„ƒZeeeeeeefdœdd„ƒZeeee edœdd„ƒZdS)ÚAlgV5)ÚRrqÚo_valueÚoe_valuer�rcCs‚|dd…}tj|||dd…|dd…ƒ|dd…kr.r.)r—Úcalculate_hashrr1r?)r˜rqr™ršr�r5Útmp_keyr$rrrr“ïs!   zAlgV5.verify_owner_password)r˜rqr�Úue_valuercCsn|dd…}tj|||dd…dƒ|dd…kr4dStdd„tdƒDƒƒ}tj|||dd …dƒ}t|||ƒS) z"See :func:`verify_owner_password`.Nr›rmrUr�css|] }dVqdS)rNr)r+r,rrrr-#sz-AlgV5.verify_user_password..r.rœ)r—r�rr1r?)r˜rqr�rŸr5ržrrrr‘s  $zAlgV5.verify_user_password)r˜rqÚsaltÚudatarc Cs¶tj|||ƒjƒ}|dkr"|Sd}x‚|d7}|||}t|dd…|dd…|dƒ}tjtjtjft|dd…ƒd}||ƒjƒ}|dkr(|d|dkr(Pq(W|dd…S) Nérr r.rmé@ryr7)r{Úsha256r€r>Úsha384Úsha512Úsum) r˜rqr r¡ÚKÚcountZK1ÚEZhash_fnrrrr�'s    zAlgV5.calculate_hash)r$Úpermsr/rwrcCs>|rdnd}tjd|ƒd|d}t||ƒ}||dd…kS)zBSee :func:`verify_owner_password` and :func:`compute_Perms_value`.óTóFz.r.Nr_css|] }dVqdS)rNr)r+r,rrrr-ls)rr1r{r¤r€r>) rqr$Ú random_bytesÚval_saltÚkey_saltr�ržr5rŸrrrr�Ys    zAlgV5.compute_U_value)rqr$r�rc CsŒtdd„tdƒDƒƒ}|dd…}|dd…}tj|||ƒjƒ||}tj|||ƒjƒ}tdd„tdƒDƒƒ}t|||ƒ} || fS)u+ Algorithm 3.9 Computing the encryption dictionary’s O (owner password) and OE (owner encryption key) values. 1. Generate 16 random bytes of data using a strong random number generator. The first 8 bytes are the Owner Validation Salt. The second 8 bytes are the Owner Key Salt. Compute the 32-byte SHA-256 hash of the password concatenated with the Owner Validation Salt and then concatenated with the 48-byte U string as generated in Algorithm 3.8. The 48-byte string consisting of the 32-byte hash followed by the Owner Validation Salt followed by the Owner Key Salt is stored as the O key. 2. Compute the 32-byte SHA-256 hash of the password concatenated with the Owner Key Salt and then concatenated with the 48-byte U string as generated in Algorithm 3.8. Using this hash as the key, encrypt the file encryption key using AES-256 in CBC mode with no padding and an initialization vector of zero. The resulting 32-byte string is stored as the OE key. css|]}tjddƒVqdS)rr@N)r)r´)r+r,rrrr-�sz(AlgV5.compute_O_value..r.Nr_css|] }dVqdS)rNr)r+r,rrrr-‰s)rr1r{r¤r€r>) rqr$r�rµr¶r·r™ržr5ršrrrr‹ps   zAlgV5.compute_O_value)r$r/rwrcCsL|rdnd}tdd„tdƒDƒƒ}tjd|ƒd|d|}t||ƒ}|S) u† Algorithm 3.10 Computing the encryption dictionary’s Perms (permissions) value 1. Extend the permissions (contents of the P integer) to 64 bits by setting the upper 32 bits to all 1’s. (This allows for future extension without changing the format.) 2. Record the 8 bytes of permission in the bytes 0-7 of the block, low order byte first. 3. Set byte 8 to the ASCII value ' T ' or ' F ' according to the EncryptMetadata Boolean. 4. Set bytes 9-11 to the ASCII characters ' a ', ' d ', ' b '. 5. Set bytes 12-15 to 4 bytes of random data, which will be ignored. 6. Encrypt the 16-byte block using AES-256 in ECB mode with an initialization vector of zero, using the file encryption key as the key. The result (16 bytes) is stored as the Perms string, and checked for validity when the file is opened. r¬r­css|]}tjddƒVqdS)rr@N)r)r´)r+r,rrrr-�sz,AlgV5.compute_Perms_value..rxztk r6|jdƒ}Yq>Xn|}|jdkrR|j|ƒn|j|ƒ\}}|tjkrv||_ ||_ |S)Nzlatin-1zutf-8rx) rOÚstrÚencodeÚ Exceptionr½Ú verify_v4Ú verify_v5r¸r¹rÅrÆ)rrqÚpwdr$ÚrcrrrÚverifys " zEncryption.verifyc CsÐtt|jdƒ}tt|jdƒ}|dd}|jjdƒ}|rB|jnd}tt|jdjƒƒj}tt|jdjƒƒj}tj |||j ||||j |ƒ}|rœ|t j fStj|||j ||||j |ƒ}|rÆ|t jfSdt jfS) Nz/Rz/Plz/EncryptMetadataTz/Oz/Ur�)rr•r¿rÄrTr Ú get_objectrPrpr“rsrvr¸r»r‘rºr¹) rrqr˜ruÚemrwrtrŽr$rrrrÕs<    zEncryption.verify_v4c Csþtt|jdjƒƒj}tt|jdjƒƒj}tt|jdjƒƒj}tt|jdjƒƒj}tj|j||||ƒ}tj }|sŽtj |j|||ƒ}tj }|sœdtj fStt|jdjƒƒj}tt |jdƒ} | dd} |jjd d ƒ} tj||| | ƒsötd tƒ||fS) Nz/Oz/Uz/OEz/UEr�z/Permsz/Plz/EncryptMetadataTzignore '/Perms' verify failed)rr r¿rÚrPr—r“r¾r¸r»r‘rºr¹r•rÄr±r r) rrqrtrŽZoe_entryZue_entryr$rØr«rurwrrrrÖ<s&   zEncryption.verify_v5)Úencryption_entryrÀrc Cs6|jdƒ}|dkrtdƒ‚d|kr*tdƒ‚d}d}d}|jddƒ}|dkrZtd|›d�ƒ‚|d k�r|d}|jddƒ}|jddƒ}|jd|ƒ}|dkr¤||d}|dkr¸||d}|dkrÌ||d}d}||kràtdƒ‚||k�rútd|›d�ƒ‚||k�rtd|›d�ƒ‚tt|dƒ} t|| |||||ƒS)Nz/Filterz /Standardz1only Standard PDF encryption handler is availablez /SubFilterz/SubFilter NOT supportedú/V2z/Vrr rŒryrxrÊz Encryption V=z NOT supportedz/CFz/StmFú /Identityz/StrFz/EFFz/CFMú/AESV2ú/AESV3z!StmF Method {StmF} NOT supported!z StrF Method z NOT supported!z EFF Method z/R)r rŒryrxrÊ)rÞrÝrßrà)rÄÚNotImplementedErrorrr•r¼) rÜrÀÚfilterrÁrÂrÃÚVÚfiltersZallowed_methodsr˜rrrÚreadWs@           zEncryption.read)rrrr•rrrÒr%r–rÇrrSr”rrÍrr¸rÙrrÕrÖrårrrrr¼©s$5 %r¼)0r{r)r~ÚenumrÚtypingrrrrrrÚ_utilsr Úerrorsr Zgenericr r rrrrrrrZ Crypto.Cipherr r!ZCrypto.Util.Paddingr"r#r'rr9r:r<r=r>r?Ú ImportErrorrHrnrorpr—r¸r¼rrrrÚsŽ    $    6