Search in sources :

Example 91 with POIFSFileSystem

use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.

the class TestSecureTempZip method protectedXLSBZip.

/**
     * Now try with xlsb.
     */
@Test
public void protectedXLSBZip() throws IOException, GeneralSecurityException, XmlException, OpenXML4JException {
    //The test file requires that JCE unlimited be installed.
    //If it isn't installed, skip this test.
    int maxKeyLen = Cipher.getMaxAllowedKeyLength("AES");
    Assume.assumeTrue("Please install JCE Unlimited Strength Jurisdiction Policy files for AES 256", maxKeyLen == 2147483647);
    File tikaProt = XSSFTestDataSamples.getSampleFile("protected_passtika.xlsb");
    FileInputStream fis = new FileInputStream(tikaProt);
    POIFSFileSystem poifs = new POIFSFileSystem(fis);
    EncryptionInfo ei = new EncryptionInfo(poifs);
    Decryptor dec = ei.getDecryptor();
    boolean passOk = dec.verifyPassword("tika");
    assertTrue(passOk);
    // extract encrypted ooxml file and write to custom encrypted zip file
    InputStream is = dec.getDataStream(poifs);
    // provide ZipEntrySource to poi which decrypts on the fly
    ZipEntrySource source = AesZipFileZipEntrySource.createZipEntrySource(is);
    // test the source
    OPCPackage opc = OPCPackage.open(source);
    String expected = "You can't see me";
    XSSFBEventBasedExcelExtractor extractor = new XSSFBEventBasedExcelExtractor(opc);
    extractor.setIncludeSheetNames(false);
    String txt = extractor.getText();
    assertEquals(expected, txt.trim());
    extractor.close();
    opc.close();
    poifs.close();
    fis.close();
}
Also used : XSSFBEventBasedExcelExtractor(org.apache.poi.xssf.extractor.XSSFBEventBasedExcelExtractor) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) File(java.io.File) ZipEntrySource(org.apache.poi.openxml4j.util.ZipEntrySource) AesZipFileZipEntrySource(org.apache.poi.poifs.crypt.temp.AesZipFileZipEntrySource) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 92 with POIFSFileSystem

use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.

the class TestDecryptor method agile.

@Test
public void agile() throws IOException, GeneralSecurityException {
    POIFSFileSystem fs = new POIFSFileSystem(POIDataSamples.getPOIFSInstance().openResourceAsStream("protected_agile.docx"));
    EncryptionInfo info = new EncryptionInfo(fs);
    assertTrue(info.getVersionMajor() == 4 && info.getVersionMinor() == 4);
    Decryptor d = Decryptor.getInstance(info);
    assertTrue(d.verifyPassword(Decryptor.DEFAULT_PASSWORD));
    zipOk(fs.getRoot(), d);
    fs.close();
}
Also used : POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) Test(org.junit.Test)

Example 93 with POIFSFileSystem

use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.

the class TestDecryptor method dataLength.

@Test
public void dataLength() throws Exception {
    POIFSFileSystem fs = new POIFSFileSystem(POIDataSamples.getPOIFSInstance().openResourceAsStream("protected_agile.docx"));
    EncryptionInfo info = new EncryptionInfo(fs);
    Decryptor d = Decryptor.getInstance(info);
    d.verifyPassword(Decryptor.DEFAULT_PASSWORD);
    InputStream is = d.getDataStream(fs);
    long len = d.getLength();
    assertEquals(12810, len);
    byte[] buf = new byte[(int) len];
    is.read(buf);
    ZipInputStream zin = new ZipInputStream(new ByteArrayInputStream(buf));
    while (true) {
        ZipEntry entry = zin.getNextEntry();
        if (entry == null) {
            break;
        }
        while (zin.available() > 0) {
            zin.skip(zin.available());
        }
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) Test(org.junit.Test)

Example 94 with POIFSFileSystem

use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.

the class TestEncryptionInfo method testEncryptionInfoSHA512.

@Test
public void testEncryptionInfoSHA512() throws Exception {
    POIFSFileSystem fs = new POIFSFileSystem(POIDataSamples.getPOIFSInstance().openResourceAsStream("protected_sha512.xlsx"));
    EncryptionInfo info = new EncryptionInfo(fs);
    assertEquals(4, info.getVersionMajor());
    assertEquals(4, info.getVersionMinor());
    assertEquals(CipherAlgorithm.aes256, info.getHeader().getCipherAlgorithm());
    assertEquals(HashAlgorithm.sha512, info.getHeader().getHashAlgorithm());
    assertEquals(256, info.getHeader().getKeySize());
    assertEquals(64, info.getVerifier().getEncryptedVerifierHash().length);
    assertEquals(CipherProvider.aes, info.getHeader().getCipherProvider());
    //        assertEquals("Microsoft Enhanced RSA and AES Cryptographic Provider", info.getHeader().getCspName());
    fs.close();
}
Also used : POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) Test(org.junit.Test)

Example 95 with POIFSFileSystem

use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.

the class TestHDGFCore method testV5.

public void testV5() throws Exception {
    fs = new POIFSFileSystem(_dgTests.openResourceAsStream("v5_Connection_Types.vsd"));
    hdgf = new HDGFDiagram(fs);
    assertNotNull(hdgf);
    textExtractor = new VisioTextExtractor(hdgf);
    String text = textExtractor.getText().replace("", "").trim();
    assertEquals("Static to Static\nDynamic to Static\nDynamic to Dynamic", text);
}
Also used : POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) VisioTextExtractor(org.apache.poi.hdgf.extractor.VisioTextExtractor)

Aggregations

POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)121 Test (org.junit.Test)58 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)38 InputStream (java.io.InputStream)36 ByteArrayInputStream (java.io.ByteArrayInputStream)33 ByteArrayOutputStream (java.io.ByteArrayOutputStream)33 FileInputStream (java.io.FileInputStream)31 File (java.io.File)25 OPOIFSFileSystem (org.apache.poi.poifs.filesystem.OPOIFSFileSystem)15 FileOutputStream (java.io.FileOutputStream)14 OutputStream (java.io.OutputStream)14 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)13 DirectoryNode (org.apache.poi.poifs.filesystem.DirectoryNode)13 TempFile (org.apache.poi.util.TempFile)13 IOException (java.io.IOException)12 MutablePropertySet (org.apache.poi.hpsf.MutablePropertySet)7 MutableSection (org.apache.poi.hpsf.MutableSection)7 HashMap (java.util.HashMap)6 DocumentEntry (org.apache.poi.poifs.filesystem.DocumentEntry)6 NDocumentOutputStream (org.apache.poi.poifs.filesystem.NDocumentOutputStream)6