Java Code Examples for javax.crypto.CipherOutputStream#flush()

The following examples show how to use javax.crypto.CipherOutputStream#flush() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AesUtil.java    From littleca with Apache License 2.0 6 votes vote down vote up
public static void decryptInputStream(InputStream inputStream, String password, OutputStream out) throws Exception {
    try {
        Cipher cipher = AesHelper.getAesCipher(iv, password.getBytes(), Cipher.DECRYPT_MODE, CIPHER_MODE);
        CipherOutputStream cipherOutputStream = new CipherOutputStream(out, cipher);
        byte[] buf = new byte[BUFF_SIZE];
        int len = -1;
        while ((len = inputStream.read(buf)) != -1) {
            cipherOutputStream.write(buf, 0, len);
            cipherOutputStream.flush();
        }
        cipherOutputStream.close();
        out.close();
        inputStream.close();
    } catch (Exception e) {
        throw e;
    }
}
 
Example 2
Source File: WrongAAD.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
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;
    }
}
 
Example 3
Source File: WrongAAD.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
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;
    }
}
 
Example 4
Source File: CryptUtil.java    From PowerFileExplorer with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Helper method to encrypt a file
 * @param inputStream stream associated with the file to be encrypted
 * @param outputStream stream associated with new output encrypted file
 * @throws CertificateException
 * @throws NoSuchAlgorithmException
 * @throws KeyStoreException
 * @throws NoSuchProviderException
 * @throws InvalidAlgorithmParameterException
 * @throws IOException
 * @throws NoSuchPaddingException
 * @throws UnrecoverableKeyException
 * @throws InvalidKeyException
 * @throws BadPaddingException
 * @throws IllegalBlockSizeException
 */
@RequiresApi(api = Build.VERSION_CODES.M)
private static void aesEncrypt(BufferedInputStream inputStream, BufferedOutputStream outputStream)
        throws CertificateException, NoSuchAlgorithmException, KeyStoreException,
        NoSuchProviderException, InvalidAlgorithmParameterException, IOException,
        NoSuchPaddingException, UnrecoverableKeyException, InvalidKeyException,
        BadPaddingException, IllegalBlockSizeException {

    Cipher cipher = Cipher.getInstance(ALGO_AES);

    GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(128, IV.getBytes());

    cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(), gcmParameterSpec);

    byte[] buffer = new byte[GenericCopyUtil.DEFAULT_BUFFER_SIZE];
    int count;

    CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, cipher);

    try {

        while ((count = inputStream.read(buffer)) != -1) {

            cipherOutputStream.write(buffer, 0, count);
            ServiceWatcherUtil.POSITION+=count;
        }
    } finally {

        cipherOutputStream.flush();
        cipherOutputStream.close();
        inputStream.close();
    }
}
 
Example 5
Source File: CryptUtil.java    From PowerFileExplorer with GNU General Public License v3.0 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
private static void rsaEncrypt(Context context, BufferedInputStream inputStream, BufferedOutputStream outputStream)
        throws NoSuchPaddingException, NoSuchAlgorithmException, NoSuchProviderException,
        CertificateException, BadPaddingException, InvalidAlgorithmParameterException,
        KeyStoreException, UnrecoverableEntryException, IllegalBlockSizeException,
        InvalidKeyException, IOException {

    Cipher cipher = Cipher.getInstance(ALGO_AES, "BC");
    RSAKeygen keygen = new RSAKeygen(context);

    IvParameterSpec ivParameterSpec = new IvParameterSpec(IV.getBytes());
    cipher.init(Cipher.ENCRYPT_MODE, keygen.getSecretKey(), ivParameterSpec);

    byte[] buffer = new byte[GenericCopyUtil.DEFAULT_BUFFER_SIZE];
    int count;

    CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, cipher);
    try {

        while ((count = inputStream.read(buffer)) != -1) {

            cipherOutputStream.write(buffer, 0, count);
            ServiceWatcherUtil.POSITION+=count;
        }
    } finally {

        cipherOutputStream.flush();
        cipherOutputStream.close();
        inputStream.close();
    }
}
 
Example 6
Source File: WrongAAD.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
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;
    }
}
 
Example 7
Source File: WrongAAD.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
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;
    }
}
 
Example 8
Source File: WrongAAD.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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;
    }
}
 
Example 9
Source File: WrongAAD.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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;
    }
}
 
Example 10
Source File: CipherAgentImpl.java    From GDH with MIT License 5 votes vote down vote up
/**
 * Encrypt a value with a key and initial vector
 * 
 * @param value
 *            the String value to encrypt
 * @param iv
 *            the initial vector. Must be a random 128 bit value and the
 *            same one used in decryption
 * @param key
 *            the key for encryption
 * 
 * @return the encrypted bytes
 */
public byte[] encrypt(String value, byte[] iv, SecretKey key)
        throws InvalidKeyException, InvalidAlgorithmParameterException, IOException {
    byte[] encryptedBytes = null;
    IvParameterSpec ivspec = new IvParameterSpec(iv);
    encryptCipher.init(Cipher.ENCRYPT_MODE, key, ivspec);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, encryptCipher);
    cipherOutputStream.write(value.getBytes(StandardCharsets.UTF_8));
    cipherOutputStream.flush();
    cipherOutputStream.close();
    encryptedBytes = outputStream.toByteArray();

    return encryptedBytes;
}
 
Example 11
Source File: WrongAAD.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
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;
    }
}
 
Example 12
Source File: WrongAAD.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
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;
    }
}
 
Example 13
Source File: IdSampleFragment.java    From genymotion-binocle with Apache License 2.0 5 votes vote down vote up
private void doEncode() {
    try {
        // Set up secret key spec for 128-bit AES encryption
        String androidId = Secure.getString(getActivity().getContentResolver(), Secure.ANDROID_ID);
        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        KeySpec spec = new PBEKeySpec(androidId.toCharArray(), salt, 45, 128);
        SecretKeySpec sks = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES");

        // Create AES cipher
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, sks);

        // This stream write the encrypted text.
        FileOutputStream fos = getActivity().openFileOutput(FILE_NAME, Activity.MODE_PRIVATE);
        // Wrap the output stream
        CipherOutputStream cos = new CipherOutputStream(fos, cipher);

        // Write bytes
        cos.write(SECRET_MESSAGE.getBytes(UTF8_CHARSET));

        // Flush and close streams.
        cos.flush();
        cos.close();
        fos.close();

        tvAndroidId.setText(getActivity().getResources().getString(R.string.encoding_done, androidId));

    } catch (NoSuchPaddingException
            |NoSuchAlgorithmException
            |IOException
            |InvalidKeyException
            |InvalidKeySpecException e) {
        Log.e(TAG, "Unable to encrypt secret", e);
        tvAndroidId.setText(R.string.encoding_failed);
    }
}