import java.security.MessageDigest;\r
import java.security.Provider;\r
import java.security.Security;\r
+import java.security.spec.AlgorithmParameterSpec;\r
import java.util.Arrays;\r
\r
import javax.crypto.Cipher;\r
import javax.crypto.Mac;\r
import javax.crypto.SecretKey;\r
import javax.crypto.spec.IvParameterSpec;\r
+import javax.crypto.spec.RC2ParameterSpec;\r
\r
import org.apache.poi.EncryptedDocumentException;\r
import org.apache.poi.util.LittleEndian;\r
if (vec == null) {\r
cipher.init(cipherMode, key);\r
} else {\r
- IvParameterSpec iv = new IvParameterSpec(vec);\r
- cipher.init(cipherMode, key, iv);\r
+ AlgorithmParameterSpec aps;\r
+ if (cipherAlgorithm == CipherAlgorithm.rc2) {\r
+ aps = new RC2ParameterSpec(key.getEncoded().length*8, vec);\r
+ } else {\r
+ aps = new IvParameterSpec(vec);\r
+ }\r
+ cipher.init(cipherMode, key, aps);\r
}\r
return cipher;\r
} catch (GeneralSecurityException e) {\r
import java.security.KeyPair;
import java.security.MessageDigest;
import java.security.cert.X509Certificate;
+import java.security.spec.AlgorithmParameterSpec;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
+import javax.crypto.spec.RC2ParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.poi.EncryptedDocumentException;
LittleEndian.putInt(blockKey, 0, index);
EncryptionHeader header = info.getHeader();
byte[] iv = generateIv(header.getHashAlgorithmEx(), header.getKeySalt(), blockKey, getBlockSizeInBytes());
- _cipher.init(Cipher.DECRYPT_MODE, getSecretKey(), new IvParameterSpec(iv));
+ AlgorithmParameterSpec aps;
+ if (header.getCipherAlgorithm() == CipherAlgorithm.rc2) {
+ aps = new RC2ParameterSpec(getSecretKey().getEncoded().length*8, iv);
+ } else {
+ aps = new IvParameterSpec(iv);
+ }
+
+ _cipher.init(Cipher.DECRYPT_MODE, getSecretKey(), aps);
if (_lastIndex != index)
_stream.skip((index - _lastIndex) << 12);
import java.security.MessageDigest;\r
import java.security.SecureRandom;\r
import java.security.cert.CertificateEncodingException;\r
+import java.security.spec.AlgorithmParameterSpec;\r
import java.util.HashMap;\r
import java.util.Map;\r
import java.util.Random;\r
import javax.crypto.Mac;\r
import javax.crypto.SecretKey;\r
import javax.crypto.spec.IvParameterSpec;\r
+import javax.crypto.spec.RC2ParameterSpec;\r
import javax.crypto.spec.SecretKeySpec;\r
\r
import org.apache.poi.EncryptedDocumentException;\r
+import org.apache.poi.poifs.crypt.CipherAlgorithm;\r
import org.apache.poi.poifs.crypt.CryptoFunctions;\r
import org.apache.poi.poifs.crypt.DataSpaceMapUtils;\r
import org.apache.poi.poifs.crypt.EncryptionHeader;\r
LittleEndian.putInt(blockKey, 0, index);\r
byte[] iv = generateIv(header.getHashAlgorithmEx(), header.getKeySalt(), blockKey, blockSize);\r
try {\r
- _cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(), new IvParameterSpec(iv));\r
+ AlgorithmParameterSpec aps;\r
+ if (header.getCipherAlgorithm() == CipherAlgorithm.rc2) {\r
+ aps = new RC2ParameterSpec(getSecretKey().getEncoded().length*8, iv);\r
+ } else {\r
+ aps = new IvParameterSpec(iv);\r
+ }\r
+ \r
+ _cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(), aps);\r
int ciLen = _cipher.doFinal(_chunk, 0, posInChunk, _chunk);\r
out.write(_chunk, 0, ciLen);\r
} catch (GeneralSecurityException e) {\r
import java.util.Collection;\r
import java.util.List;\r
\r
+import javax.crypto.Cipher;\r
+\r
import org.apache.poi.POIDataSamples;\r
import org.apache.poi.poifs.filesystem.POIFSFileSystem;\r
import org.apache.poi.util.IOUtils;\r
+import org.junit.Assume;\r
import org.junit.BeforeClass;\r
import org.junit.Test;\r
import org.junit.runner.RunWith;\r
\r
@Test\r
public void testAgileEncryptionModes() throws Exception {\r
+ int maxKeyLen = Cipher.getMaxAllowedKeyLength(ca.jceId);\r
+ Assume.assumeTrue("Please install JCE Unlimited Strength Jurisdiction Policy files", maxKeyLen >= ca.defaultKeySize);\r
+ \r
ByteArrayOutputStream bos = new ByteArrayOutputStream();\r
\r
POIFSFileSystem fsEnc = new POIFSFileSystem();\r
@Test\r
public void testCertificateEncryption() throws Exception {\r
POIFSFileSystem fs = new POIFSFileSystem();\r
- EncryptionInfo info = new EncryptionInfo(fs, EncryptionMode.agile, CipherAlgorithm.aes192, HashAlgorithm.sha1, -1, -1, ChainingMode.cbc);\r
+ EncryptionInfo info = new EncryptionInfo(fs, EncryptionMode.agile, CipherAlgorithm.aes128, HashAlgorithm.sha1, -1, -1, ChainingMode.cbc);\r
AgileEncryptionVerifier aev = (AgileEncryptionVerifier)info.getVerifier();\r
CertData certData = loadKeystore();\r
aev.addCertificate(certData.x509);\r
import java.io.OutputStream;\r
import java.util.Iterator;\r
\r
+import javax.crypto.Cipher;\r
+\r
import org.apache.poi.POIDataSamples;\r
import org.apache.poi.poifs.crypt.agile.AgileEncryptionHeader;\r
import org.apache.poi.poifs.filesystem.DirectoryNode;\r
import org.apache.poi.poifs.filesystem.POIFSFileSystem;\r
import org.apache.poi.util.BoundedInputStream;\r
import org.apache.poi.util.IOUtils;\r
+import org.junit.Assume;\r
import org.junit.Test;\r
\r
public class TestEncryptor {\r
@Test\r
public void testAgileEncryption() throws Exception {\r
+ int maxKeyLen = Cipher.getMaxAllowedKeyLength("AES");\r
+ Assume.assumeTrue("Please install JCE Unlimited Strength Jurisdiction Policy files for AES 256", maxKeyLen == 2147483647);\r
+\r
File file = POIDataSamples.getDocumentInstance().getFile("bug53475-password-is-pass.docx");\r
String pass = "pass";\r
NPOIFSFileSystem nfs = new NPOIFSFileSystem(file);\r