public OutputStream getDataStream(POIFSFileSystem fs) throws IOException, GeneralSecurityException { return getDataStream(fs.getRoot()); }
@Override public BinaryRC4Encryptor clone() throws CloneNotSupportedException { return (BinaryRC4Encryptor)super.clone(); }
final byte xorArray[] = getEncryptionInfo().getEncryptor().getSecretKey().getEncoded(); final byte chunk[] = getChunk(); final byte plain[] = (plainBytes.isEmpty()) ? null : chunk.clone();
@Override public EncryptionInfo clone() throws CloneNotSupportedException { EncryptionInfo other = (EncryptionInfo)super.clone(); other.header = header.clone(); other.verifier = verifier.clone(); other.decryptor = decryptor.clone(); other.decryptor.setEncryptionInfo(other); other.encryptor = encryptor.clone(); other.encryptor.setEncryptionInfo(other); return other; } }
// create a new POIFSFileSystem, which is the container for // encrypted OOXML-based files POIFSFileSystem fs = new POIFSFileSystem(); EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile); // setup the encryption Encryptor enc = info.getEncryptor(); enc.confirmPassword("foobaa"); // after writing to the SXSSF workbook you need the content // as a stream InputStream is = <open the SXSSF workbook as stream> OutputStream os = enc.getDataStream(fs); // copy the stream to the OutputStream IOUtils.copy(is, os); // Write out the encrypted version FileOutputStream fos = new FileOutputStream("..."); fs.writeFilesystem(fos); fos.close();
public static void save(final InputStream inputStream, final String filename, final String pwd) throws InvalidFormatException, IOException, GeneralSecurityException { try (POIFSFileSystem fs = new POIFSFileSystem(); OPCPackage opc = OPCPackage.open(inputStream); FileOutputStream fos = new FileOutputStream(filename)) { EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile); Encryptor enc = Encryptor.getInstance(info); enc.confirmPassword(pwd); opc.save(enc.getDataStream(fs)); fs.writeFilesystem(fos); } finally { IOUtils.closeQuietly(inputStream); } }
LittleEndianByteArrayInputStream plain = new LittleEndianByteArrayInputStream(buf, 0); // NOSONAR LittleEndianByteArrayOutputStream leos = new LittleEndianByteArrayOutputStream(buf, 0); // NOSONAR 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) {
try { if (encVer == null || !dec.verifyPassword(password)) { enc.confirmPassword(password); } else { byte verifier[] = dec.getVerifier(); byte salt[] = ver.getSalt(); enc.confirmPassword(password, null, null, verifier, salt, null);
@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); }
POIFSFileSystem fs = new POIFSFileSystem(); EncryptionInfo info = new EncryptionInfo(fs, EncryptionMode.agile); Encryptor enc = info.getEncryptor(); enc.confirmPassword("xxxxx"); OPCPackage opc = OPCPackage.open(new File("c:/test/sample.xlsx"),PackageAccess.READ_WRITE); OutputStream os = enc.getDataStream(fs); opc.save(os); opc.close(); FileOutputStream fos = new FileOutputStream("c:/test/sample.xlsx"); fs.writeFilesystem(fos); fos.close();
private void encryptBytes(byte[] plain, int encryptOffset, OutputStream bos) throws IOException { try { EncryptionInfo ei = getEncryptionInfo(); Encryptor enc = ei.getEncryptor(); enc.setChunkSize(RC4_REKEYING_INTERVAL); ChunkedCipherOutputStream os = enc.getDataStream(bos, 0); if (encryptOffset > 0) { os.writePlain(plain, 0, encryptOffset); } os.write(plain, encryptOffset, plain.length-encryptOffset); os.close(); } catch (GeneralSecurityException e) { throw new IOException(e); } }
POIFSFileSystem fs = new POIFSFileSystem(); EncryptionInfo info = new EncryptionInfo(fs, EncryptionMode.agile); Encryptor enc = info.getEncryptor(); enc.confirmPassword(<your_password>); OPCPackage opc = OPCPackage.open(new File(<file_path>), PackageAccess.READ_WRITE); //opening package for encryption OutputStream os = enc.getDataStream(fs); //perform encryption opc.save(os); //save package opc.close(); FileOutputStream fos = new FileOutputStream("file_path"); fs.writeFilesystem(fos); //write the file back to file system fos.close();
@Override public EncryptionInfo clone() throws CloneNotSupportedException { EncryptionInfo other = (EncryptionInfo)super.clone(); other.header = header.clone(); other.verifier = verifier.clone(); other.decryptor = decryptor.clone(); other.decryptor.setEncryptionInfo(other); other.encryptor = encryptor.clone(); other.encryptor.setEncryptionInfo(other); return other; } }
@Override public void initialize(EncryptionInfo info, CipherAlgorithm cipherAlgorithm, HashAlgorithm hashAlgorithm, int keyBits, int blockSize, ChainingMode chainingMode) { info.setHeader(new BinaryRC4EncryptionHeader()); info.setVerifier(new BinaryRC4EncryptionVerifier()); Decryptor dec = new BinaryRC4Decryptor(); dec.setEncryptionInfo(info); info.setDecryptor(dec); Encryptor enc = new BinaryRC4Encryptor(); enc.setEncryptionInfo(info); info.setEncryptor(enc); } }
// Prepare POIFSFileSystem fs = new POIFSFileSystem(); EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile); CipherAlgorithm.aes192, HashAlgorithm.sha384, -1, -1, null); Encryptor enc = info.getEncryptor(); enc.confirmPassword("foobaa"); // Create the normal workbook Workbook wb = new XSSFWorkbook(); Sheet s = wb.createSheet(); // TODO Populate // Encrypt OutputStream os = enc.getDataStream(fs); wb.save(os); opc.close(); // Save FileOutputStream fos = new FileOutputStream("protected.xlsx"); fs.writeFilesystem(fos); fos.close();
protected OutputStream encryptRecord(OutputStream plainStream, int persistId, Record record) { boolean isPlain = (dea == null || record instanceof UserEditAtom || record instanceof PersistPtrHolder || record instanceof DocumentEncryptionAtom ); try { if (isPlain) { if (cyos != null) { // write cached data to stream cyos.flush(); } return plainStream; } if (cyos == null) { Encryptor enc = getEncryptionInfo().getEncryptor(); enc.setChunkSize(-1); cyos = enc.getDataStream(plainStream, 0); } cyos.initCipherForBlock(persistId, false); } catch (Exception e) { throw new EncryptedPowerPointFileException(e); } return cyos; }
protected Record[] updateEncryptionRecord(Record records[]) { String password = Biff8EncryptionKey.getCurrentUserPassword(); if (password == null) { if (dea == null) { // no password given, no encryption record exits -> done return records; } else { // need to remove password data dea = null; return removeEncryptionRecord(records); } } else { // create password record if (dea == null) { dea = new DocumentEncryptionAtom(); } EncryptionInfo ei = dea.getEncryptionInfo(); byte salt[] = ei.getVerifier().getSalt(); Encryptor enc = getEncryptionInfo().getEncryptor(); if (salt == null) { enc.confirmPassword(password); } else { byte verifier[] = ei.getDecryptor().getVerifier(); enc.confirmPassword(password, null, null, verifier, salt, null); } // move EncryptionRecord to last slide position records = normalizeRecords(records); return addEncryptionRecord(records, dea); } }
@Override public CryptoAPIEncryptor clone() throws CloneNotSupportedException { return (CryptoAPIEncryptor)super.clone(); }
public OutputStream getDataStream(POIFSFileSystem fs) throws IOException, GeneralSecurityException { return getDataStream(fs.getRoot()); }
@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); } }