Search in sources :

Example 1 with ChunkedCipherOutputStream

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

the class HSLFSlideShowEncrypted method encryptPicture.

protected void encryptPicture(byte[] pictstream, int offset) {
    if (dea == null) {
        return;
    }
    encryptInit();
    // NOSONAR
    LittleEndianByteArrayOutputStream los = new LittleEndianByteArrayOutputStream(pictstream, offset);
    ChunkedCipherOutputStream ccos = null;
    try {
        enc.setChunkSize(-1);
        ccos = enc.getDataStream(los, 0);
        int recInst = fieldRecInst.getValue(LittleEndian.getUShort(pictstream, offset));
        int recType = LittleEndian.getUShort(pictstream, offset + 2);
        final int rlen = (int) LittleEndian.getUInt(pictstream, offset + 4);
        ccos.write(pictstream, offset, 8);
        ccos.flush();
        offset += 8;
        int endOffset = offset + rlen;
        if (recType == 0xF007) {
            // TOOD: get a real example file ... to actual test the FBSE entry
            // not sure where the foDelay block is
            // File BLIP Store Entry (FBSE)
            int cbName = LittleEndian.getUShort(pictstream, offset + 33);
            for (int part : BLIB_STORE_ENTRY_PARTS) {
                ccos.write(pictstream, offset, part);
                ccos.flush();
                offset += part;
            }
            if (cbName > 0) {
                ccos.write(pictstream, offset, cbName);
                ccos.flush();
                offset += cbName;
            }
            if (offset == endOffset) {
                // no embedded blip
                return;
            }
            // fall through, read embedded blip now
            // update header data
            recInst = fieldRecInst.getValue(LittleEndian.getUShort(pictstream, offset));
            recType = LittleEndian.getUShort(pictstream, offset + 2);
            ccos.write(pictstream, offset, 8);
            ccos.flush();
            offset += 8;
        }
        int rgbUidCnt = (recInst == 0x217 || recInst == 0x3D5 || recInst == 0x46B || recInst == 0x543 || recInst == 0x6E1 || recInst == 0x6E3 || recInst == 0x6E5 || recInst == 0x7A9) ? 2 : 1;
        for (int i = 0; i < rgbUidCnt; i++) {
            // rgbUid 1/2
            ccos.write(pictstream, offset, 16);
            ccos.flush();
            offset += 16;
        }
        if (recType == 0xF01A || recType == 0XF01B || recType == 0XF01C) {
            // metafileHeader
            ccos.write(pictstream, offset, 34);
            offset += 34;
            ccos.flush();
        } else {
            // tag
            ccos.write(pictstream, offset, 1);
            offset += 1;
            ccos.flush();
        }
        int blipLen = endOffset - offset;
        ccos.write(pictstream, offset, blipLen);
        ccos.flush();
    } catch (Exception e) {
        throw new EncryptedPowerPointFileException(e);
    } finally {
        IOUtils.closeQuietly(ccos);
        IOUtils.closeQuietly(los);
    }
}
Also used : LittleEndianByteArrayOutputStream(org.apache.poi.util.LittleEndianByteArrayOutputStream) ChunkedCipherOutputStream(org.apache.poi.poifs.crypt.ChunkedCipherOutputStream) 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 2 with ChunkedCipherOutputStream

use of org.apache.poi.poifs.crypt.ChunkedCipherOutputStream 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

IOException (java.io.IOException)2 EncryptedDocumentException (org.apache.poi.EncryptedDocumentException)2 ChunkedCipherOutputStream (org.apache.poi.poifs.crypt.ChunkedCipherOutputStream)2 LittleEndianByteArrayOutputStream (org.apache.poi.util.LittleEndianByteArrayOutputStream)2 FileNotFoundException (java.io.FileNotFoundException)1 GeneralSecurityException (java.security.GeneralSecurityException)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 ExtendedFormatRecord (org.apache.poi.hssf.record.ExtendedFormatRecord)1 FilePassRecord (org.apache.poi.hssf.record.FilePassRecord)1 FontRecord (org.apache.poi.hssf.record.FontRecord)1