@Override public void close() throws IOException { if (isClosed) { LOG.log(POILogger.DEBUG, "ChunkedCipherOutputStream was already closed - ignoring"); return; } isClosed = true; try { writeChunk(false); super.close(); if (fileOut != null) { int oleStreamSize = (int)(fileOut.length()+LittleEndianConsts.LONG_SIZE); calculateChecksum(fileOut, (int)pos); dir.createDocument(DEFAULT_POIFS_ENTRY, oleStreamSize, new EncryptedPackageWriter()); createEncryptionInfoEntry(dir, fileOut); } } catch (GeneralSecurityException e) { throw new IOException(e); } }
@Override public void flush() throws IOException { writeChunk(false); super.flush(); } }
int posInChunk = (int)(pos & getChunkMask()); cipher = initCipherForBlock(cipher, index, lastChunk); ciLen = invokeCipher(posInChunk, doFinal); } catch (GeneralSecurityException e) { throw new IOException("can't re-/initialize cipher", e);
protected void write(byte[] b, int off, int len, boolean writePlain) throws IOException { if (len == 0) { return; } if (len < 0 || b.length < off+len) { throw new IOException("not enough bytes in your input buffer"); } final int chunkMask = getChunkMask(); while (len > 0) { int posInChunk = (int)(pos & chunkMask); int nextLen = Math.min(chunk.length-posInChunk, len); System.arraycopy(b, off, chunk, posInChunk, nextLen); if (writePlain) { plainByteFlags.set(posInChunk, posInChunk+nextLen); } pos += nextLen; totalPos += nextLen; off += nextLen; len -= nextLen; if ((pos & chunkMask) == 0) { writeChunk(len > 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) { os.writePlain(bsrBuf, 0, 4); os.write(bsrBuf, 4, len-4); } else { int todo = len; plain.readFully(tmp, 0, nextLen); if (isPlain) { os.writePlain(tmp, 0, nextLen); } else { os.write(tmp, 0, nextLen); os.close(); } catch (Exception e) { throw new EncryptedDocumentException(e);
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); } }
@Override public void write(byte[] b) throws IOException { write(b, 0, b.length); }
protected abstract Cipher initCipherForBlock(Cipher existing, int block, boolean lastChunk) throws IOException, GeneralSecurityException;
ccos.write(pictstream, offset, 8); ccos.flush(); offset += 8; int endOffset = offset + rlen; ccos.write(pictstream, offset, part); ccos.flush(); offset += part; ccos.write(pictstream, offset, cbName); ccos.flush(); offset += cbName; ccos.write(pictstream, offset, 8); ccos.flush(); offset += 8; ccos.write(pictstream, offset, 16); // rgbUid 1/2 ccos.flush(); offset += 16; ccos.write(pictstream, offset, 34); // metafileHeader offset += 34; ccos.flush(); } else { ccos.write(pictstream, offset, 1); // tag offset += 1; ccos.flush();
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; }
@Override public void close() throws IOException { if (cyos != null) { cyos.close(); } } }
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) { os.writePlain(bsrBuf, 0, 4); os.write(bsrBuf, 4, len-4); } else { int todo = len; plain.readFully(tmp, 0, nextLen); if (isPlain) { os.writePlain(tmp, 0, nextLen); } else { os.write(tmp, 0, nextLen); os.close(); } catch (Exception e) { throw new EncryptedDocumentException(e);
@Override public void write(int b) throws IOException { write(new byte[]{(byte)b}); }
protected void write(byte[] b, int off, int len, boolean writePlain) throws IOException { if (len == 0) { return; } if (len < 0 || b.length < off+len) { throw new IOException("not enough bytes in your input buffer"); } final int chunkMask = getChunkMask(); while (len > 0) { int posInChunk = (int)(pos & chunkMask); int nextLen = Math.min(chunk.length-posInChunk, len); System.arraycopy(b, off, chunk, posInChunk, nextLen); if (writePlain) { plainByteFlags.set(posInChunk, posInChunk+nextLen); } pos += nextLen; totalPos += nextLen; off += nextLen; len -= nextLen; if ((pos & chunkMask) == 0) { writeChunk(len > 0); } } }
public ChunkedCipherOutputStream(OutputStream stream, int chunkSize) throws IOException, GeneralSecurityException { super(stream); this.chunkSize = chunkSize; int cs = chunkSize == STREAMING ? 4096 : chunkSize; this.chunk = IOUtils.safelyAllocate(cs, MAX_RECORD_LENGTH); this.plainByteFlags = new BitSet(cs); this.chunkBits = Integer.bitCount(cs-1); this.fileOut = null; this.dir = null; this.cipher = initCipherForBlock(null, 0, false); }
@Override public void close() throws IOException { if (isClosed) { LOG.log(POILogger.DEBUG, "ChunkedCipherOutputStream was already closed - ignoring"); return; } isClosed = true; try { writeChunk(false); super.close(); if (fileOut != null) { int oleStreamSize = (int)(fileOut.length()+LittleEndianConsts.LONG_SIZE); calculateChecksum(fileOut, (int)pos); dir.createDocument(DEFAULT_POIFS_ENTRY, oleStreamSize, new EncryptedPackageWriter()); createEncryptionInfoEntry(dir, fileOut); } } catch (GeneralSecurityException e) { throw new IOException(e); } }
int posInChunk = (int)(pos & getChunkMask()); cipher = initCipherForBlock(cipher, index, lastChunk); ciLen = invokeCipher(posInChunk, doFinal); } catch (GeneralSecurityException e) { throw new IOException("can't re-/initialize cipher", e);
@Override public void write(byte[] b, int off, int len) throws IOException { write(b, off, len, false); }
public ChunkedCipherOutputStream(DirectoryNode dir, int chunkSize) throws IOException, GeneralSecurityException { super(null); this.chunkSize = chunkSize; int cs = chunkSize == STREAMING ? 4096 : chunkSize; this.chunk = IOUtils.safelyAllocate(cs, MAX_RECORD_LENGTH); this.plainByteFlags = new BitSet(cs); this.chunkBits = Integer.bitCount(cs-1); this.fileOut = TempFile.createTempFile("encrypted_package", "crypt"); this.fileOut.deleteOnExit(); this.out = new FileOutputStream(fileOut); this.dir = dir; this.cipher = initCipherForBlock(null, 0, false); }
@Override public void flush() throws IOException { setNextRecordSize(0, true); super.flush(); }