Search in sources :

Example 6 with CipherOutputStream

use of javax.crypto.CipherOutputStream in project robovm by robovm.

the class CipherOutputStream1Test method testCipherOutputStream.

/**
     * CipherOutputStream(OutputStream os) method testing. Tests that
     * CipherOutputStream uses NullCipher if Cipher is not specified
     * in the constructor.
     */
public void testCipherOutputStream() throws Exception {
    byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
    TestOutputStream tos = new TestOutputStream();
    CipherOutputStream cos = new CipherOutputStream(tos) {
    };
    cos.write(data);
    cos.flush();
    byte[] result = tos.toByteArray();
    if (!Arrays.equals(result, data)) {
        fail("NullCipher should be used " + "if Cipher is not specified.");
    }
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream)

Example 7 with CipherOutputStream

use of javax.crypto.CipherOutputStream in project robovm by robovm.

the class CipherOutputStream1Test method test_ConstructorLjava_io_OutputStreamLjavax_crypto_Cipher.

public void test_ConstructorLjava_io_OutputStreamLjavax_crypto_Cipher() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    KeyGenerator kg = KeyGenerator.getInstance("DES");
    kg.init(56, new SecureRandom());
    Key key = kg.generateKey();
    Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, key);
    CipherOutputStream cos = new CipherOutputStream(baos, c);
    assertNotNull(cos);
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) SecureRandom(java.security.SecureRandom) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Cipher(javax.crypto.Cipher) NullCipher(javax.crypto.NullCipher) KeyGenerator(javax.crypto.KeyGenerator) Key(java.security.Key)

Example 8 with CipherOutputStream

use of javax.crypto.CipherOutputStream in project AndroidChromium by JackyAndroid.

the class TabState method saveState.

/**
     * Writes the TabState to disk. This method may be called on either the UI or background thread.
     * @param file File to write the tab's state to.
     * @param state State object obtained from from {@link Tab#getState()}.
     * @param encrypted Whether or not the TabState should be encrypted.
     */
public static void saveState(File file, TabState state, boolean encrypted) {
    if (state == null || state.contentsState == null) {
        return;
    }
    // Create the byte array from contentsState before opening the FileOutputStream, in case
    // contentsState.buffer is an instance of MappedByteBuffer that is mapped to
    // the tab state file.
    byte[] contentsStateBytes = new byte[state.contentsState.buffer().limit()];
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        state.contentsState.buffer().rewind();
        state.contentsState.buffer().get(contentsStateBytes);
    } else {
        // http://b.android.com/53637.
        for (int i = 0; i < state.contentsState.buffer().limit(); i++) {
            contentsStateBytes[i] = state.contentsState.buffer().get(i);
        }
    }
    DataOutputStream dataOutputStream = null;
    FileOutputStream fileOutputStream = null;
    try {
        fileOutputStream = new FileOutputStream(file);
        if (encrypted) {
            Cipher cipher = CipherFactory.getInstance().getCipher(Cipher.ENCRYPT_MODE);
            if (cipher != null) {
                dataOutputStream = new DataOutputStream(new CipherOutputStream(fileOutputStream, cipher));
            } else {
                // better than failing to provide the guarantee of Incognito Mode.
                return;
            }
        } else {
            dataOutputStream = new DataOutputStream(fileOutputStream);
        }
        if (encrypted) {
            dataOutputStream.writeLong(KEY_CHECKER);
        }
        dataOutputStream.writeLong(state.timestampMillis);
        dataOutputStream.writeInt(contentsStateBytes.length);
        dataOutputStream.write(contentsStateBytes);
        dataOutputStream.writeInt(state.parentId);
        dataOutputStream.writeUTF(state.openerAppId != null ? state.openerAppId : "");
        dataOutputStream.writeInt(state.contentsState.version());
        dataOutputStream.writeLong(state.syncId);
        dataOutputStream.writeBoolean(state.shouldPreserve);
        dataOutputStream.writeInt(state.themeColor);
    } catch (FileNotFoundException e) {
        Log.w(TAG, "FileNotFoundException while attempting to save TabState.");
    } catch (IOException e) {
        Log.w(TAG, "IOException while attempting to save TabState.");
    } finally {
        StreamUtil.closeQuietly(dataOutputStream);
        StreamUtil.closeQuietly(fileOutputStream);
    }
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) DataOutputStream(java.io.DataOutputStream) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) Cipher(javax.crypto.Cipher) IOException(java.io.IOException)

Example 9 with CipherOutputStream

use of javax.crypto.CipherOutputStream in project AndroidChromium by JackyAndroid.

the class CookiesFetcher method saveFetchedCookiesToDisk.

private void saveFetchedCookiesToDisk(CanonicalCookie[] cookies) {
    DataOutputStream out = null;
    try {
        Cipher cipher = CipherFactory.getInstance().getCipher(Cipher.ENCRYPT_MODE);
        if (cipher == null) {
            // Something is wrong. Can't encrypt, don't save cookies.
            return;
        }
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        CipherOutputStream cipherOut = new CipherOutputStream(byteOut, cipher);
        out = new DataOutputStream(cipherOut);
        CanonicalCookie.saveListToStream(out, cookies);
        out.close();
        ImportantFileWriterAndroid.writeFileAtomically(fetchFileName(mContext), byteOut.toByteArray());
        out = null;
    } catch (IOException e) {
        Log.w(TAG, "IOException during Cookie Fetch");
    } catch (Throwable t) {
        Log.w(TAG, "Error storing cookies.", t);
    } finally {
        try {
            if (out != null)
                out.close();
        } catch (IOException e) {
            Log.w(TAG, "IOException during Cookie Fetch");
        }
    }
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) DataOutputStream(java.io.DataOutputStream) Cipher(javax.crypto.Cipher) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 10 with CipherOutputStream

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

the class CipherStreamClose method streamEncrypt.

public static byte[] streamEncrypt(String message, SecretKey key, MessageDigest digest) throws Exception {
    byte[] data;
    Cipher encCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    encCipher.init(Cipher.ENCRYPT_MODE, key);
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DigestOutputStream dos = new DigestOutputStream(bos, digest);
        CipherOutputStream cos = new CipherOutputStream(dos, encCipher)) {
        try (ObjectOutputStream oos = new ObjectOutputStream(cos)) {
            oos.writeObject(message);
        }
        data = bos.toByteArray();
    }
    if (debug) {
        System.out.println(DatatypeConverter.printHexBinary(data));
    }
    return data;
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) DigestOutputStream(java.security.DigestOutputStream) Cipher(javax.crypto.Cipher)

Aggregations

CipherOutputStream (javax.crypto.CipherOutputStream)39 Cipher (javax.crypto.Cipher)27 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 IOException (java.io.IOException)13 SecretKeySpec (javax.crypto.spec.SecretKeySpec)9 CipherInputStream (javax.crypto.CipherInputStream)8 IvParameterSpec (javax.crypto.spec.IvParameterSpec)7 FileOutputStream (java.io.FileOutputStream)6 SecretKey (javax.crypto.SecretKey)6 OutputStream (java.io.OutputStream)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 NullCipher (javax.crypto.NullCipher)5 InvalidKeyException (java.security.InvalidKeyException)4 KeyGenerator (javax.crypto.KeyGenerator)4 BufferedOutputStream (java.io.BufferedOutputStream)3 DataOutputStream (java.io.DataOutputStream)3 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)3 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)3 AEADBlockCipher (org.bouncycastle.crypto.modes.AEADBlockCipher)3 GCMBlockCipher (org.bouncycastle.crypto.modes.GCMBlockCipher)3