@@ -258,15 +258,20 @@ public void prepareForDecryption(PDEncryption encryption, COSArray documentIDArr
258258 ownerKey , dicRevision , dicLength );
259259 }
260260
261- setEncryptionKey (
262- computeEncryptedKey (
261+ byte [] encryptedKey = computeEncryptedKey (
263262 computedPassword ,
264263 ownerKey , userKey , oe , ue ,
265264 dicPermissions ,
266265 documentIDBytes ,
267266 dicRevision ,
268267 dicLength ,
269- encryptMetadata , true ));
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 );
270275 }
271276 else if ( isUserPassword (password .getBytes (passwordCharset ), userKey , ownerKey ,
272277 dicPermissions , documentIDBytes , dicRevision ,
@@ -276,15 +281,20 @@ else if( isUserPassword(password.getBytes(passwordCharset), userKey, ownerKey,
276281 currentAccessPermission .setReadOnly ();
277282 setCurrentAccessPermission (currentAccessPermission );
278283
279- setEncryptionKey (
280- computeEncryptedKey (
284+ byte [] encryptedKey = computeEncryptedKey (
281285 password .getBytes (passwordCharset ),
282286 ownerKey , userKey , oe , ue ,
283287 dicPermissions ,
284288 documentIDBytes ,
285289 dicRevision ,
286290 dicLength ,
287- encryptMetadata , false ));
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 );
288298 }
289299 else
290300 {
0 commit comments