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);
}
}
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);
}
}
Aggregations