Search in sources :

Example 6 with LittleEndianByteArrayInputStream

use of org.apache.poi.util.LittleEndianByteArrayInputStream in project poi by apache.

the class HSLFSlideShowEncrypted method decryptRecord.

protected void decryptRecord(byte[] docstream, int persistId, int offset) {
    if (dea == null) {
        return;
    }
    decryptInit();
    dec.setChunkSize(-1);
    // NOSONAR
    LittleEndianByteArrayInputStream lei = new LittleEndianByteArrayInputStream(docstream, offset);
    ChunkedCipherInputStream ccis = null;
    try {
        ccis = dec.getDataStream(lei, docstream.length - offset, 0);
        ccis.initCipherForBlock(persistId);
        // decrypt header and read length to be decrypted
        readFully(ccis, docstream, offset, 8);
        // decrypt the rest of the record
        int rlen = (int) LittleEndian.getUInt(docstream, offset + 4);
        readFully(ccis, docstream, offset + 8, rlen);
    } catch (Exception e) {
        throw new EncryptedPowerPointFileException(e);
    } finally {
        IOUtils.closeQuietly(ccis);
        IOUtils.closeQuietly(lei);
    }
}
Also used : LittleEndianByteArrayInputStream(org.apache.poi.util.LittleEndianByteArrayInputStream) ChunkedCipherInputStream(org.apache.poi.poifs.crypt.ChunkedCipherInputStream) EncryptedPowerPointFileException(org.apache.poi.hslf.exceptions.EncryptedPowerPointFileException) EncryptedPowerPointFileException(org.apache.poi.hslf.exceptions.EncryptedPowerPointFileException) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) CorruptPowerPointFileException(org.apache.poi.hslf.exceptions.CorruptPowerPointFileException) EncryptedDocumentException(org.apache.poi.EncryptedDocumentException)

Example 7 with LittleEndianByteArrayInputStream

use of org.apache.poi.util.LittleEndianByteArrayInputStream in project poi by apache.

the class TestVariantSupport method test52337.

@Test
public void test52337() throws Exception {
    // document summary stream   from test1-excel.doc attached to Bugzilla 52337
    String documentSummaryEnc = "H4sIAAAAAAAAAG2RsUvDQBjFXxsraiuNKDoI8ZwclIJOjhYCGpQitINbzXChgTQtyQ3+Hw52cHB0E" + "kdHRxfBpeAf4H/g5KK+M59Firn8eNx3d+++x31+AZVSGdOfrZTHz+Prxrp7eTWH7Z2PO5+1ylTtrA" + "SskBrXKOiROhnavWREZskNWSK3ZI3ckyp5IC55JMvkiaySF7JIXlF4v0tPbzOAR1XE18MwM32dGjW" + "IVJAanaVhoppRFMZZDjjSgyO9WT10Cq1vVX/uh/Txn3pucc7m6fTiXPEPldG5Qc0t2vEkXic2iZ5c" + "JDkd8VFS3pcMBzIvS7buaeB3j06C1nF7krFJPRdz62M4rM7/8f3NtyE+LQyQoY8QCfbQwAU1l/UF0" + "ubraA6DXWzC5x7gG6xzLtsAAgAA";
    byte[] bytes = RawDataUtil.decompress(documentSummaryEnc);
    PropertySet ps = PropertySetFactory.create(new ByteArrayInputStream(bytes));
    DocumentSummaryInformation dsi = (DocumentSummaryInformation) ps;
    Section s = dsi.getSections().get(0);
    Object hdrs = s.getProperty(PropertyIDMap.PID_HEADINGPAIR);
    assertNotNull(hdrs);
    assertEquals(byte[].class, hdrs.getClass());
    // parse the value
    Vector v = new Vector((short) Variant.VT_VARIANT);
    LittleEndianByteArrayInputStream lei = new LittleEndianByteArrayInputStream((byte[]) hdrs, 0);
    v.read(lei);
    TypedPropertyValue[] items = v.getValues();
    assertEquals(2, items.length);
    Object cp = items[0].getValue();
    assertNotNull(cp);
    assertEquals(CodePageString.class, cp.getClass());
    Object i = items[1].getValue();
    assertNotNull(i);
    assertEquals(Integer.class, i.getClass());
    assertEquals(1, i);
}
Also used : LittleEndianByteArrayInputStream(org.apache.poi.util.LittleEndianByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) LittleEndianByteArrayInputStream(org.apache.poi.util.LittleEndianByteArrayInputStream) Test(org.junit.Test)

Example 8 with LittleEndianByteArrayInputStream

use of org.apache.poi.util.LittleEndianByteArrayInputStream in project poi by apache.

the class HSSFWorkbook method encryptBytes.

@SuppressWarnings("resource")
protected void encryptBytes(byte[] buf) {
    int initialOffset = 0;
    FilePassRecord fpr = null;
    for (Record r : workbook.getRecords()) {
        initialOffset += r.getRecordSize();
        if (r instanceof FilePassRecord) {
            fpr = (FilePassRecord) r;
            break;
        }
    }
    if (fpr == null) {
        return;
    }
    // NOSONAR
    LittleEndianByteArrayInputStream plain = new LittleEndianByteArrayInputStream(buf, 0);
    // NOSONAR
    LittleEndianByteArrayOutputStream leos = new LittleEndianByteArrayOutputStream(buf, 0);
    Encryptor enc = fpr.getEncryptionInfo().getEncryptor();
    enc.setChunkSize(Biff8DecryptingStream.RC4_REKEYING_INTERVAL);
    byte[] tmp = new byte[1024];
    try {
        ChunkedCipherOutputStream os = enc.getDataStream(leos, initialOffset);
        int totalBytes = 0;
        while (totalBytes < buf.length) {
            plain.read(tmp, 0, 4);
            final int sid = LittleEndian.getUShort(tmp, 0);
            final int len = LittleEndian.getUShort(tmp, 2);
            boolean isPlain = Biff8DecryptingStream.isNeverEncryptedRecord(sid);
            os.setNextRecordSize(len, isPlain);
            os.writePlain(tmp, 0, 4);
            if (sid == BoundSheetRecord.sid) {
                // special case for the field_1_position_of_BOF (=lbPlyPos) field of
                // the BoundSheet8 record which must be unencrypted
                byte[] bsrBuf = new byte[len];
                plain.readFully(bsrBuf);
                os.writePlain(bsrBuf, 0, 4);
                os.write(bsrBuf, 4, len - 4);
            } else {
                int todo = len;
                while (todo > 0) {
                    int nextLen = Math.min(todo, tmp.length);
                    plain.readFully(tmp, 0, nextLen);
                    if (isPlain) {
                        os.writePlain(tmp, 0, nextLen);
                    } else {
                        os.write(tmp, 0, nextLen);
                    }
                    todo -= nextLen;
                }
            }
            totalBytes += 4 + len;
        }
        os.close();
    } catch (Exception e) {
        throw new EncryptedDocumentException(e);
    }
}
Also used : FilePassRecord(org.apache.poi.hssf.record.FilePassRecord) LittleEndianByteArrayOutputStream(org.apache.poi.util.LittleEndianByteArrayOutputStream) ChunkedCipherOutputStream(org.apache.poi.poifs.crypt.ChunkedCipherOutputStream) EncryptedDocumentException(org.apache.poi.EncryptedDocumentException) LittleEndianByteArrayInputStream(org.apache.poi.util.LittleEndianByteArrayInputStream) UnknownRecord(org.apache.poi.hssf.record.UnknownRecord) RecalcIdRecord(org.apache.poi.hssf.record.RecalcIdRecord) EscherBSERecord(org.apache.poi.ddf.EscherBSERecord) Record(org.apache.poi.hssf.record.Record) AbstractEscherHolderRecord(org.apache.poi.hssf.record.AbstractEscherHolderRecord) BoundSheetRecord(org.apache.poi.hssf.record.BoundSheetRecord) EscherBlipRecord(org.apache.poi.ddf.EscherBlipRecord) DrawingGroupRecord(org.apache.poi.hssf.record.DrawingGroupRecord) BackupRecord(org.apache.poi.hssf.record.BackupRecord) EscherRecord(org.apache.poi.ddf.EscherRecord) NameRecord(org.apache.poi.hssf.record.NameRecord) LabelSSTRecord(org.apache.poi.hssf.record.LabelSSTRecord) LabelRecord(org.apache.poi.hssf.record.LabelRecord) FilePassRecord(org.apache.poi.hssf.record.FilePassRecord) FontRecord(org.apache.poi.hssf.record.FontRecord) SSTRecord(org.apache.poi.hssf.record.SSTRecord) ExtendedFormatRecord(org.apache.poi.hssf.record.ExtendedFormatRecord) Encryptor(org.apache.poi.poifs.crypt.Encryptor) FileNotFoundException(java.io.FileNotFoundException) OldExcelFormatException(org.apache.poi.hssf.OldExcelFormatException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException) EncryptedDocumentException(org.apache.poi.EncryptedDocumentException)

Aggregations

LittleEndianByteArrayInputStream (org.apache.poi.util.LittleEndianByteArrayInputStream)8 Test (org.junit.Test)4 IOException (java.io.IOException)2 EncryptedDocumentException (org.apache.poi.EncryptedDocumentException)2 ChunkedCipherInputStream (org.apache.poi.poifs.crypt.ChunkedCipherInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 ArrayList (java.util.ArrayList)1 NoSuchElementException (java.util.NoSuchElementException)1 EscherBSERecord (org.apache.poi.ddf.EscherBSERecord)1 EscherBlipRecord (org.apache.poi.ddf.EscherBlipRecord)1 EscherRecord (org.apache.poi.ddf.EscherRecord)1 CorruptPowerPointFileException (org.apache.poi.hslf.exceptions.CorruptPowerPointFileException)1 EncryptedPowerPointFileException (org.apache.poi.hslf.exceptions.EncryptedPowerPointFileException)1 OldExcelFormatException (org.apache.poi.hssf.OldExcelFormatException)1 AbstractEscherHolderRecord (org.apache.poi.hssf.record.AbstractEscherHolderRecord)1 BackupRecord (org.apache.poi.hssf.record.BackupRecord)1 BoundSheetRecord (org.apache.poi.hssf.record.BoundSheetRecord)1 DrawingGroupRecord (org.apache.poi.hssf.record.DrawingGroupRecord)1