Search in sources :

Example 6 with EncryptionInfo

use of org.apache.poi.poifs.crypt.EncryptionInfo in project poi by apache.

the class TestDocumentEncryption method cryptoAPIChangeKeySize.

@Test
public void cryptoAPIChangeKeySize() throws Exception {
    String pptFile = "cryptoapi-proc2356.ppt";
    Biff8EncryptionKey.setCurrentUserPassword("crypto");
    NPOIFSFileSystem fs = new NPOIFSFileSystem(slTests.getFile(pptFile), true);
    HSLFSlideShowImpl hss = new HSLFSlideShowImpl(fs);
    // need to cache data (i.e. read all data) before changing the key size
    List<HSLFPictureData> picsExpected = hss.getPictureData();
    hss.getDocumentSummaryInformation();
    EncryptionInfo ei = hss.getDocumentEncryptionAtom().getEncryptionInfo();
    ((CryptoAPIEncryptionHeader) ei.getHeader()).setKeySize(0x78);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    hss.write(bos);
    hss.close();
    fs.close();
    fs = new NPOIFSFileSystem(new ByteArrayInputStream(bos.toByteArray()));
    hss = new HSLFSlideShowImpl(fs);
    List<HSLFPictureData> picsActual = hss.getPictureData();
    assertEquals(picsExpected.size(), picsActual.size());
    for (int i = 0; i < picsExpected.size(); i++) {
        assertArrayEquals(picsExpected.get(i).getRawData(), picsActual.get(i).getRawData());
    }
    hss.close();
    fs.close();
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) CryptoAPIEncryptionHeader(org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIEncryptionHeader) ByteArrayInputStream(java.io.ByteArrayInputStream) EncryptionInfo(org.apache.poi.poifs.crypt.EncryptionInfo) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HSLFPictureData(org.apache.poi.hslf.usermodel.HSLFPictureData) HSLFSlideShowImpl(org.apache.poi.hslf.usermodel.HSLFSlideShowImpl) Test(org.junit.Test)

Example 7 with EncryptionInfo

use of org.apache.poi.poifs.crypt.EncryptionInfo in project poi by apache.

the class TestBinaryRC4 method confirmValid.

private static void confirmValid(boolean expectedResult, String docIdHex, String saltDataHex, String saltHashHex, String password) throws GeneralSecurityException {
    byte[] docId = readFromString(docIdHex);
    byte[] saltData = readFromString(saltDataHex);
    byte[] saltHash = readFromString(saltHashHex);
    EncryptionInfo ei = new EncryptionInfo(EncryptionMode.binaryRC4);
    BinaryRC4EncryptionVerifier ver = (BinaryRC4EncryptionVerifier) ei.getVerifier();
    ver.setSalt(docId);
    ver.setEncryptedVerifier(saltData);
    ver.setEncryptedVerifierHash(saltHash);
    String pass = password == null ? Decryptor.DEFAULT_PASSWORD : password;
    boolean actResult = ei.getDecryptor().verifyPassword(pass);
    if (expectedResult) {
        assertTrue("validate failed", actResult);
    } else {
        assertFalse("validate succeeded unexpectedly", actResult);
    }
}
Also used : EncryptionInfo(org.apache.poi.poifs.crypt.EncryptionInfo) HexRead.readFromString(org.apache.poi.util.HexRead.readFromString)

Example 8 with EncryptionInfo

use of org.apache.poi.poifs.crypt.EncryptionInfo in project poi by apache.

the class HSLFSlideShowEncrypted method decryptInit.

protected void decryptInit() {
    if (dec != null) {
        return;
    }
    EncryptionInfo ei = dea.getEncryptionInfo();
    dec = (CryptoAPIDecryptor) ei.getDecryptor();
}
Also used : EncryptionInfo(org.apache.poi.poifs.crypt.EncryptionInfo)

Example 9 with EncryptionInfo

use of org.apache.poi.poifs.crypt.EncryptionInfo in project poi by apache.

the class HSLFSlideShowEncrypted method updateEncryptionRecord.

protected Record[] updateEncryptionRecord(Record[] records) {
    String password = Biff8EncryptionKey.getCurrentUserPassword();
    if (password == null) {
        if (dea == null) {
            // no password given, no encryption record exits -> done
            return records;
        } else {
            // need to remove password data
            dea = null;
            return removeEncryptionRecord(records);
        }
    } else {
        // create password record
        if (dea == null) {
            dea = new DocumentEncryptionAtom();
            enc = null;
        }
        encryptInit();
        EncryptionInfo ei = dea.getEncryptionInfo();
        byte[] salt = ei.getVerifier().getSalt();
        if (salt == null) {
            enc.confirmPassword(password);
        } else {
            byte[] verifier = ei.getDecryptor().getVerifier();
            enc.confirmPassword(password, null, null, verifier, salt, null);
        }
        // move EncryptionRecord to last slide position
        records = normalizeRecords(records);
        return addEncryptionRecord(records, dea);
    }
}
Also used : DocumentEncryptionAtom(org.apache.poi.hslf.record.DocumentEncryptionAtom) EncryptionInfo(org.apache.poi.poifs.crypt.EncryptionInfo)

Example 10 with EncryptionInfo

use of org.apache.poi.poifs.crypt.EncryptionInfo in project poi by apache.

the class HSLFSlideShowEncrypted method encryptInit.

protected void encryptInit() {
    if (enc != null) {
        return;
    }
    EncryptionInfo ei = dea.getEncryptionInfo();
    enc = (CryptoAPIEncryptor) ei.getEncryptor();
}
Also used : EncryptionInfo(org.apache.poi.poifs.crypt.EncryptionInfo)

Aggregations

EncryptionInfo (org.apache.poi.poifs.crypt.EncryptionInfo)20 Decryptor (org.apache.poi.poifs.crypt.Decryptor)7 Test (org.junit.Test)5 File (java.io.File)4 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)4 Encryptor (org.apache.poi.poifs.crypt.Encryptor)4 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)4 LittleEndianByteArrayOutputStream (org.apache.poi.util.LittleEndianByteArrayOutputStream)4 FileOutputStream (java.io.FileOutputStream)3 InputStream (java.io.InputStream)3 GeneralSecurityException (java.security.GeneralSecurityException)3 SecretKey (javax.crypto.SecretKey)3 EncryptionRecord (org.apache.poi.poifs.crypt.standard.EncryptionRecord)3 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)3 OutputStream (java.io.OutputStream)2 ZipFile (java.util.zip.ZipFile)2 EncryptedDocumentException (org.apache.poi.EncryptedDocumentException)2 TempFile (org.apache.poi.util.TempFile)2 XWPFWordExtractor (org.apache.poi.xwpf.extractor.XWPFWordExtractor)2 XWPFDocument (org.apache.poi.xwpf.usermodel.XWPFDocument)2