decrypt Block Cipher Without IV - Android java.security

Android examples for java.security:Decrypt Encrypt

Description

decrypt Block Cipher Without IV

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 javax.crypto.Cipher;
import javax.crypto.CipherInputStream;

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 SYMMETRIC_MODE = "CBC";
    private static final String SYMMETRIC_PADDING = "PKCS7Padding";

    public static boolean decryptBlockCipherWithoutIV(Uri fileName,
            SecretKey key) {//from  w  ww. j  ava 2s  . co m
        return decryptBlockCipher(fileName, key, false);
    }

    private static boolean decryptBlockCipher(Uri fileName, SecretKey key,
            boolean withIV) {
        CipherInputStream cis;
        FileInputStream fis;
        FileOutputStream fos;

        // generate cipher
        try {
            File sdCard = Environment.getExternalStorageDirectory();
            Cipher cipDecrypt = Cipher.getInstance(String.format(
                    "%s/%s/%s", SYMMETRIC_ALGORITHM, SYMMETRIC_MODE,
                    SYMMETRIC_PADDING));
            byte[] ivData = new byte[cipDecrypt.getBlockSize()];

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

            File fileOutputDirectory = new File(sdCard, "dCacheCloud/");
            fileOutputDirectory.mkdirs();

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

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

            fis.read(ivData, 0, ivData.length);
            IvParameterSpec iv = new IvParameterSpec(ivData);

            if (withIV)
                cipDecrypt.init(Cipher.DECRYPT_MODE, key, iv);
            else
                cipDecrypt.init(Cipher.DECRYPT_MODE, key);

            cis = new CipherInputStream(fis, cipDecrypt);

            /// DECRYPT ///
            int read;
            byte[] buffer = new byte[cipDecrypt.getBlockSize()];
            while ((read = cis.read(buffer)) != -1) {
                fos.write(buffer, 0, read);
            }

            fos.flush();
            fos.close();
            cis.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