public static PGPDataDecryptor createDataDecryptor(boolean withIntegrityPacket, BlockCipher engine, byte[] key) { final BufferedBlockCipher c = createStreamCipher(false, engine, withIntegrityPacket, key); return new PGPDataDecryptor() { public InputStream getInputStream(InputStream in) { return new CipherInputStream(in, c); } public int getBlockSize() { return c.getBlockSize(); } public PGPDigestCalculator getIntegrityCalculator() { return new SHA1PGPDigestCalculator(); } }; }
private InputStream createCipherInputStream(byte[] data, Object cipher) { ByteArrayInputStream input = new ByteArrayInputStream(data); if (cipher instanceof BufferedBlockCipher) { return new CipherInputStream(input, (BufferedBlockCipher)cipher); } else if (cipher instanceof AEADBlockCipher) { return new CipherInputStream(input, (AEADBlockCipher)cipher); } else { return new CipherInputStream(input, (StreamCipher)cipher); } }
public MultilayeredInputStream(ArchiveInfoContext context, FileInventoryItem target) { this.emptyFile = target.getActualSize() == 0; if (! emptyFile) { this.baseStream = new BlockReaderInputStream(context.filePath, context.getBlockSize(), target); this.topstream = this.baseStream; if (target.isEncrypted()) { this.topstream = new CipherInputStream( this.topstream, new BufferedBlockCipher(CipherBuilder.buildCipherForFile(target, false)) ); } this.topstream = new InflaterInputStream(this.topstream); } }
@Override public InputStream decryptedInputStream(final Path path, final String password) throws IOException, DecryptionFailedException { try { InputStream in = new BufferedInputStream(Files.newInputStream(path)); byte[] initBlock = readInitBlock(in); byte[] salt = extractSalt(initBlock); byte[] iv = extractIV(initBlock); byte[] key = generateKey(password, salt); PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); KeyParameter keyParam = new KeyParameter(key); CipherParameters params = new ParametersWithIV(keyParam, iv); cipher.init(false, params); return new CipherInputStream(in, cipher); } catch (InvalidKeySpecException | NoSuchAlgorithmException e) { throw new DecryptionFailedException(e); } }
private static InputStream CreateInputStream(InputStream 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 { // Cipher r = Cipher.getInstance("AES/CBC/PKCS5Padding"); // IvParameterSpec ivspec = new IvParameterSpec(pbLocalIV); // SecretKeySpec keyspec = new SecretKeySpec(pbLocalKey, "AES"); // r.init(Cipher.DECRYPT_MODE, keyspec, ivspec); BlockCipher aes = AesEngines.createAesEngine(); KeyParameter key = new KeyParameter(pbLocalKey); ParametersWithIV iv = new ParametersWithIV(key, pbLocalIV); BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(aes)); cipher.init(false, iv); return new CipherInputStream(s, cipher); } catch (Exception e) { throw new IllegalStateException(e); } }
/** * Encrypt or decrypt data with AES256. * * @param data The data to encrypt. * @param output The output to write the encrypted data to. * * @throws IOException If there is an error reading the data. */ private void decryptDataAES256(InputStream data, OutputStream output) throws IOException { byte[] iv = new byte[16]; // read IV from stream int ivSize = data.read(iv); if (ivSize == -1) { return; } if (ivSize != iv.length) { throw new IOException("AES initialization vector not fully read: only " + ivSize + " bytes read instead of " + iv.length); } PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher( new CBCBlockCipher(new AESFastEngine())); cipher.init(false, new ParametersWithIV(new KeyParameter(encryptionKey), iv)); try (CipherInputStream cis = new CipherInputStream(data, cipher)) { org.apache.commons.io.IOUtils.copy(cis, output); } }
@Override public void transform(InputStream in, OutputStream out) { try { AEADBlockCipher aeadBlockCipher = createAEADBlockCipher(); AEADParameters aeadParameters = new AEADParameters(new KeyParameter(key.getKey()), Constants.GCM_MAC_SIZE, nonce, nonSecretPayload); aeadBlockCipher.init(true, aeadParameters); // Write nonce as first 4 bytes out.write(this.nonce); try (CipherInputStream cipherInputStream = new CipherInputStream(in, aeadBlockCipher)) { IOUtils.copy(cipherInputStream, out); } } catch (Exception ex) { LOG.error(ex.getMessage(), ex); throw new EncryptorException(ex.getMessage(), ex); } }
public InputDecryptorProvider build(final char[] password) { return new InputDecryptorProvider() { public InputDecryptor get(final AlgorithmIdentifier algorithmIdentifier) { final PaddedBufferedBlockCipher engine = PKCS12PBEUtils.getEngine(algorithmIdentifier.getAlgorithm()); PKCS12PBEParams pbeParams = PKCS12PBEParams.getInstance(algorithmIdentifier.getParameters()); CipherParameters params = PKCS12PBEUtils.createCipherParameters(algorithmIdentifier.getAlgorithm(), digest, engine.getBlockSize(), pbeParams, password); engine.init(false, params); return new InputDecryptor() { public AlgorithmIdentifier getAlgorithmIdentifier() { return algorithmIdentifier; } public InputStream getInputStream(InputStream input) { return new CipherInputStream(input, engine); } public GenericKey getKey() { return new GenericKey(PKCS12ParametersGenerator.PKCS12PasswordToBytes(password)); } }; } }; }
public RecipientOperator getRecipientOperator(AlgorithmIdentifier keyEncryptionAlgorithm, final AlgorithmIdentifier contentEncryptionAlgorithm, byte[] derivedKey, byte[] encryptedContentEncryptionKey) throws CMSException { KeyParameter secretKey = extractSecretKey(keyEncryptionAlgorithm, contentEncryptionAlgorithm, derivedKey, encryptedContentEncryptionKey); final Object dataCipher = EnvelopedDataHelper.createContentCipher(false, secretKey, contentEncryptionAlgorithm); return new RecipientOperator(new InputDecryptor() { public AlgorithmIdentifier getAlgorithmIdentifier() { return contentEncryptionAlgorithm; } public InputStream getInputStream(InputStream dataOut) { if (dataCipher instanceof BufferedBlockCipher) { return new CipherInputStream(dataOut, (BufferedBlockCipher)dataCipher); } else { return new CipherInputStream(dataOut, (StreamCipher)dataCipher); } } }); }
public RecipientOperator getRecipientOperator(AlgorithmIdentifier keyEncryptionAlgorithm, final AlgorithmIdentifier contentEncryptionAlgorithm, byte[] encryptedContentEncryptionKey) throws CMSException { CipherParameters secretKey = extractSecretKey(keyEncryptionAlgorithm, contentEncryptionAlgorithm, encryptedContentEncryptionKey); final Object dataCipher = EnvelopedDataHelper.createContentCipher(false, secretKey, contentEncryptionAlgorithm); return new RecipientOperator(new InputDecryptor() { public AlgorithmIdentifier getAlgorithmIdentifier() { return contentEncryptionAlgorithm; } public InputStream getInputStream(InputStream dataIn) { if (dataCipher instanceof BufferedBlockCipher) { return new CipherInputStream(dataIn, (BufferedBlockCipher)dataCipher); } else { return new CipherInputStream(dataIn, (StreamCipher)dataCipher); } } }); }
public static Curve25519PrivateKey deserialize(InputStream in, char[] password) throws IOException { try { // check magic number byte[] mn = new byte[MagicNumbers.PRIVATE_KEY.length]; IOUtils.readFully(in, mn, 0, mn.length); if (!Arrays.areEqual(mn, MagicNumbers.PRIVATE_KEY)) throw new IllegalArgumentException("Wrong key file format"); // read initial vector byte[] iv = new byte[16]; IOUtils.readFully(in, iv, 0, iv.length); // read salt byte[] salt = new byte[64]; IOUtils.readFully(in, salt, 0, salt.length); // 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(false, params); // decrypt key CipherInputStream cin = new CipherInputStream(in, cipher); byte[] key = new byte[Curve25519.KEY_SIZE]; IOUtils.readFully(cin, key, 0, key.length); // return key instance return new Curve25519PrivateKey(key); } catch (UnsupportedEncodingException ex) { throw new UnsupportedOperationException(ex.getMessage(), ex); } }
public CipherInputStream newCipherInputStream(InputStream is, byte[] password) throws IOException { byte[] salt = IOUtils.readFully(is, saltLength); byte[] nonce = IOUtils.readFully(is, nonceLength); byte[] dk = kdf.apply(password, salt); GCMBlockCipher cipher = new GCMBlockCipher(new AESEngine()); AEADParameters parameters = new AEADParameters(new KeyParameter(dk), tagLength * 8, nonce); cipher.init(false, parameters); return new CipherInputStream(is, cipher); }
public static InputStream setupInputStream(InputStream is, byte[] keyAndIv) { if (keyAndIv != null && keyAndIv.length == 48) { 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(true, new AEADParameters(new KeyParameter(key), 128, iv)); return new CipherInputStream(is, cipher); } else { return is; } }
CipherInputStream cipherInputStream(InputStream inputStream, byte[] key, byte[] checksum) { CFBBlockCipher cipher = new CFBBlockCipher(new AESFastEngine(), 128); KeyParameter keyParameter = new KeyParameter(key); cipher.init(false, keyParameter); return new CipherInputStream(inputStream, cipher); }
static InputStream decryptStream(InputStream in, XFileKey keyCipher) { BlockCipher cipher = keyCipher.ciphers().get(); cipher.init(false, new KeyParameter(keyCipher.key())); return new CipherInputStream(in, new BufferedBlockCipher(cipher)); }
@Override public InputStream encryptStream(InputStream data, byte[] key, byte[] iv) { init(key, iv); return new CipherInputStream(data, cipher); }
@Override public InputStream encryptStream(InputStream data, byte[] key) { init(key); return new CipherInputStream(data, cipher); }
public static InputStream func_75888_a(SecretKey p_75888_0_, InputStream p_75888_1_) { return new CipherInputStream(p_75888_1_, func_75892_a(false, p_75888_0_)); }
public static InputStream decryptInputStream(SecretKey par0SecretKey, InputStream par1InputStream) { return new CipherInputStream(par1InputStream, createBufferedBlockCipher(false, par0SecretKey)); }
public static InputStream decryptInputStream(InputStream inputStream, SecretKey key) { return new CipherInputStream(inputStream, createBlockCipher(key, false)); }
public static CipherInputStream createAesDecryptStream(final InputStream is, final byte[] key, final SecureRandom srand) { final PaddedBufferedBlockCipher cipher = initAes(key, srand, false); return new CipherInputStream(is, cipher); }