encrypt Or Decrypt File - Android java.security

Android examples for java.security:Decrypt Encrypt

Description

encrypt Or Decrypt File

Demo Code


//package com.java2s;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;

public class Main {
    private static final int CACHE_SIZE = 1024;

    public static boolean enOrDecryptFile(byte[] paramArrayOfByte,
            String sourceFilePath, String destFilePath, int mode) {
        File sourceFile = new File(sourceFilePath);
        File destFile = new File(destFilePath);
        CipherOutputStream cout = null;
        FileInputStream in = null;
        FileOutputStream out = null;
        if (sourceFile.exists() && sourceFile.isFile()) {
            if (!destFile.getParentFile().exists()) {
                destFile.getParentFile().mkdirs();
            }/*from   w  w w. j av  a 2  s.co  m*/
            try {
                destFile.createNewFile();
                in = new FileInputStream(sourceFile);
                out = new FileOutputStream(destFile);
          
                KeyGenerator kgen = KeyGenerator.getInstance("AES");
                SecureRandom secureRandom = SecureRandom.getInstance(
                        "SHA1PRNG", "Crypto");
                secureRandom.setSeed("aasdaqr1235134".getBytes());
                kgen.init(128, secureRandom);

                SecretKey secretKey = kgen.generateKey();
                byte[] enCodeFormat = secretKey.getEncoded();
                SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");

                Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); 
                SecretKeySpec secretKeySpec = new SecretKeySpec(
                        key.getEncoded(), "AES");

                cipher.init(mode, secretKeySpec);
                cout = new CipherOutputStream(out, cipher);
                byte[] cache = new byte[CACHE_SIZE];
                int nRead = 0;
                while ((nRead = in.read(cache)) != -1) {
                    cout.write(cache, 0, nRead);
                    cout.flush();
                }
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
                return false;
            } catch (NoSuchPaddingException e) {
                e.printStackTrace();
                return false;
            } catch (InvalidKeyException e) {
                e.printStackTrace();
                return false;
            } catch (NoSuchProviderException e) {
                e.printStackTrace();
            } finally {
                if (cout != null) {
                    try {
                        cout.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return true;
        }
        return false;
    }
}

Related Tutorials