Skip to content

Commit ead33fc

Browse files
committed
PDFBOX-5955: refactor
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1924056 13f79535-47bb-0310-9956-ffa450edef68
1 parent 4894574 commit ead33fc

File tree

1 file changed

+22
-33
lines changed

1 file changed

+22
-33
lines changed

pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/StandardSecurityHandler.java

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -240,38 +240,26 @@ public void prepareForDecryption(PDEncryption encryption, COSArray documentIDArr
240240

241241
AccessPermission currentAccessPermission;
242242

243+
byte[] encryptedKey;
244+
byte[] passwordBytes;
245+
boolean isOwnerPassword;
243246
if( isOwnerPassword(password.getBytes(passwordCharset), userKey, ownerKey,
244247
dicPermissions, documentIDBytes, dicRevision,
245248
dicLength, encryptMetadata) )
246249
{
247250
currentAccessPermission = AccessPermission.getOwnerAccessPermission();
248251
setCurrentAccessPermission(currentAccessPermission);
249252

250-
byte[] computedPassword;
251253
if (dicRevision == REVISION_5 || dicRevision == REVISION_6)
252254
{
253-
computedPassword = password.getBytes(passwordCharset);
255+
passwordBytes = password.getBytes(passwordCharset);
254256
}
255257
else
256258
{
257-
computedPassword = getUserPassword234(password.getBytes(passwordCharset),
259+
passwordBytes = getUserPassword234(password.getBytes(passwordCharset),
258260
ownerKey, dicRevision, dicLength );
259261
}
260-
261-
byte[] encryptedKey = computeEncryptedKey(
262-
computedPassword,
263-
ownerKey, userKey, oe, ue,
264-
dicPermissions,
265-
documentIDBytes,
266-
dicRevision,
267-
dicLength,
268-
encryptMetadata, true);
269-
if (dicRevision == REVISION_4 && encryptedKey.length < 16)
270-
{
271-
LOG.info("PDFBOX-5955: padding RC4 key to length 16");
272-
encryptedKey = Arrays.copyOf(encryptedKey, 16);
273-
}
274-
setEncryptionKey(encryptedKey);
262+
isOwnerPassword = true;
275263
}
276264
else if( isUserPassword(password.getBytes(passwordCharset), userKey, ownerKey,
277265
dicPermissions, documentIDBytes, dicRevision,
@@ -280,26 +268,27 @@ else if( isUserPassword(password.getBytes(passwordCharset), userKey, ownerKey,
280268
currentAccessPermission = new AccessPermission(dicPermissions);
281269
currentAccessPermission.setReadOnly();
282270
setCurrentAccessPermission(currentAccessPermission);
283-
284-
byte[] encryptedKey = computeEncryptedKey(
285-
password.getBytes(passwordCharset),
286-
ownerKey, userKey, oe, ue,
287-
dicPermissions,
288-
documentIDBytes,
289-
dicRevision,
290-
dicLength,
291-
encryptMetadata, false);
292-
if (dicRevision == REVISION_4 && encryptedKey.length < 16)
293-
{
294-
LOG.info("PDFBOX-5955: padding RC4 key to length 16");
295-
encryptedKey = Arrays.copyOf(encryptedKey, 16);
296-
}
297-
setEncryptionKey(encryptedKey);
271+
passwordBytes = password.getBytes(passwordCharset);
272+
isOwnerPassword = false;
298273
}
299274
else
300275
{
301276
throw new InvalidPasswordException("Cannot decrypt PDF, the password is incorrect");
302277
}
278+
encryptedKey = computeEncryptedKey(
279+
passwordBytes,
280+
ownerKey, userKey, oe, ue,
281+
dicPermissions,
282+
documentIDBytes,
283+
dicRevision,
284+
dicLength,
285+
encryptMetadata, isOwnerPassword);
286+
if (dicRevision == REVISION_4 && encryptedKey.length < 16)
287+
{
288+
LOG.info("PDFBOX-5955: padding RC4 key to length 16");
289+
encryptedKey = Arrays.copyOf(encryptedKey, 16);
290+
}
291+
setEncryptionKey(encryptedKey);
303292

304293
if (dicRevision == REVISION_5 || dicRevision == REVISION_6)
305294
{

0 commit comments

Comments
 (0)