Search in sources :

Example 1 with Encryptor

use of org.apache.poi.poifs.crypt.Encryptor 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 Encryptor

use of org.apache.poi.poifs.crypt.Encryptor 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 3 with Encryptor

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

the class TestXSLFSlideShowFactory method createProtected.

private static File createProtected(String basefile, String password) throws IOException, GeneralSecurityException {
    NPOIFSFileSystem fs = new NPOIFSFileSystem();
    EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile);
    Encryptor enc = info.getEncryptor();
    enc.confirmPassword(password);
    InputStream fis = _slTests.openResourceAsStream(basefile);
    OutputStream os = enc.getDataStream(fs);
    IOUtils.copy(fis, os);
    os.close();
    fis.close();
    File tf = TempFile.createTempFile("test-xslf-slidefactory", ".pptx");
    FileOutputStream fos = new FileOutputStream(tf);
    fs.writeFilesystem(fos);
    fos.close();
    fs.close();
    return tf;
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) EncryptionInfo(org.apache.poi.poifs.crypt.EncryptionInfo) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) Encryptor(org.apache.poi.poifs.crypt.Encryptor) TempFile(org.apache.poi.util.TempFile) File(java.io.File)

Example 4 with Encryptor

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

the class SavePasswordProtectedXlsx method save.

public static void save(final InputStream inputStream, final String filename, final String pwd) throws InvalidFormatException, IOException, GeneralSecurityException {
    POIFSFileSystem fs = null;
    FileOutputStream fos = null;
    OPCPackage opc = null;
    try {
        fs = new POIFSFileSystem();
        EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile);
        Encryptor enc = Encryptor.getInstance(info);
        enc.confirmPassword(pwd);
        opc = OPCPackage.open(inputStream);
        fos = new FileOutputStream(filename);
        opc.save(enc.getDataStream(fs));
        fs.writeFilesystem(fos);
    } finally {
        IOUtils.closeQuietly(fos);
        IOUtils.closeQuietly(opc);
        IOUtils.closeQuietly(fs);
        IOUtils.closeQuietly(inputStream);
    }
}
Also used : POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) EncryptionInfo(org.apache.poi.poifs.crypt.EncryptionInfo) FileOutputStream(java.io.FileOutputStream) Encryptor(org.apache.poi.poifs.crypt.Encryptor) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage)

Example 5 with Encryptor

use of org.apache.poi.poifs.crypt.Encryptor 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

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