Search in sources :

Example 96 with CipherInputStream

use of javax.crypto.CipherInputStream in project wycheproof by google.

the class CipherInputStreamTest method testEncrypt.

@SuppressWarnings("InsecureCryptoUsage")
public void testEncrypt(Iterable<TestVector> tests) throws Exception {
    for (TestVector t : tests) {
        Cipher cipher = Cipher.getInstance(t.algorithm);
        cipher.init(Cipher.ENCRYPT_MODE, t.key, t.params);
        cipher.updateAAD(t.aad);
        InputStream is = new ByteArrayInputStream(t.pt);
        CipherInputStream cis = new CipherInputStream(is, cipher);
        byte[] result = new byte[t.ct.length];
        int totalLength = 0;
        int length = 0;
        do {
            length = cis.read(result, totalLength, result.length - totalLength);
            if (length > 0) {
                totalLength += length;
            }
        } while (length >= 0 && totalLength != result.length);
        assertEquals(-1, cis.read());
        assertEquals(TestUtil.bytesToHex(t.ct), TestUtil.bytesToHex(result));
        cis.close();
    }
}
Also used : CipherInputStream(javax.crypto.CipherInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) CipherInputStream(javax.crypto.CipherInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Cipher(javax.crypto.Cipher)

Example 97 with CipherInputStream

use of javax.crypto.CipherInputStream in project openolat by klemens.

the class PersistedProperties method loadPropertiesFromFile.

/**
 * Load the persisted properties from disk. This can be useful when your
 * code gets a PersistedPropertiesChangedEvent and you just want to reload
 * the property instead of modifying the one you have already loaded.
 */
public void loadPropertiesFromFile() {
    // Might get an event after beeing disposed. Should not be the case, but you never know with multi user events accross nodes.
    if (propertiesChangedEventListener != null && configurationPropertiesFile.exists()) {
        InputStream is;
        try {
            is = new FileInputStream(configurationPropertiesFile);
            if (secured) {
                SecretKey key = generateKey("rk6R9pQy7dg3usJk");
                Cipher cipher = Cipher.getInstance("AES/CTR/NOPADDING");
                cipher.init(Cipher.DECRYPT_MODE, key, generateIV(cipher), random);
                is = new CipherInputStream(is, cipher);
            }
            configuredProperties.load(is);
            is.close();
        } catch (FileNotFoundException e) {
            logError("Could not load config file from path::" + configurationPropertiesFile.getAbsolutePath(), e);
        } catch (IOException e) {
            logError("Could not load config file from path::" + configurationPropertiesFile.getAbsolutePath(), e);
        } catch (Exception e) {
            logError("Could not load config file from path::" + configurationPropertiesFile.getAbsolutePath(), e);
        }
    }
}
Also used : SecretKey(javax.crypto.SecretKey) CipherInputStream(javax.crypto.CipherInputStream) CipherInputStream(javax.crypto.CipherInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) Cipher(javax.crypto.Cipher) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 98 with CipherInputStream

use of javax.crypto.CipherInputStream in project RxCache by VictorAlbertos.

the class BuiltInEncryptor method encrypt.

@Override
public void encrypt(String key, File decryptedFile, File encryptedFile) {
    initCiphers(key);
    try {
        CipherInputStream cis = new CipherInputStream(new FileInputStream(decryptedFile), encryptCipher);
        write(cis, new FileOutputStream(encryptedFile));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : CipherInputStream(javax.crypto.CipherInputStream) FileOutputStream(java.io.FileOutputStream) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException)

Example 99 with CipherInputStream

use of javax.crypto.CipherInputStream in project Signal-Android by signalapp.

the class ModernDecryptingPartInputStream method createFor.

private static InputStream createFor(@NonNull AttachmentSecret attachmentSecret, @NonNull byte[] random, @NonNull InputStream inputStream, long offset) throws IOException {
    try {
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(attachmentSecret.getModernKey(), "HmacSHA256"));
        byte[] iv = new byte[16];
        int remainder = (int) (offset % 16);
        Conversions.longTo4ByteArray(iv, 12, offset / 16);
        byte[] key = mac.doFinal(random);
        Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"), new IvParameterSpec(iv));
        long skipped = inputStream.skip(offset - remainder);
        if (skipped != offset - remainder) {
            throw new IOException("Skip failed: " + skipped + " vs " + (offset - remainder));
        }
        CipherInputStream cipherInputStream = new CipherInputStream(inputStream, cipher);
        byte[] remainderBuffer = new byte[remainder];
        readFully(cipherInputStream, remainderBuffer);
        return cipherInputStream;
    } catch (NoSuchAlgorithmException | InvalidKeyException | InvalidAlgorithmParameterException | NoSuchPaddingException e) {
        throw new AssertionError(e);
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) CipherInputStream(javax.crypto.CipherInputStream) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) Mac(javax.crypto.Mac) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher)

Example 100 with CipherInputStream

use of javax.crypto.CipherInputStream in project Signal-Android by signalapp.

the class EncryptedCoder method createEncryptedInputStream.

CipherInputStream createEncryptedInputStream(@NonNull byte[] masterKey, @NonNull File file) throws IOException {
    try {
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(masterKey, "HmacSHA256"));
        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] theirMagic = new byte[MAGIC_BYTES.length];
        byte[] theirRandom = new byte[32];
        byte[] theirEncryptedMagic = new byte[MAGIC_BYTES.length];
        StreamUtil.readFully(fileInputStream, theirMagic);
        StreamUtil.readFully(fileInputStream, theirRandom);
        if (!MessageDigest.isEqual(theirMagic, MAGIC_BYTES)) {
            throw new IOException("Not an encrypted cache file!");
        }
        byte[] iv = new byte[16];
        byte[] key = mac.doFinal(theirRandom);
        Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"), new IvParameterSpec(iv));
        CipherInputStream inputStream = new CipherInputStream(fileInputStream, cipher);
        StreamUtil.readFully(inputStream, theirEncryptedMagic);
        if (!MessageDigest.isEqual(theirEncryptedMagic, MAGIC_BYTES)) {
            throw new IOException("Key change on encrypted cache file!");
        }
        return inputStream;
    } catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException | InvalidAlgorithmParameterException e) {
        throw new AssertionError(e);
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) CipherInputStream(javax.crypto.CipherInputStream) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) Mac(javax.crypto.Mac) FileInputStream(java.io.FileInputStream) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher)

Aggregations

CipherInputStream (javax.crypto.CipherInputStream)102 Cipher (javax.crypto.Cipher)66 IOException (java.io.IOException)42 InputStream (java.io.InputStream)32 ByteArrayInputStream (java.io.ByteArrayInputStream)30 IvParameterSpec (javax.crypto.spec.IvParameterSpec)21 SecretKeySpec (javax.crypto.spec.SecretKeySpec)21 FileInputStream (java.io.FileInputStream)19 InvalidKeyException (java.security.InvalidKeyException)16 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)15 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)13 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 NullCipher (javax.crypto.NullCipher)11 DataInputStream (java.io.DataInputStream)9 RuntimeException (java.lang.RuntimeException)9 GeneralSecurityException (java.security.GeneralSecurityException)9 CipherOutputStream (javax.crypto.CipherOutputStream)8 BufferedInputStream (java.io.BufferedInputStream)7 Key (java.security.Key)7