use of org.apache.poi.poifs.crypt.Encryptor 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);
}
}
use of org.apache.poi.poifs.crypt.Encryptor 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();
}
Aggregations