Search in sources :

Example 21 with CipherInputStream

use of javax.crypto.CipherInputStream in project jdk8u_jdk by JetBrains.

the class WrongAAD method decrypt.

private boolean decrypt(CipherOutputStream ciOutput, ByteArrayOutputStream baOutput) throws IOException {
    try (ByteArrayInputStream baInput = new ByteArrayInputStream(plainText);
        CipherInputStream ciInput = new CipherInputStream(baInput, encryptCipher)) {
        byte[] buffer = new byte[TEXT_SIZE];
        int len = ciInput.read(buffer);
        while (len != -1) {
            ciOutput.write(buffer, 0, len);
            len = ciInput.read(buffer);
        }
        ciOutput.flush();
        byte[] recoveredText = baOutput.toByteArray();
        System.out.println("recoveredText: " + new String(recoveredText));
        /*
             * See bug 8012900, AEADBadTagException is swalloed by CI/CO streams
             * If recovered text is empty, than decryption failed
             */
        if (recoveredText.length == 0) {
            return false;
        }
        return Arrays.equals(plainText, recoveredText);
    } catch (IllegalStateException e) {
        System.out.println("Expected IllegalStateException: " + e.getMessage());
        e.printStackTrace(System.out);
        return false;
    }
}
Also used : CipherInputStream(javax.crypto.CipherInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 22 with CipherInputStream

use of javax.crypto.CipherInputStream in project tika by apache.

the class EncryptedPrescriptionParser method parse.

public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException {
    try {
        Key key = Pharmacy.getKey();
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, key);
        InputStream decrypted = new CipherInputStream(stream, cipher);
        new PrescriptionParser().parse(decrypted, handler, metadata, context);
    } catch (GeneralSecurityException e) {
        throw new TikaException("Unable to decrypt a digital prescription", e);
    }
}
Also used : TikaException(org.apache.tika.exception.TikaException) CipherInputStream(javax.crypto.CipherInputStream) CipherInputStream(javax.crypto.CipherInputStream) InputStream(java.io.InputStream) GeneralSecurityException(java.security.GeneralSecurityException) Cipher(javax.crypto.Cipher) Key(java.security.Key)

Example 23 with CipherInputStream

use of javax.crypto.CipherInputStream in project bnd by bndtools.

the class PasswordCryptor method decrypt.

public InputStream decrypt(char[] password, InputStream out) throws Exception {
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
    SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
    final Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
    cipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);
    return new CipherInputStream(out, cipher);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) CipherInputStream(javax.crypto.CipherInputStream) Cipher(javax.crypto.Cipher)

Example 24 with CipherInputStream

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

the class CachedContentIndex method readFile.

private boolean readFile() {
    DataInputStream input = null;
    try {
        InputStream inputStream = new BufferedInputStream(atomicFile.openRead());
        input = new DataInputStream(inputStream);
        int version = input.readInt();
        if (version != VERSION) {
            // Currently there is no other version
            return false;
        }
        int flags = input.readInt();
        if ((flags & FLAG_ENCRYPTED_INDEX) != 0) {
            if (cipher == null) {
                return false;
            }
            byte[] initializationVector = new byte[16];
            input.readFully(initializationVector);
            IvParameterSpec ivParameterSpec = new IvParameterSpec(initializationVector);
            try {
                cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
            } catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
                throw new IllegalStateException(e);
            }
            input = new DataInputStream(new CipherInputStream(inputStream, cipher));
        } else {
            if (cipher != null) {
                // Force index to be rewritten encrypted after read.
                changed = true;
            }
        }
        int count = input.readInt();
        int hashCode = 0;
        for (int i = 0; i < count; i++) {
            CachedContent cachedContent = new CachedContent(input);
            add(cachedContent);
            hashCode += cachedContent.headerHashCode();
        }
        if (input.readInt() != hashCode) {
            return false;
        }
    } catch (FileNotFoundException e) {
        return false;
    } catch (IOException e) {
        Log.e(TAG, "Error reading cache content index file.", e);
        return false;
    } finally {
        if (input != null) {
            Util.closeQuietly(input);
        }
    }
    return true;
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) CipherInputStream(javax.crypto.CipherInputStream) DataInputStream(java.io.DataInputStream) BufferedInputStream(java.io.BufferedInputStream) CipherInputStream(javax.crypto.CipherInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) InvalidKeyException(java.security.InvalidKeyException) BufferedInputStream(java.io.BufferedInputStream) IvParameterSpec(javax.crypto.spec.IvParameterSpec)

Example 25 with CipherInputStream

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

the class CipherInputStreamTest method testCorruptDecryptEmpty.

@SuppressWarnings("InsecureCryptoUsage")
public void testCorruptDecryptEmpty(Iterable<TestVector> tests) throws Exception {
    for (TestVector t : tests) {
        Cipher cipher = Cipher.getInstance(t.algorithm);
        cipher.init(Cipher.DECRYPT_MODE, t.key, t.params);
        cipher.updateAAD(t.aad);
        byte[] ct = Arrays.copyOf(t.ct, t.ct.length);
        ct[ct.length - 1] ^= (byte) 1;
        InputStream is = new ByteArrayInputStream(ct);
        CipherInputStream cis = new CipherInputStream(is, cipher);
        try {
            byte[] result = new byte[t.pt.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);
            cis.close();
            fail("this should fail");
        } catch (IOException ex) {
        // expected
        }
    }
}
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) IOException(java.io.IOException)

Aggregations

CipherInputStream (javax.crypto.CipherInputStream)54 Cipher (javax.crypto.Cipher)31 ByteArrayInputStream (java.io.ByteArrayInputStream)18 IOException (java.io.IOException)17 InputStream (java.io.InputStream)14 NullCipher (javax.crypto.NullCipher)10 RuntimeException (java.lang.RuntimeException)9 SecretKeySpec (javax.crypto.spec.SecretKeySpec)7 Key (java.security.Key)6 CipherOutputStream (javax.crypto.CipherOutputStream)6 IvParameterSpec (javax.crypto.spec.IvParameterSpec)6 GeneralSecurityException (java.security.GeneralSecurityException)5 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)4 InvalidKeyException (java.security.InvalidKeyException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 BufferedInputStream (java.io.BufferedInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 DataInputStream (java.io.DataInputStream)3 Throwable (java.lang.Throwable)3 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)3