aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache
diff options
context:
space:
mode:
authorNick Burch <nick@apache.org>2010-06-03 16:13:38 +0000
committerNick Burch <nick@apache.org>2010-06-03 16:13:38 +0000
commitfa5055f2edc40c17d0e327a9ca8f336933a13f88 (patch)
tree59c8b4acb5b020e3f28f1d820a655c055b8d1b3c /src/java/org/apache
parent93f034976a9bf290118a6cc1a75b2d25c2f0c1b7 (diff)
downloadpoi-fa5055f2edc40c17d0e327a9ca8f336933a13f88.tar.gz
poi-fa5055f2edc40c17d0e327a9ca8f336933a13f88.zip
Fix bug #49378 - correct 1.6ism
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@951048 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache')
-rw-r--r--src/java/org/apache/poi/poifs/crypt/Decryptor.java38
1 files changed, 27 insertions, 11 deletions
diff --git a/src/java/org/apache/poi/poifs/crypt/Decryptor.java b/src/java/org/apache/poi/poifs/crypt/Decryptor.java
index 7b36dc4e01..9e6c430803 100644
--- a/src/java/org/apache/poi/poifs/crypt/Decryptor.java
+++ b/src/java/org/apache/poi/poifs/crypt/Decryptor.java
@@ -16,23 +16,23 @@
==================================================================== */
package org.apache.poi.poifs.crypt;
-import org.apache.poi.poifs.filesystem.DocumentInputStream;
-import org.apache.poi.poifs.filesystem.POIFSFileSystem;
-import org.apache.poi.util.LittleEndian;
-
-import javax.crypto.Cipher;
-import javax.crypto.CipherInputStream;
-import javax.crypto.SecretKey;
-import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
-import java.nio.charset.Charset;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
+import javax.crypto.Cipher;
+import javax.crypto.CipherInputStream;
+import javax.crypto.SecretKey;
+import javax.crypto.spec.SecretKeySpec;
+
+import org.apache.poi.poifs.filesystem.DocumentInputStream;
+import org.apache.poi.poifs.filesystem.POIFSFileSystem;
+import org.apache.poi.util.LittleEndian;
+
/**
* @author Maxim Valyanskiy
*/
@@ -104,7 +104,7 @@ public class Decryptor {
System.arraycopy(x1, 0, x3, 0, x1.length);
System.arraycopy(x2, 0, x3, x1.length, x2.length);
- return Arrays.copyOf(x3, requiredKeyLength);
+ return truncateOrPad(x3, requiredKeyLength);
}
public boolean verifyPassword(String password) throws GeneralSecurityException {
@@ -117,10 +117,26 @@ public class Decryptor {
MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
byte[] calcVerifierHash = sha1.digest(verifier);
- byte[] verifierHash = Arrays.copyOf(cipher.doFinal(info.getVerifier().getVerifierHash()), calcVerifierHash.length);
+ byte[] verifierHash = truncateOrPad(cipher.doFinal(info.getVerifier().getVerifierHash()), calcVerifierHash.length);
return Arrays.equals(calcVerifierHash, verifierHash);
}
+
+ /**
+ * Returns a byte array of the requested length,
+ * truncated or zero padded as needed.
+ * Behaves like Arrays.copyOf in Java 1.6
+ */
+ private byte[] truncateOrPad(byte[] source, int length) {
+ byte[] result = new byte[length];
+ System.arraycopy(source, 0, result, 0, Math.min(length, source.length));
+ if(length > source.length) {
+ for(int i=source.length; i<length; i++) {
+ result[i] = 0;
+ }
+ }
+ return result;
+ }
private Cipher getCipher() throws GeneralSecurityException {
byte[] key = generateKey(0);