encrypt Block Cipher - Android java.security

Android examples for java.security:Decrypt Encrypt

Description

encrypt Block Cipher

Demo Code


//package com.java2s;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;

import java.security.NoSuchAlgorithmException;

import java.security.SecureRandom;

import javax.crypto.Cipher;

import javax.crypto.CipherOutputStream;

import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import android.net.Uri;
import android.os.Environment;

public class Main {
    private static final String SYMMETRIC_ALGORITHM = "AES";
    private static final String SECURERANDOM_ALGORITHM = "SHA1PRNG";
    private static final String SYMMETRIC_MODE = "CBC";
    private static final String SYMMETRIC_PADDING = "PKCS7Padding";

    private static boolean encryptBlockCipher(Uri fileName, SecretKey key,
            boolean withIV) {
        FileInputStream fis;//from  w w  w  .j  av  a2 s  .co m
        FileOutputStream fos;
        CipherOutputStream cos;

        File sdCard = Environment.getExternalStorageDirectory();
        File fileInput = new File(fileName.getPath());
        File fileOutputDirectory = new File(sdCard, "dCacheCloud/.enc");
        fileOutputDirectory.mkdirs();

        File fileOutput = new File(sdCard, String.format(
                "dCacheCloud/.enc/%s", fileName.getLastPathSegment()));

        // generate Cipher
        try {
            Cipher cipEncrypt = Cipher.getInstance(String.format(
                    "%s/%s/%s", SYMMETRIC_ALGORITHM, SYMMETRIC_MODE,
                    SYMMETRIC_PADDING));
            final byte[] ivData = new byte[cipEncrypt.getBlockSize()];
            SecureRandom rnd = SecureRandom
                    .getInstance(SECURERANDOM_ALGORITHM);
            rnd.nextBytes(ivData);
            final IvParameterSpec iv = new IvParameterSpec(ivData);

            fis = new FileInputStream(fileInput);
            fos = new FileOutputStream(fileOutput);

            fos.write(iv.getIV(), 0, iv.getIV().length);

            if (withIV)
                cipEncrypt.init(Cipher.ENCRYPT_MODE, key, iv);
            else
                cipEncrypt.init(Cipher.ENCRYPT_MODE, key);

            if (!fileInput.exists())
                fileInput.createNewFile();

            if (!fileOutput.exists())
                fileOutput.createNewFile();

            cos = new CipherOutputStream(fos, cipEncrypt);

            /// ENCRYPT ///
            int read;
            byte[] buffer = new byte[cipEncrypt.getBlockSize()];

            // put iv at the start!
            while ((read = fis.read(buffer)) != -1) {
                cos.write(buffer, 0, read);
            }

            cos.flush();
            cos.close();
            fos.close();
            fis.close();

            return true;
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
}

Related Tutorials