Search in sources :

Example 1 with CryptoAPIEncryptionHeader

use of org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIEncryptionHeader in project poi by apache.

the class DocumentEncryptionAtom method writeOut.

/**
	 * Write the contents of the record back, so it can be written
	 *  to disk
	 */
public void writeOut(OutputStream out) throws IOException {
    // Data
    byte[] data = new byte[1024];
    LittleEndianByteArrayOutputStream bos = new LittleEndianByteArrayOutputStream(data, 0);
    bos.writeShort(ei.getVersionMajor());
    bos.writeShort(ei.getVersionMinor());
    bos.writeInt(ei.getEncryptionFlags());
    ((CryptoAPIEncryptionHeader) ei.getHeader()).write(bos);
    ((CryptoAPIEncryptionVerifier) ei.getVerifier()).write(bos);
    // Header
    LittleEndian.putInt(_header, 4, bos.getWriteIndex());
    out.write(_header);
    out.write(data, 0, bos.getWriteIndex());
    bos.close();
}
Also used : CryptoAPIEncryptionVerifier(org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIEncryptionVerifier) LittleEndianByteArrayOutputStream(org.apache.poi.util.LittleEndianByteArrayOutputStream) CryptoAPIEncryptionHeader(org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIEncryptionHeader)

Example 2 with CryptoAPIEncryptionHeader

use of org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIEncryptionHeader in project poi by apache.

the class FilePassRecord method serialize.

@SuppressWarnings("resource")
@Override
public void serialize(LittleEndianOutput out) {
    out.writeShort(encryptionType);
    byte[] data = new byte[1024];
    // NOSONAR
    LittleEndianByteArrayOutputStream bos = new LittleEndianByteArrayOutputStream(data, 0);
    switch(encryptionInfo.getEncryptionMode()) {
        case xor:
            ((XOREncryptionHeader) encryptionInfo.getHeader()).write(bos);
            ((XOREncryptionVerifier) encryptionInfo.getVerifier()).write(bos);
            break;
        case binaryRC4:
            out.writeShort(encryptionInfo.getVersionMajor());
            out.writeShort(encryptionInfo.getVersionMinor());
            ((BinaryRC4EncryptionHeader) encryptionInfo.getHeader()).write(bos);
            ((BinaryRC4EncryptionVerifier) encryptionInfo.getVerifier()).write(bos);
            break;
        case cryptoAPI:
            out.writeShort(encryptionInfo.getVersionMajor());
            out.writeShort(encryptionInfo.getVersionMinor());
            out.writeInt(encryptionInfo.getEncryptionFlags());
            ((CryptoAPIEncryptionHeader) encryptionInfo.getHeader()).write(bos);
            ((CryptoAPIEncryptionVerifier) encryptionInfo.getVerifier()).write(bos);
            break;
        default:
            throw new EncryptedDocumentException("not supported");
    }
    out.write(data, 0, bos.getWriteIndex());
}
Also used : CryptoAPIEncryptionVerifier(org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIEncryptionVerifier) LittleEndianByteArrayOutputStream(org.apache.poi.util.LittleEndianByteArrayOutputStream) EncryptedDocumentException(org.apache.poi.EncryptedDocumentException) CryptoAPIEncryptionHeader(org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIEncryptionHeader) XOREncryptionHeader(org.apache.poi.poifs.crypt.xor.XOREncryptionHeader) BinaryRC4EncryptionVerifier(org.apache.poi.poifs.crypt.binaryrc4.BinaryRC4EncryptionVerifier) XOREncryptionVerifier(org.apache.poi.poifs.crypt.xor.XOREncryptionVerifier) BinaryRC4EncryptionHeader(org.apache.poi.poifs.crypt.binaryrc4.BinaryRC4EncryptionHeader)

Example 3 with CryptoAPIEncryptionHeader

use of org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIEncryptionHeader 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)

Aggregations

CryptoAPIEncryptionHeader (org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIEncryptionHeader)3 CryptoAPIEncryptionVerifier (org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIEncryptionVerifier)2 LittleEndianByteArrayOutputStream (org.apache.poi.util.LittleEndianByteArrayOutputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 EncryptedDocumentException (org.apache.poi.EncryptedDocumentException)1 HSLFPictureData (org.apache.poi.hslf.usermodel.HSLFPictureData)1 HSLFSlideShowImpl (org.apache.poi.hslf.usermodel.HSLFSlideShowImpl)1 EncryptionInfo (org.apache.poi.poifs.crypt.EncryptionInfo)1 BinaryRC4EncryptionHeader (org.apache.poi.poifs.crypt.binaryrc4.BinaryRC4EncryptionHeader)1 BinaryRC4EncryptionVerifier (org.apache.poi.poifs.crypt.binaryrc4.BinaryRC4EncryptionVerifier)1 XOREncryptionHeader (org.apache.poi.poifs.crypt.xor.XOREncryptionHeader)1 XOREncryptionVerifier (org.apache.poi.poifs.crypt.xor.XOREncryptionVerifier)1 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)1 Test (org.junit.Test)1