public MultilayeredOutputStream(ArchiveInfoContext context, FileInventoryItem target) throws FileNotFoundException { this.target = target; this.topstream = new BlockWriterOutputStream( context.filePath, context.getBlockSize(), target, new BlockAllocationManager(context.getInventory(), target.getBlocks()) ); target.setEncryptionAlgorithm(context.getInventory().getDefaultEncryption()); if (target.isEncrypted()) { this.topstream = new CipherOutputStream( this.topstream, new BufferedBlockCipher(CipherBuilder.buildCipherForFile(target, true)) ); } else { target.setEncryptionData(null); } this.topstream = new DeflaterOutputStream(this.topstream, new Deflater(Deflater.BEST_SPEED)); }
@Override public OutputStream encryptedOutputStream(final Path path, final String password) throws IOException, EncryptionFailedException { try { final byte[] salt = generateSalt(); final byte[] key = generateKey(password, salt); final byte[] iv = generateIV(); final byte[] fileInitBlock = generateOutputInitBlock(salt, iv); final PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher( new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); final KeyParameter keyParam = new KeyParameter(key); final CipherParameters params = new ParametersWithIV(keyParam, iv); cipher.init(true, params); final BufferedOutputStream out = new BufferedOutputStream(Files.newOutputStream(path)); out.write(fileInitBlock); return new CipherOutputStream(out, cipher); } catch (InvalidKeySpecException | NoSuchAlgorithmException e) { throw new EncryptionFailedException(e); } }
private static OutputStream CreateOutputStream(OutputStream s, boolean bEncrypt, byte[] pbKey, byte[] pbIV) { byte[] pbLocalIV = new byte[16]; System.arraycopy(pbIV, 0, pbLocalIV, 0, 16); byte[] pbLocalKey = new byte[32]; System.arraycopy(pbKey, 0, pbLocalKey, 0, 32); try { BlockCipher aes = AesEngines.createAesEngine(); KeyParameter key = new KeyParameter(pbLocalKey); ParametersWithIV iv = new ParametersWithIV(key, pbLocalIV); BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(aes)); cipher.init(true, iv); // Cipher r = Cipher.getInstance("AES/CBC/PKCS5Padding"); // IvParameterSpec ivspec = new IvParameterSpec(pbLocalIV); // SecretKeySpec keyspec = new SecretKeySpec(pbLocalKey, "AES"); // r.init(Cipher.ENCRYPT_MODE, keyspec, ivspec); return new CipherOutputStream(s, cipher); } catch (Exception e) { throw new IllegalStateException(e); } }
@Override public void transform(InputStream in, OutputStream out) { try { AEADBlockCipher aeadBlockCipher = createAEADBlockCipher(); // Read the first 4 bytes as they contains nonce byte[] nonce = new byte[4]; int bytesRead = in.read(nonce); if (bytesRead < 4) { throw new RuntimeException("Failed to read nonce! Bytes read=" + bytesRead); } AEADParameters aeadParameters = new AEADParameters(new KeyParameter(key.getKey()), Constants.GCM_MAC_SIZE, nonce, nonSecretPayload); aeadBlockCipher.init(false, aeadParameters); try (CipherOutputStream cipherOutputStream = new CipherOutputStream(out, aeadBlockCipher)) { IOUtils.copy(in, cipherOutputStream); } } catch (IOException ex) { LOG.error(ex.getMessage(), ex); throw new EncryptorException(ex.getMessage(), ex); } }
public OutputStream getOutputStream(OutputStream dOut) { if (cipher instanceof BufferedBlockCipher) { return new CipherOutputStream(dOut, (BufferedBlockCipher)cipher); } else { return new CipherOutputStream(dOut, (StreamCipher)cipher); } }
public OutputEncryptor build(final char[] password) { if (random == null) { random = new SecureRandom(); } final byte[] salt = new byte[20]; random.nextBytes(salt); final PKCS12PBEParams pbeParams = new PKCS12PBEParams(salt, iterationCount); CipherParameters params = PKCS12PBEUtils.createCipherParameters(algorithm, digest, engine.getBlockSize(), pbeParams, password); engine.init(true, params); return new OutputEncryptor() { public AlgorithmIdentifier getAlgorithmIdentifier() { return new AlgorithmIdentifier(algorithm, pbeParams); } public OutputStream getOutputStream(OutputStream out) { return new CipherOutputStream(out, engine); } public GenericKey getKey() { return new GenericKey(new AlgorithmIdentifier(algorithm, pbeParams), PKCS12ParametersGenerator.PKCS12PasswordToBytes(password)); } }; }
private OutputStream createCipherOutputStream(OutputStream output, Object cipher) { if (cipher instanceof BufferedBlockCipher) { return new CipherOutputStream(output, (BufferedBlockCipher)cipher); } else if (cipher instanceof AEADBlockCipher) { return new CipherOutputStream(output, (AEADBlockCipher)cipher); } else { return new CipherOutputStream(output, (StreamCipher)cipher); } }
@Override public byte[] encrypt(byte[] buffer) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try (CipherOutputStream cipherOutputStream = new CipherOutputStream(byteArrayOutputStream, encryptor)) { cipherOutputStream.write(buffer); } catch (IOException e) { throw new RuntimeException(e); } return byteArrayOutputStream.toByteArray(); }
@Override public byte[] decrypt(byte[] buffer) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try (CipherOutputStream cipherOutputStream = new CipherOutputStream(byteArrayOutputStream, decryptor)) { cipherOutputStream.write(buffer); } catch (IOException e) { throw new RuntimeException(e); } return byteArrayOutputStream.toByteArray(); }
public CipherOutputStream newCipherOutputStream(OutputStream os, byte[] password) throws IOException { byte[] salt = randomBytes(saltLength); byte[] nonce = randomBytes(nonceLength); os.write(salt); os.write(nonce); byte[] dk = kdf.apply(password, salt); GCMBlockCipher cipher = new GCMBlockCipher(new AESEngine()); AEADParameters parameters = new AEADParameters(new KeyParameter(dk), tagLength * 8, nonce); cipher.init(true, parameters); return new CipherOutputStream(os, cipher); }
public static OutputStream setupOutputStream(OutputStream os, String reference) { if (reference != null && reference.length() == 96) { byte[] keyAndIv = hexToBytes(reference); byte[] key = new byte[32]; byte[] iv = new byte[16]; System.arraycopy(keyAndIv, 0, iv, 0, 16); System.arraycopy(keyAndIv, 16, key, 0, 32); AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine()); cipher.init(false, new AEADParameters(new KeyParameter(key), 128, iv)); return new CipherOutputStream(os, cipher); } else { return os; } }
public OutputEncryptor build(final char[] password) { if (random == null) { random = new SecureRandom(); } final byte[] salt = new byte[20]; final int iterationCount = 1024; random.nextBytes(salt); final PKCS12PBEParams pbeParams = new PKCS12PBEParams(salt, iterationCount); CipherParameters params = PKCS12PBEUtils.createCipherParameters(algorithm, digest, engine.getBlockSize(), pbeParams, password); engine.init(true, params); return new OutputEncryptor() { public AlgorithmIdentifier getAlgorithmIdentifier() { return new AlgorithmIdentifier(algorithm, pbeParams); } public OutputStream getOutputStream(OutputStream out) { return new CipherOutputStream(out, engine); } public GenericKey getKey() { return new GenericKey(new AlgorithmIdentifier(algorithm, pbeParams), PKCS12ParametersGenerator.PKCS12PasswordToBytes(password)); } }; }
public OutputStream getOutputStream(OutputStream out) { return new CipherOutputStream(out, c); }
public CipherEndableWriteStream(BufferEndableWriteStream delegate, AEADBlockCipher cipher) { this.delegate = delegate; this.bufferEndableWriteStreamOutputStream = new BufferEndableWriteStreamOutputStream(delegate); this.outputStream = new CipherOutputStream(bufferEndableWriteStreamOutputStream, cipher); }
public CipherReadStream(ReadStream<Buffer> delegate, AEADBlockCipher aeadBlockCipher) { this.delegate = delegate; this.readStreamDataHandlerOutputStream = new ReadStreamDataHandlerOutputStream(dataHandler); this.cipherOutputStream = new CipherOutputStream(readStreamDataHandlerOutputStream, aeadBlockCipher); }
public void serialize(OutputStream out, char[] password) throws IOException { try { // generate initial vector SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); byte[] iv = new byte[16]; random.nextBytes(iv); // generate salt byte[] salt = new byte[64]; random.nextBytes(salt); // initialize cipher CipherParameters params = new ParametersWithIV(new KeyParameter(deriveKey(password, salt)), iv); BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); cipher.reset(); cipher.init(true, params); // write magic number out.write(MagicNumbers.PRIVATE_KEY); out.flush(); // write initial vector and salt out.write(iv); out.write(salt); out.flush(); // write encrypted key to output stream ByteArrayOutputStream buf = new ByteArrayOutputStream(); CipherOutputStream cout = new CipherOutputStream(buf, cipher); cout.write(key); cout.close(); out.write(buf.toByteArray()); out.flush(); } catch (UnsupportedEncodingException | NoSuchAlgorithmException ex) { throw new UnsupportedOperationException(ex.getMessage(), ex); } }
public static OutputStream func_75897_a(SecretKey p_75897_0_, OutputStream p_75897_1_) { return new CipherOutputStream(p_75897_1_, func_75892_a(true, p_75897_0_)); }
public static OutputStream encryptOuputStream(SecretKey par0SecretKey, OutputStream par1OutputStream) { return new CipherOutputStream(par1OutputStream, createBufferedBlockCipher(true, par0SecretKey)); }
public static OutputStream encryptOutputStream(OutputStream outputStream, SecretKey key) { return new CipherOutputStream(outputStream, createBlockCipher(key, true)); }
public static CipherOutputStream createAesEncryptStream(final OutputStream os, final byte[] key, final SecureRandom srand) { final PaddedBufferedBlockCipher cipher = initAes(key, srand, true); return new CipherOutputStream(os, cipher); }