Search in sources :

Example 1 with EncryptionInfo

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

the class CryptoAPIEncryptor method createEncryptionInfoEntry.

protected void createEncryptionInfoEntry(DirectoryNode dir) throws IOException {
    DataSpaceMapUtils.addDefaultDataSpace(dir);
    final EncryptionInfo info = getEncryptionInfo();
    final CryptoAPIEncryptionHeader header = (CryptoAPIEncryptionHeader) getEncryptionInfo().getHeader();
    final CryptoAPIEncryptionVerifier verifier = (CryptoAPIEncryptionVerifier) getEncryptionInfo().getVerifier();
    EncryptionRecord er = new EncryptionRecord() {

        @Override
        public void write(LittleEndianByteArrayOutputStream bos) {
            bos.writeShort(info.getVersionMajor());
            bos.writeShort(info.getVersionMinor());
            header.write(bos);
            verifier.write(bos);
        }
    };
    DataSpaceMapUtils.createEncryptionEntry(dir, "EncryptionInfo", er);
}
Also used : LittleEndianByteArrayOutputStream(org.apache.poi.util.LittleEndianByteArrayOutputStream) EncryptionInfo(org.apache.poi.poifs.crypt.EncryptionInfo) EncryptionRecord(org.apache.poi.poifs.crypt.standard.EncryptionRecord)

Example 2 with EncryptionInfo

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

the class CryptoAPIDecryptor method initCipherForBlock.

@Override
public Cipher initCipherForBlock(Cipher cipher, int block) throws GeneralSecurityException {
    EncryptionInfo ei = getEncryptionInfo();
    SecretKey sk = getSecretKey();
    return initCipherForBlock(cipher, block, ei, sk, Cipher.DECRYPT_MODE);
}
Also used : SecretKey(javax.crypto.SecretKey) EncryptionInfo(org.apache.poi.poifs.crypt.EncryptionInfo)

Example 3 with EncryptionInfo

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

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

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

Aggregations

EncryptionInfo (org.apache.poi.poifs.crypt.EncryptionInfo)20 Decryptor (org.apache.poi.poifs.crypt.Decryptor)7 Test (org.junit.Test)5 File (java.io.File)4 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)4 Encryptor (org.apache.poi.poifs.crypt.Encryptor)4 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)4 LittleEndianByteArrayOutputStream (org.apache.poi.util.LittleEndianByteArrayOutputStream)4 FileOutputStream (java.io.FileOutputStream)3 InputStream (java.io.InputStream)3 GeneralSecurityException (java.security.GeneralSecurityException)3 SecretKey (javax.crypto.SecretKey)3 EncryptionRecord (org.apache.poi.poifs.crypt.standard.EncryptionRecord)3 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)3 OutputStream (java.io.OutputStream)2 ZipFile (java.util.zip.ZipFile)2 EncryptedDocumentException (org.apache.poi.EncryptedDocumentException)2 TempFile (org.apache.poi.util.TempFile)2 XWPFWordExtractor (org.apache.poi.xwpf.extractor.XWPFWordExtractor)2 XWPFDocument (org.apache.poi.xwpf.usermodel.XWPFDocument)2