Encrypt piece of data - Android java.security

Android examples for java.security:Decrypt Encrypt

Description

Encrypt piece of data

Demo Code


//package com.java2s;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.crypto.Cipher;

import javax.crypto.CipherOutputStream;

public class Main {
    /**/*  ww  w .  j av a  2 s .  c  om*/
     * Encrypt piece of data
     *
     * @param encryptCipher an instance of {@link javax.crypto.Cipher} initialized for encryption
     * @param rawBytes a data to encrypt
     * @return an encrypted data
     * @throws IOException
     */
    public static byte[] encrypt(Cipher encryptCipher, byte[] rawBytes)
            throws IOException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        CipherOutputStream cipherOutputStream = new CipherOutputStream(
                outputStream, encryptCipher);
        cipherOutputStream.write(rawBytes);
        cipherOutputStream.flush();
        cipherOutputStream.close();
        return outputStream.toByteArray();
    }
}

Related Tutorials