Search in sources :

Example 1 with CryptoAPIDecryptor

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

the class POIDocument method getPropertySet.

/** 
     * For a given named property entry, either return it or null if
     *  if it wasn't found
     *  
     *  @param setName The property to read
     *  @param encryptionInfo the encryption descriptor in case of cryptoAPI encryption
     *  @return The value of the given property or null if it wasn't found.
     */
protected PropertySet getPropertySet(String setName, EncryptionInfo encryptionInfo) {
    DirectoryNode dirNode = directory;
    NPOIFSFileSystem encPoifs = null;
    String step = "getting";
    try {
        if (encryptionInfo != null) {
            step = "getting encrypted";
            String encryptedStream = null;
            for (String s : encryptedStreamNames) {
                if (dirNode.hasEntry(s)) {
                    encryptedStream = s;
                }
            }
            if (encryptedStream == null) {
                throw new EncryptedDocumentException("can't find matching encrypted property stream");
            }
            CryptoAPIDecryptor dec = (CryptoAPIDecryptor) encryptionInfo.getDecryptor();
            encPoifs = dec.getSummaryEntries(dirNode, encryptedStream);
            dirNode = encPoifs.getRoot();
        }
        //directory can be null when creating new documents
        if (dirNode == null || !dirNode.hasEntry(setName)) {
            return null;
        }
        // Find the entry, and get an input stream for it
        step = "getting";
        DocumentInputStream dis = dirNode.createDocumentInputStream(dirNode.getEntry(setName));
        try {
            // Create the Property Set
            step = "creating";
            return PropertySetFactory.create(dis);
        } finally {
            dis.close();
        }
    } catch (Exception e) {
        logger.log(POILogger.WARN, "Error " + step + " property set with name " + setName, e);
        return null;
    } finally {
        if (encPoifs != null) {
            try {
                encPoifs.close();
            } catch (IOException e) {
                logger.log(POILogger.WARN, "Error closing encrypted property poifs", e);
            }
        }
    }
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) IOException(java.io.IOException) DocumentInputStream(org.apache.poi.poifs.filesystem.DocumentInputStream) CryptoAPIDecryptor(org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIDecryptor) IOException(java.io.IOException)

Example 2 with CryptoAPIDecryptor

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

the class TestDocumentEncryption method cryptoAPIDecryption.

@Test
public void cryptoAPIDecryption() throws Exception {
    // taken from a msdn blog:
    // http://blogs.msdn.com/b/openspecification/archive/2009/05/08/dominic-salemno.aspx
    Biff8EncryptionKey.setCurrentUserPassword("crypto");
    NPOIFSFileSystem fs = new NPOIFSFileSystem(slTests.getFile("cryptoapi-proc2356.ppt"));
    HSLFSlideShowImpl hss = new HSLFSlideShowImpl(fs);
    HSLFSlideShow ss = new HSLFSlideShow(hss);
    HSLFSlide slide = ss.getSlides().get(0);
    String rawText = HSLFTextParagraph.getRawText(slide.getTextParagraphs().get(0));
    assertEquals("Dominic Salemno", rawText);
    String[][] picCmp = { { "0", "nKsDTKqxTCR8LFkVVWlP9GSTvZ0=" }, { "95163", "SuNOR+9V1UVYZIoeD65l3VTaLoc=" }, { "100864", "Ql3IGrr4bNq07ZTp5iPg7b+pva8=" }, { "714114", "8pdst9NjBGSfWezSZE8+aVhIRe0=" }, { "723752", "go6xqW7lvkCtlOO5tYLiMfb4oxw=" }, { "770128", "gZUM8YqRNL5kGNfyyYvEEernvCc=" }, { "957958", "CNU2iiqUFAnk3TDXsXV1ihH9eRM=" } };
    MessageDigest md = CryptoFunctions.getMessageDigest(HashAlgorithm.sha1);
    List<HSLFPictureData> pd = hss.getPictureData();
    int i = 0;
    for (HSLFPictureData p : pd) {
        byte[] hash = md.digest(p.getData());
        assertEquals(Integer.parseInt(picCmp[i][0]), p.getOffset());
        assertEquals(picCmp[i][1], Base64.encodeBase64String(hash));
        i++;
    }
    DocumentEncryptionAtom dea = hss.getDocumentEncryptionAtom();
    POIFSFileSystem fs2 = ((CryptoAPIDecryptor) dea.getEncryptionInfo().getDecryptor()).getSummaryEntries(fs.getRoot(), "EncryptedSummary");
    PropertySet ps = PropertySetFactory.create(fs2.getRoot(), SummaryInformation.DEFAULT_STREAM_NAME);
    assertTrue(ps.isSummaryInformation());
    assertEquals("RC4 CryptoAPI Encryption", ps.getProperties()[1].getValue());
    ps = PropertySetFactory.create(fs2.getRoot(), DocumentSummaryInformation.DEFAULT_STREAM_NAME);
    assertTrue(ps.isDocumentSummaryInformation());
    assertEquals("On-screen Show (4:3)", ps.getProperties()[1].getValue());
    ss.close();
    fs.close();
    fs2.close();
}
Also used : HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) HSLFSlideShowImpl(org.apache.poi.hslf.usermodel.HSLFSlideShowImpl) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) PropertySet(org.apache.poi.hpsf.PropertySet) MessageDigest(java.security.MessageDigest) HSLFPictureData(org.apache.poi.hslf.usermodel.HSLFPictureData) HSLFSlide(org.apache.poi.hslf.usermodel.HSLFSlide) CryptoAPIDecryptor(org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIDecryptor) Test(org.junit.Test)

Aggregations

CryptoAPIDecryptor (org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIDecryptor)2 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)2 IOException (java.io.IOException)1 MessageDigest (java.security.MessageDigest)1 PropertySet (org.apache.poi.hpsf.PropertySet)1 HSLFPictureData (org.apache.poi.hslf.usermodel.HSLFPictureData)1 HSLFSlide (org.apache.poi.hslf.usermodel.HSLFSlide)1 HSLFSlideShow (org.apache.poi.hslf.usermodel.HSLFSlideShow)1 HSLFSlideShowImpl (org.apache.poi.hslf.usermodel.HSLFSlideShowImpl)1 DirectoryNode (org.apache.poi.poifs.filesystem.DirectoryNode)1 DocumentInputStream (org.apache.poi.poifs.filesystem.DocumentInputStream)1 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)1 Test (org.junit.Test)1