Search in sources :

Example 16 with EncryptedDocumentException

use of org.apache.poi.EncryptedDocumentException in project poi by apache.

the class TestWorkbookFactory method testCreateWithPasswordFromStream.

/**
     * Check that the overloaded stream methods which take passwords work properly
     */
@Test
public void testCreateWithPasswordFromStream() throws Exception {
    Workbook wb;
    // Unprotected, no password given, opens normally
    wb = WorkbookFactory.create(HSSFTestDataSamples.openSampleFileStream(xls), null);
    assertNotNull(wb);
    assertTrue(wb instanceof HSSFWorkbook);
    assertCloseDoesNotModifyFile(xls, wb);
    wb = WorkbookFactory.create(HSSFTestDataSamples.openSampleFileStream(xlsx), null);
    assertNotNull(wb);
    assertTrue(wb instanceof XSSFWorkbook);
    assertCloseDoesNotModifyFile(xlsx, wb);
    // Unprotected, wrong password, opens normally
    wb = WorkbookFactory.create(HSSFTestDataSamples.openSampleFileStream(xls), "wrong");
    assertNotNull(wb);
    assertTrue(wb instanceof HSSFWorkbook);
    assertCloseDoesNotModifyFile(xls, wb);
    wb = WorkbookFactory.create(HSSFTestDataSamples.openSampleFileStream(xlsx), "wrong");
    assertNotNull(wb);
    assertTrue(wb instanceof XSSFWorkbook);
    assertCloseDoesNotModifyFile(xlsx, wb);
    // Protected, correct password, opens fine
    wb = WorkbookFactory.create(HSSFTestDataSamples.openSampleFileStream(xls_prot[0]), xls_prot[1]);
    assertNotNull(wb);
    assertTrue(wb instanceof HSSFWorkbook);
    assertCloseDoesNotModifyFile(xls_prot[0], wb);
    wb = WorkbookFactory.create(HSSFTestDataSamples.openSampleFileStream(xlsx_prot[0]), xlsx_prot[1]);
    assertNotNull(wb);
    assertTrue(wb instanceof XSSFWorkbook);
    assertCloseDoesNotModifyFile(xlsx_prot[0], wb);
    // Protected, wrong password, throws Exception
    try {
        wb = WorkbookFactory.create(HSSFTestDataSamples.openSampleFileStream(xls_prot[0]), "wrong");
        assertCloseDoesNotModifyFile(xls_prot[0], wb);
        fail("Shouldn't be able to open with the wrong password");
    } catch (EncryptedDocumentException e) {
    }
    try {
        wb = WorkbookFactory.create(HSSFTestDataSamples.openSampleFileStream(xlsx_prot[0]), "wrong");
        assertCloseDoesNotModifyFile(xlsx_prot[0], wb);
        fail("Shouldn't be able to open with the wrong password");
    } catch (EncryptedDocumentException e) {
    }
}
Also used : EncryptedDocumentException(org.apache.poi.EncryptedDocumentException) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.Test)

Example 17 with EncryptedDocumentException

use of org.apache.poi.EncryptedDocumentException in project poi by apache.

the class TestWorkbookFactory method testCreateWithPasswordFromFile.

/**
     * Check that the overloaded file methods which take passwords work properly
     */
@Test
public void testCreateWithPasswordFromFile() throws Exception {
    Workbook wb;
    // Unprotected, no password given, opens normally
    wb = WorkbookFactory.create(HSSFTestDataSamples.getSampleFile(xls), null);
    assertNotNull(wb);
    assertTrue(wb instanceof HSSFWorkbook);
    assertCloseDoesNotModifyFile(xls, wb);
    wb = WorkbookFactory.create(HSSFTestDataSamples.getSampleFile(xlsx), null);
    assertNotNull(wb);
    assertTrue(wb instanceof XSSFWorkbook);
    assertCloseDoesNotModifyFile(xlsx, wb);
    // Unprotected, wrong password, opens normally
    wb = WorkbookFactory.create(HSSFTestDataSamples.getSampleFile(xls), "wrong");
    assertNotNull(wb);
    assertTrue(wb instanceof HSSFWorkbook);
    assertCloseDoesNotModifyFile(xls, wb);
    wb = WorkbookFactory.create(HSSFTestDataSamples.getSampleFile(xlsx), "wrong");
    assertNotNull(wb);
    assertTrue(wb instanceof XSSFWorkbook);
    assertCloseDoesNotModifyFile(xlsx, wb);
    // Protected, correct password, opens fine
    wb = WorkbookFactory.create(HSSFTestDataSamples.getSampleFile(xls_prot[0]), xls_prot[1]);
    assertNotNull(wb);
    assertTrue(wb instanceof HSSFWorkbook);
    assertCloseDoesNotModifyFile(xls_prot[0], wb);
    wb = WorkbookFactory.create(HSSFTestDataSamples.getSampleFile(xlsx_prot[0]), xlsx_prot[1]);
    assertNotNull(wb);
    assertTrue(wb instanceof XSSFWorkbook);
    assertTrue(wb.getNumberOfSheets() > 0);
    assertNotNull(wb.getSheetAt(0));
    assertNotNull(wb.getSheetAt(0).getRow(0));
    assertCloseDoesNotModifyFile(xlsx_prot[0], wb);
    // Protected, wrong password, throws Exception
    try {
        wb = WorkbookFactory.create(HSSFTestDataSamples.getSampleFile(xls_prot[0]), "wrong");
        assertCloseDoesNotModifyFile(xls_prot[0], wb);
        fail("Shouldn't be able to open with the wrong password");
    } catch (EncryptedDocumentException e) {
    // expected here
    }
    try {
        wb = WorkbookFactory.create(HSSFTestDataSamples.getSampleFile(xlsx_prot[0]), "wrong");
        assertCloseDoesNotModifyFile(xlsx_prot[0], wb);
        fail("Shouldn't be able to open with the wrong password");
    } catch (EncryptedDocumentException e) {
    // expected here
    }
}
Also used : EncryptedDocumentException(org.apache.poi.EncryptedDocumentException) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.Test)

Example 18 with EncryptedDocumentException

use of org.apache.poi.EncryptedDocumentException in project poi by apache.

the class TestOldExcelExtractor method testEncryptionException.

@Test
public void testEncryptionException() throws IOException {
    //test file derives from Common Crawl
    File file = HSSFTestDataSamples.getSampleFile("60284.xls");
    OldExcelExtractor ex = new OldExcelExtractor(file);
    assertEquals(5, ex.getBiffVersion());
    assertEquals(5, ex.getFileType());
    try {
        ex.getText();
        fail();
    } catch (EncryptedDocumentException e) {
        assertTrue("correct exception thrown", true);
    }
    ex.close();
}
Also used : EncryptedDocumentException(org.apache.poi.EncryptedDocumentException) File(java.io.File) Test(org.junit.Test)

Example 19 with EncryptedDocumentException

use of org.apache.poi.EncryptedDocumentException in project poi by apache.

the class TestHSSFEventFactory method testWithPasswordProtectedWorkbooks.

public void testWithPasswordProtectedWorkbooks() throws Exception {
    HSSFRequest req = new HSSFRequest();
    MockHSSFListener mockListen = new MockHSSFListener();
    req.addListenerForAllRecords(mockListen);
    // Without a password, can't be read
    Biff8EncryptionKey.setCurrentUserPassword(null);
    POIFSFileSystem fs = new POIFSFileSystem(openSample("xor-encryption-abc.xls"));
    HSSFEventFactory factory = new HSSFEventFactory();
    try {
        factory.processWorkbookEvents(req, fs);
        fail("Shouldn't be able to process protected workbook without the password");
    } catch (EncryptedDocumentException e) {
    }
    // With the password, is properly processed
    Biff8EncryptionKey.setCurrentUserPassword("abc");
    req = new HSSFRequest();
    mockListen = new MockHSSFListener();
    req.addListenerForAllRecords(mockListen);
    factory.processWorkbookEvents(req, fs);
    // Check we got the sheet and the contents
    Record[] recs = mockListen.getRecords();
    assertTrue(recs.length > 50);
    // Has one sheet, with values 1,2,3 in column A rows 1-3
    boolean hasSheet = false, hasA1 = false, hasA2 = false, hasA3 = false;
    for (Record r : recs) {
        if (r instanceof BoundSheetRecord) {
            BoundSheetRecord bsr = (BoundSheetRecord) r;
            assertEquals("Sheet1", bsr.getSheetname());
            hasSheet = true;
        }
        if (r instanceof NumberRecord) {
            NumberRecord nr = (NumberRecord) r;
            if (nr.getColumn() == 0 && nr.getRow() == 0) {
                assertEquals(1, (int) nr.getValue());
                hasA1 = true;
            }
            if (nr.getColumn() == 0 && nr.getRow() == 1) {
                assertEquals(2, (int) nr.getValue());
                hasA2 = true;
            }
            if (nr.getColumn() == 0 && nr.getRow() == 2) {
                assertEquals(3, (int) nr.getValue());
                hasA3 = true;
            }
        }
    }
    assertTrue("Sheet record not found", hasSheet);
    assertTrue("Numeric record for A1 not found", hasA1);
    assertTrue("Numeric record for A2 not found", hasA2);
    assertTrue("Numeric record for A3 not found", hasA3);
}
Also used : EncryptedDocumentException(org.apache.poi.EncryptedDocumentException) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) Record(org.apache.poi.hssf.record.Record) NumberRecord(org.apache.poi.hssf.record.NumberRecord) EOFRecord(org.apache.poi.hssf.record.EOFRecord) FeatHdrRecord(org.apache.poi.hssf.record.FeatHdrRecord) SelectionRecord(org.apache.poi.hssf.record.SelectionRecord) ContinueRecord(org.apache.poi.hssf.record.ContinueRecord) BoundSheetRecord(org.apache.poi.hssf.record.BoundSheetRecord) WindowTwoRecord(org.apache.poi.hssf.record.WindowTwoRecord) DVALRecord(org.apache.poi.hssf.record.DVALRecord) DVRecord(org.apache.poi.hssf.record.DVRecord) NumberRecord(org.apache.poi.hssf.record.NumberRecord) BoundSheetRecord(org.apache.poi.hssf.record.BoundSheetRecord)

Example 20 with EncryptedDocumentException

use of org.apache.poi.EncryptedDocumentException in project poi by apache.

the class OldExcelExtractor method getText.

/**
     * Retrieves the text contents of the file, as best we can
     *  for these old file formats
     * 
     * @return the text contents of the file
     */
public String getText() {
    StringBuffer text = new StringBuffer();
    // To track formats and encodings
    CodepageRecord codepage = null;
    // Process each record in turn, looking for interesting ones
    while (ris.hasNextRecord()) {
        int sid = ris.getNextSid();
        ris.nextRecord();
        switch(sid) {
            case FILE_PASS_RECORD_SID:
                throw new EncryptedDocumentException("Encryption not supported for Old Excel files");
            case OldSheetRecord.sid:
                OldSheetRecord shr = new OldSheetRecord(ris);
                shr.setCodePage(codepage);
                text.append("Sheet: ");
                text.append(shr.getSheetname());
                text.append('\n');
                break;
            case OldLabelRecord.biff2_sid:
            case OldLabelRecord.biff345_sid:
                OldLabelRecord lr = new OldLabelRecord(ris);
                lr.setCodePage(codepage);
                text.append(lr.getValue());
                text.append('\n');
                break;
            case OldStringRecord.biff2_sid:
            case OldStringRecord.biff345_sid:
                OldStringRecord sr = new OldStringRecord(ris);
                sr.setCodePage(codepage);
                text.append(sr.getString());
                text.append('\n');
                break;
            case NumberRecord.sid:
                NumberRecord nr = new NumberRecord(ris);
                handleNumericCell(text, nr.getValue());
                break;
            case OldFormulaRecord.biff2_sid:
            case OldFormulaRecord.biff3_sid:
            case OldFormulaRecord.biff4_sid:
                // Biff 2 and 5+ share the same SID, due to a bug...
                if (biffVersion == 5) {
                    FormulaRecord fr = new FormulaRecord(ris);
                    if (fr.getCachedResultType() == CellType.NUMERIC.getCode()) {
                        handleNumericCell(text, fr.getValue());
                    }
                } else {
                    OldFormulaRecord fr = new OldFormulaRecord(ris);
                    if (fr.getCachedResultType() == CellType.NUMERIC.getCode()) {
                        handleNumericCell(text, fr.getValue());
                    }
                }
                break;
            case RKRecord.sid:
                RKRecord rr = new RKRecord(ris);
                handleNumericCell(text, rr.getRKNumber());
                break;
            case CodepageRecord.sid:
                codepage = new CodepageRecord(ris);
                break;
            default:
                ris.readFully(new byte[ris.remaining()]);
        }
    }
    close();
    ris = null;
    return text.toString();
}
Also used : EncryptedDocumentException(org.apache.poi.EncryptedDocumentException) OldFormulaRecord(org.apache.poi.hssf.record.OldFormulaRecord) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) OldSheetRecord(org.apache.poi.hssf.record.OldSheetRecord) OldLabelRecord(org.apache.poi.hssf.record.OldLabelRecord) RKRecord(org.apache.poi.hssf.record.RKRecord) CodepageRecord(org.apache.poi.hssf.record.CodepageRecord) OldStringRecord(org.apache.poi.hssf.record.OldStringRecord) OldFormulaRecord(org.apache.poi.hssf.record.OldFormulaRecord) NumberRecord(org.apache.poi.hssf.record.NumberRecord)

Aggregations

EncryptedDocumentException (org.apache.poi.EncryptedDocumentException)33 GeneralSecurityException (java.security.GeneralSecurityException)16 Cipher (javax.crypto.Cipher)10 SecretKey (javax.crypto.SecretKey)9 MessageDigest (java.security.MessageDigest)8 IOException (java.io.IOException)6 HashAlgorithm (org.apache.poi.poifs.crypt.HashAlgorithm)4 LittleEndianByteArrayOutputStream (org.apache.poi.util.LittleEndianByteArrayOutputStream)4 Test (org.junit.Test)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 DigestException (java.security.DigestException)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 NavigableMap (java.util.NavigableMap)2 NoSuchElementException (java.util.NoSuchElementException)2 TreeMap (java.util.TreeMap)2 SecretKeySpec (javax.crypto.spec.SecretKeySpec)2 PersistPtrHolder (org.apache.poi.hslf.record.PersistPtrHolder)2 PositionDependentRecord (org.apache.poi.hslf.record.PositionDependentRecord)2