public static byte[] decrypt(InputStream fromInputStream, String password) throws IOException { CipherSession cipherSession = new CipherSession(password); MultiCipherInputStream cipherInputStream = new MultiCipherInputStream(fromInputStream, cipherSession); ByteArrayOutputStream plaintextOutputStream = new ByteArrayOutputStream(); FileUtil.appendToOutputStream(cipherInputStream, plaintextOutputStream); cipherInputStream.close(); plaintextOutputStream.close(); return plaintextOutputStream.toByteArray(); }
public static void encrypt( InputStream plaintextInputStream, OutputStream ciphertextOutputStream, List<CipherSpec> cipherSuites, String password) throws IOException { CipherSession cipherSession = new CipherSession(password); OutputStream multiCipherOutputStream = new MultiCipherOutputStream(ciphertextOutputStream, cipherSuites, cipherSession); FileUtil.appendToOutputStream(plaintextInputStream, multiCipherOutputStream); multiCipherOutputStream.close(); ciphertextOutputStream.close(); }
private void downloadAndDecryptMultiChunks(Set<MultiChunkEntry> unknownMultiChunks) throws StorageException, IOException { logger.log(Level.INFO, "- Downloading and extracting multichunks ..."); TransferManager transferManager = config.getConnection().createTransferManager(); // TODO [medium] Check existing files by checksum and do NOT download them if they exist // locally, or copy them for (MultiChunkEntry multiChunkEntry : unknownMultiChunks) { File localEncryptedMultiChunkFile = config.getCache().getEncryptedMultiChunkFile(multiChunkEntry.getId()); File localDecryptedMultiChunkFile = config.getCache().getDecryptedMultiChunkFile(multiChunkEntry.getId()); MultiChunkRemoteFile remoteMultiChunkFile = new MultiChunkRemoteFile( localEncryptedMultiChunkFile .getName()); // TODO [low] Make MultiChunkRemoteFile class, or something like that logger.log( Level.INFO, " + Downloading multichunk " + StringUtil.toHex(multiChunkEntry.getId()) + " ..."); transferManager.download(remoteMultiChunkFile, localEncryptedMultiChunkFile); result.downloadedMultiChunks.add(multiChunkEntry); logger.log( Level.INFO, " + Decrypting multichunk " + StringUtil.toHex(multiChunkEntry.getId()) + " ..."); InputStream multiChunkInputStream = config .getTransformer() .createInputStream(new FileInputStream(localEncryptedMultiChunkFile)); OutputStream decryptedMultiChunkOutputStream = new FileOutputStream(localDecryptedMultiChunkFile); // TODO [medium] Calculate checksum while writing file, to verify correct content FileUtil.appendToOutputStream(multiChunkInputStream, decryptedMultiChunkOutputStream); decryptedMultiChunkOutputStream.close(); multiChunkInputStream.close(); logger.log( Level.FINE, " + Locally deleting multichunk " + StringUtil.toHex(multiChunkEntry.getId()) + " ..."); localEncryptedMultiChunkFile.delete(); } transferManager.disconnect(); }
public static void appendToOutputStream(File fileToAppend, OutputStream outputStream) throws IOException { appendToOutputStream(new FileInputStream(fileToAppend), outputStream); }