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