Search in sources :

Example 6 with Encryptor

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

Example 7 with Encryptor

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();
}
Also used : Decryptor(org.apache.poi.poifs.crypt.Decryptor) EncryptionInfo(org.apache.poi.poifs.crypt.EncryptionInfo) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Encryptor(org.apache.poi.poifs.crypt.Encryptor) FileInputStream(java.io.FileInputStream) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) TempFile(org.apache.poi.util.TempFile) File(java.io.File) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Aggregations

Encryptor (org.apache.poi.poifs.crypt.Encryptor)7 Decryptor (org.apache.poi.poifs.crypt.Decryptor)4 EncryptionInfo (org.apache.poi.poifs.crypt.EncryptionInfo)4 FileOutputStream (java.io.FileOutputStream)3 File (java.io.File)2 OutputStream (java.io.OutputStream)2 EncryptedDocumentException (org.apache.poi.EncryptedDocumentException)2 FilePassRecord (org.apache.poi.hssf.record.FilePassRecord)2 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)2 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)2 TempFile (org.apache.poi.util.TempFile)2 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 GeneralSecurityException (java.security.GeneralSecurityException)1 NoSuchElementException (java.util.NoSuchElementException)1 SecretKey (javax.crypto.SecretKey)1 EscherBSERecord (org.apache.poi.ddf.EscherBSERecord)1 EscherBlipRecord (org.apache.poi.ddf.EscherBlipRecord)1