Search in sources :

Example 1 with Decryptor

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

the class XOREncryptionInfoBuilder method initialize.

@Override
public void initialize(EncryptionInfo info, CipherAlgorithm cipherAlgorithm, HashAlgorithm hashAlgorithm, int keyBits, int blockSize, ChainingMode chainingMode) {
    info.setHeader(new XOREncryptionHeader());
    info.setVerifier(new XOREncryptionVerifier());
    Decryptor dec = new XORDecryptor();
    dec.setEncryptionInfo(info);
    info.setDecryptor(dec);
    Encryptor enc = new XOREncryptor();
    enc.setEncryptionInfo(info);
    info.setEncryptor(enc);
}
Also used : Decryptor(org.apache.poi.poifs.crypt.Decryptor) Encryptor(org.apache.poi.poifs.crypt.Encryptor)

Example 2 with Decryptor

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

the class EncryptionUtils method decrypt.

public static InputStream decrypt(final InputStream inputStream, final String pwd) throws Exception {
    try {
        POIFSFileSystem fs = new POIFSFileSystem(inputStream);
        EncryptionInfo info = new EncryptionInfo(fs);
        Decryptor d = Decryptor.getInstance(info);
        if (!d.verifyPassword(pwd)) {
            throw new RuntimeException("incorrect password");
        }
        return d.getDataStream(fs);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
}
Also used : Decryptor(org.apache.poi.poifs.crypt.Decryptor) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) EncryptionInfo(org.apache.poi.poifs.crypt.EncryptionInfo)

Example 3 with Decryptor

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

the class InternalWorkbook method updateEncryptionRecord.

private void updateEncryptionRecord() {
    FilePassRecord fpr = (FilePassRecord) findFirstRecordBySid(FilePassRecord.sid);
    String password = Biff8EncryptionKey.getCurrentUserPassword();
    if (password == null) {
        if (fpr != null) {
            // need to remove password data
            records.remove(fpr);
        }
    } else {
        // create password record
        if (fpr == null) {
            fpr = new FilePassRecord(EncryptionMode.binaryRC4);
            records.add(1, fpr);
        }
        // check if the password has been changed
        EncryptionInfo ei = fpr.getEncryptionInfo();
        byte[] encVer = ei.getVerifier().getEncryptedVerifier();
        try {
            Decryptor dec = ei.getDecryptor();
            Encryptor enc = ei.getEncryptor();
            if (encVer == null || !dec.verifyPassword(password)) {
                enc.confirmPassword(password);
            } else {
                SecretKey sk = dec.getSecretKey();
                ei.getEncryptor().setSecretKey(sk);
            }
        } catch (GeneralSecurityException e) {
            throw new EncryptedDocumentException("can't validate/update encryption setting", e);
        }
    }
}
Also used : FilePassRecord(org.apache.poi.hssf.record.FilePassRecord) SecretKey(javax.crypto.SecretKey) Decryptor(org.apache.poi.poifs.crypt.Decryptor) EncryptedDocumentException(org.apache.poi.EncryptedDocumentException) EncryptionInfo(org.apache.poi.poifs.crypt.EncryptionInfo) GeneralSecurityException(java.security.GeneralSecurityException) Encryptor(org.apache.poi.poifs.crypt.Encryptor) UnicodeString(org.apache.poi.hssf.record.common.UnicodeString)

Example 4 with Decryptor

use of org.apache.poi.poifs.crypt.Decryptor in project data-prep by Talend.

the class StreamingWorkbookReader method init.

// to override https://bz.apache.org/bugzilla/show_bug.cgi?id=57699
public void init(File f) {
    try {
        if (builder.getPassword() != null) {
            // Based on: https://poi.apache.org/encryption.html
            POIFSFileSystem poifs = new POIFSFileSystem(f);
            EncryptionInfo info = new EncryptionInfo(poifs);
            Decryptor d = Decryptor.getInstance(info);
            d.verifyPassword(builder.getPassword());
            pkg = OPCPackage.open(d.getDataStream(poifs));
        } else {
            pkg = OPCPackage.open(f);
        }
        XSSFReader reader = new XSSFReader(pkg);
        ReadOnlySharedStringsTable sst = new ReadOnlySharedStringsTable(pkg);
        StylesTable styles = reader.getStylesTable();
        loadSheets(reader, sst, styles, builder.getRowCacheSize());
    } catch (IOException e) {
        throw new OpenException("Failed to open file", e);
    } catch (OpenXML4JException | XMLStreamException | SAXException e) {
        throw new ReadException("Unable to read workbook", e);
    } catch (GeneralSecurityException e) {
        throw new ReadException("Unable to read workbook - Decryption failed", e);
    }
}
Also used : ReadOnlySharedStringsTable(org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable) Decryptor(org.apache.poi.poifs.crypt.Decryptor) EncryptionInfo(org.apache.poi.poifs.crypt.EncryptionInfo) GeneralSecurityException(java.security.GeneralSecurityException) StylesTable(org.apache.poi.xssf.model.StylesTable) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) ReadException(com.monitorjbl.xlsx.exceptions.ReadException) OpenXML4JException(org.apache.poi.openxml4j.exceptions.OpenXML4JException) XMLStreamException(javax.xml.stream.XMLStreamException) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) OpenException(com.monitorjbl.xlsx.exceptions.OpenException) XSSFReader(org.apache.poi.xssf.eventusermodel.XSSFReader)

Example 5 with Decryptor

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

the class XOREncryptionInfoBuilder method initialize.

@Override
public void initialize(EncryptionInfo info, LittleEndianInput dis) throws IOException {
    info.setHeader(new XOREncryptionHeader());
    info.setVerifier(new XOREncryptionVerifier(dis));
    Decryptor dec = new XORDecryptor();
    dec.setEncryptionInfo(info);
    info.setDecryptor(dec);
    Encryptor enc = new XOREncryptor();
    enc.setEncryptionInfo(info);
    info.setEncryptor(enc);
}
Also used : Decryptor(org.apache.poi.poifs.crypt.Decryptor) Encryptor(org.apache.poi.poifs.crypt.Encryptor)

Aggregations

Decryptor (org.apache.poi.poifs.crypt.Decryptor)10 EncryptionInfo (org.apache.poi.poifs.crypt.EncryptionInfo)8 GeneralSecurityException (java.security.GeneralSecurityException)4 Encryptor (org.apache.poi.poifs.crypt.Encryptor)4 File (java.io.File)3 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)3 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ZipFile (java.util.zip.ZipFile)2 EncryptedDocumentException (org.apache.poi.EncryptedDocumentException)2 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)2 XWPFWordExtractor (org.apache.poi.xwpf.extractor.XWPFWordExtractor)2 XWPFDocument (org.apache.poi.xwpf.usermodel.XWPFDocument)2 OpenException (com.monitorjbl.xlsx.exceptions.OpenException)1 ReadException (com.monitorjbl.xlsx.exceptions.ReadException)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 FilterInputStream (java.io.FilterInputStream)1