Search in sources :

Example 16 with EncryptionInfo

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

the class TestXWPFBugs method bug53475NoCSPName.

/**
     * A word document that's encrypted with non-standard
     * Encryption options, and no cspname section. See bug 53475
     */
@Test
public void bug53475NoCSPName() throws Exception {
    File file = POIDataSamples.getDocumentInstance().getFile("bug53475-password-is-solrcell.docx");
    NPOIFSFileSystem filesystem = new NPOIFSFileSystem(file, true);
    // Check the encryption details
    EncryptionInfo info = new EncryptionInfo(filesystem);
    assertEquals(128, info.getHeader().getKeySize());
    assertEquals(CipherAlgorithm.aes128, info.getHeader().getCipherAlgorithm());
    assertEquals(HashAlgorithm.sha1, info.getHeader().getHashAlgorithmEx());
    // Check it can be decoded
    Decryptor d = Decryptor.getInstance(info);
    assertTrue("Unable to process: document is encrypted", d.verifyPassword("solrcell"));
    // Check we can read the word document in that
    InputStream dataStream = d.getDataStream(filesystem);
    OPCPackage opc = OPCPackage.open(dataStream);
    XWPFDocument doc = new XWPFDocument(opc);
    XWPFWordExtractor ex = new XWPFWordExtractor(doc);
    String text = ex.getText();
    assertNotNull(text);
    assertEquals("This is password protected Word document.", text.trim());
    ex.close();
    filesystem.close();
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) Decryptor(org.apache.poi.poifs.crypt.Decryptor) EncryptionInfo(org.apache.poi.poifs.crypt.EncryptionInfo) InputStream(java.io.InputStream) XWPFWordExtractor(org.apache.poi.xwpf.extractor.XWPFWordExtractor) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) File(java.io.File) ZipFile(java.util.zip.ZipFile) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Example 17 with EncryptionInfo

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

the class TestXWPFBugs method bug53475_aes256.

/**
     * A word document with aes-256, i.e. aes is always 128 bit (= 128 bit block size),
     * but the key can be 128/192/256 bits
     */
@Test
public void bug53475_aes256() throws Exception {
    int maxKeyLen = Cipher.getMaxAllowedKeyLength("AES");
    Assume.assumeTrue("Please install JCE Unlimited Strength Jurisdiction Policy files for AES 256", maxKeyLen == 2147483647);
    File file = POIDataSamples.getDocumentInstance().getFile("bug53475-password-is-pass.docx");
    NPOIFSFileSystem filesystem = new NPOIFSFileSystem(file, true);
    // Check the encryption details
    EncryptionInfo info = new EncryptionInfo(filesystem);
    assertEquals(16, info.getHeader().getBlockSize());
    assertEquals(256, info.getHeader().getKeySize());
    assertEquals(CipherAlgorithm.aes256, info.getHeader().getCipherAlgorithm());
    assertEquals(HashAlgorithm.sha1, info.getHeader().getHashAlgorithmEx());
    // Check it can be decoded
    Decryptor d = Decryptor.getInstance(info);
    assertTrue("Unable to process: document is encrypted", d.verifyPassword("pass"));
    // Check we can read the word document in that
    InputStream dataStream = d.getDataStream(filesystem);
    OPCPackage opc = OPCPackage.open(dataStream);
    XWPFDocument doc = new XWPFDocument(opc);
    XWPFWordExtractor ex = new XWPFWordExtractor(doc);
    String text = ex.getText();
    assertNotNull(text);
    // I know ... a stupid typo, maybe next time ...
    assertEquals("The is a password protected document.", text.trim());
    ex.close();
    filesystem.close();
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) Decryptor(org.apache.poi.poifs.crypt.Decryptor) EncryptionInfo(org.apache.poi.poifs.crypt.EncryptionInfo) InputStream(java.io.InputStream) XWPFWordExtractor(org.apache.poi.xwpf.extractor.XWPFWordExtractor) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) File(java.io.File) ZipFile(java.util.zip.ZipFile) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Example 18 with EncryptionInfo

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

the class TestBinaryRC4 method createKeyDigest.

@Test
public void createKeyDigest() throws GeneralSecurityException {
    byte[] docIdData = readFromString("17 F6 D1 6B 09 B1 5F 7B 4C 9D 03 B4 81 B5 B4 4A");
    byte[] expResult = readFromString("C2 D9 56 B2 6B");
    EncryptionInfo ei = new EncryptionInfo(EncryptionMode.binaryRC4);
    BinaryRC4EncryptionVerifier ver = (BinaryRC4EncryptionVerifier) ei.getVerifier();
    ver.setSalt(docIdData);
    SecretKey sk = BinaryRC4Decryptor.generateSecretKey("MoneyForNothing", ver);
    assertArrayEquals("keyDigest mismatch", expResult, sk.getEncoded());
}
Also used : SecretKey(javax.crypto.SecretKey) EncryptionInfo(org.apache.poi.poifs.crypt.EncryptionInfo) Test(org.junit.Test)

Example 19 with EncryptionInfo

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

the class TestWorkbookProtection method testEncryptDecrypt.

@Test
public void testEncryptDecrypt() throws Exception {
    final String password = "abc123";
    final String sheetName = "TestSheet1";
    final String cellValue = "customZipEntrySource";
    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet1 = workbook.createSheet(sheetName);
    XSSFRow row1 = sheet1.createRow(1);
    XSSFCell cell1 = row1.createCell(1);
    cell1.setCellValue(cellValue);
    File tf1 = TempFile.createTempFile("poitest", ".xlsx");
    FileOutputStream fos1 = new FileOutputStream(tf1);
    workbook.write(fos1);
    IOUtils.closeQuietly(fos1);
    POIFSFileSystem poiFileSystem = new POIFSFileSystem();
    EncryptionInfo encryptionInfo = new EncryptionInfo(EncryptionMode.agile);
    Encryptor enc = encryptionInfo.getEncryptor();
    enc.confirmPassword(password);
    FileInputStream fis = new FileInputStream(tf1);
    OPCPackage opc = OPCPackage.open(fis);
    IOUtils.closeQuietly(fis);
    try {
        OutputStream os = enc.getDataStream(poiFileSystem);
        opc.save(os);
        IOUtils.closeQuietly(os);
    } finally {
        IOUtils.closeQuietly(opc);
    }
    tf1.delete();
    FileOutputStream fos2 = new FileOutputStream(tf1);
    poiFileSystem.writeFilesystem(fos2);
    IOUtils.closeQuietly(fos2);
    workbook.close();
    fis = new FileInputStream(tf1);
    POIFSFileSystem poiFileSystem2 = new POIFSFileSystem(fis);
    IOUtils.closeQuietly(fis);
    EncryptionInfo encryptionInfo2 = new EncryptionInfo(poiFileSystem2);
    Decryptor decryptor = encryptionInfo2.getDecryptor();
    decryptor.verifyPassword(password);
    XSSFWorkbook workbook2 = new XSSFWorkbook(decryptor.getDataStream(poiFileSystem2));
    workbook2.close();
    tf1.delete();
}
Also used : Decryptor(org.apache.poi.poifs.crypt.Decryptor) EncryptionInfo(org.apache.poi.poifs.crypt.EncryptionInfo) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Encryptor(org.apache.poi.poifs.crypt.Encryptor) FileInputStream(java.io.FileInputStream) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) TempFile(org.apache.poi.util.TempFile) File(java.io.File) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Example 20 with EncryptionInfo

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

the class BinaryRC4Encryptor method createEncryptionInfoEntry.

protected void createEncryptionInfoEntry(DirectoryNode dir) throws IOException {
    DataSpaceMapUtils.addDefaultDataSpace(dir);
    final EncryptionInfo info = getEncryptionInfo();
    final BinaryRC4EncryptionHeader header = (BinaryRC4EncryptionHeader) info.getHeader();
    final BinaryRC4EncryptionVerifier verifier = (BinaryRC4EncryptionVerifier) info.getVerifier();
    EncryptionRecord er = new EncryptionRecord() {

        @Override
        public void write(LittleEndianByteArrayOutputStream bos) {
            bos.writeShort(info.getVersionMajor());
            bos.writeShort(info.getVersionMinor());
            header.write(bos);
            verifier.write(bos);
        }
    };
    DataSpaceMapUtils.createEncryptionEntry(dir, "EncryptionInfo", er);
}
Also used : LittleEndianByteArrayOutputStream(org.apache.poi.util.LittleEndianByteArrayOutputStream) EncryptionInfo(org.apache.poi.poifs.crypt.EncryptionInfo) EncryptionRecord(org.apache.poi.poifs.crypt.standard.EncryptionRecord)

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