From 28c37dbcf87665a4ccec58bef9ef8ff0697022dd Mon Sep 17 00:00:00 2001 From: Martin Kretzschmar Date: Mon, 31 Mar 2003 16:45:09 +0000 Subject: Import of Xpdf 2.00 for merge --- (limited to 'pdf/xpdf/Decrypt.cc') diff --git a/pdf/xpdf/Decrypt.cc b/pdf/xpdf/Decrypt.cc index 8de4091..bb3e3f1 100644 --- a/pdf/xpdf/Decrypt.cc +++ b/pdf/xpdf/Decrypt.cc @@ -6,11 +6,12 @@ // //======================================================================== -#ifdef __GNUC__ +#include + +#ifdef USE_GCC_PRAGMAS #pragma implementation #endif -#include #include "gmem.h" #include "Decrypt.h" @@ -65,11 +66,12 @@ GBool Decrypt::makeFileKey(int encVersion, int encRevision, int keyLength, int permissions, GString *fileID, GString *ownerPassword, GString *userPassword, Guchar *fileKey, GBool *ownerPasswordOk) { - Guchar test[32]; + Guchar test[32], test2[32]; GString *userPassword2; Guchar fState[256]; + Guchar tmpKey[16]; Guchar fx, fy; - int len, i; + int len, i, j; // try using the supplied owner password to generate the user password if (ownerPassword) { @@ -89,12 +91,26 @@ GBool Decrypt::makeFileKey(int encVersion, int encRevision, int keyLength, md5(test, 16, test); } } - rc4InitKey(test, keyLength, fState); - fx = fy = 0; - for (i = 0; i < 32; ++i) { - test[i] = rc4DecryptByte(fState, &fx, &fy, ownerKey->getChar(i)); + if (encRevision == 2) { + rc4InitKey(test, keyLength, fState); + fx = fy = 0; + for (i = 0; i < 32; ++i) { + test2[i] = rc4DecryptByte(fState, &fx, &fy, ownerKey->getChar(i)); + } + } else { + memcpy(test2, ownerKey->getCString(), 32); + for (i = 19; i >= 0; --i) { + for (j = 0; j < keyLength; ++j) { + tmpKey[j] = test[j] ^ i; + } + rc4InitKey(tmpKey, keyLength, fState); + fx = fy = 0; + for (j = 0; j < 32; ++j) { + test2[j] = rc4DecryptByte(fState, &fx, &fy, test2[j]); + } + } } - userPassword2 = new GString((char *)test, 32); + userPassword2 = new GString((char *)test2, 32); if (makeFileKey2(encVersion, encRevision, keyLength, ownerKey, userKey, permissions, fileID, userPassword2, fileKey)) { *ownerPasswordOk = gTrue; @@ -143,7 +159,7 @@ GBool Decrypt::makeFileKey2(int encVersion, int encRevision, int keyLength, md5(buf, 68 + fileID->getLength(), fileKey); if (encRevision == 3) { for (i = 0; i < 50; ++i) { - md5(fileKey, 16, fileKey); + md5(fileKey, keyLength, fileKey); } } -- cgit v0.9.1