加密工具 conceal使用

来源:互联网 发布:淘宝产品详情图片尺寸 编辑:程序博客网 时间:2024/05/18 05:46

android 加密工具 Conceal

Conceal的介绍

Conceal提供了一系列的java apiandroid设备实现加解密操作,而其设计目的是为了更快和更高效的使用内存对硬盘上的大文件进行加密。

至于这个加密速度有多快,贴张官方的比较图,大家自己体会体会。

这里写图片描述

使用方法:

build.gradle中添加如下依赖


compile 'com.facebook.conceal:conceal:2.0.1@aar'

#

1.生成加密对象
  KeyChain keyChain = new SharedPrefsBackedKeyChain(this, CryptoConfig.KEY_256);  Crypto mCrypto = AndroidConceal.get().createDefaultCrypto(keyChain);

加密文件的方法

    /**     * @param crypto     * @param srcFile 需要加密的源文件     * @param destFile 加密以后的文件     * @return     */    public static boolean encryptFile(Crypto crypto, File srcFile, File destFile) {        try {            FileInputStream fis = new FileInputStream(srcFile);            OutputStream fileStream = new BufferedOutputStream(new FileOutputStream(destFile));            OutputStream outputStream = crypto.getCipherOutputStream(                    fileStream,                    Entity.create(MyApplication.getContext().getResources().getString(R.string.encryptkey)));            int i=0;            byte[] buffer = new byte[1024];            while ((i = fis.read(buffer)) > 0) {                outputStream.write(buffer, 0, i);                outputStream.flush();            }            fis.close();            outputStream.close();            fileStream.close();            return true;        } catch (Exception e) {            e.printStackTrace();        }        return false;    }

解密文件的方法

public static boolean decryptFile(Crypto crypto, File srcFile, File destFile) {        try {            FileOutputStream out = new FileOutputStream(destFile);            BufferedOutputStream bos = new BufferedOutputStream(out);            FileInputStream fileStream = new FileInputStream(srcFile);            InputStream inputStream = crypto.getCipherInputStream(fileStream,Entity.create(MyApplication.getContext().getResources().getString(R.string.encryptkey)));            int read;            byte[] buffer = new byte[1024];            while ((read = inputStream.read(buffer)) != -1) {                bos.write(buffer, 0, read);                bos.flush();            }            out.close();            inputStream.close();            fileStream.close();            return true;        } catch (IOException e) {            e.printStackTrace();        } catch (KeyChainException e) {            e.printStackTrace();        } catch (CryptoInitializationException e) {            e.printStackTrace();        }        return false;    }
其中要注意的地方是
  InputStream inputStream = crypto.getCipherInputStream(  fileStream,  Entity.create("entity_id"));  InputStream inputStream = crypto.getCipherInputStream(  fileStream,  Entity.create("entity_id"));
加密和解密过程中Entity.create("entity_id"))传入的参数要保持一致

完整的加密工具类代码

    /**     * @param crypto     * @param srcFile 需要加密的文件     * @param destFile 加密以后的文件     * @return     */    public static boolean encryptFile(Crypto crypto, File srcFile, File destFile) {        try {            FileInputStream fis = new FileInputStream(srcFile);            OutputStream fileStream = new BufferedOutputStream(new FileOutputStream(destFile));            OutputStream outputStream = crypto.getCipherOutputStream(                    fileStream,                    Entity.create(MyApplication.getContext().getResources().getString(R.string.encryptkey)));            int i=0;            byte[] buffer = new byte[1024];            while ((i = fis.read(buffer)) > 0) {                outputStream.write(buffer, 0, i);                outputStream.flush();            }            fis.close();            outputStream.close();            fileStream.close();            return true;        } catch (Exception e) {            e.printStackTrace();        }        return false;    }    /**     * @param crypto     * @param srcFile 需要解密的文件     * @param destFile 解密后的文件     * @return     */    public static boolean decryptFile(Crypto crypto, File srcFile, File destFile) {        try {            FileOutputStream out = new FileOutputStream(destFile);            BufferedOutputStream bos = new BufferedOutputStream(out);            FileInputStream fileStream = new FileInputStream(srcFile);            InputStream inputStream = crypto.getCipherInputStream(fileStream,Entity.create(MyApplication.getContext().getResources().getString(R.string.encryptkey)));            int read;            byte[] buffer = new byte[1024];            while ((read = inputStream.read(buffer)) != -1) {                bos.write(buffer, 0, read);                bos.flush();            }            out.close();            inputStream.close();            fileStream.close();            return true;        } catch (IOException e) {            e.printStackTrace();        } catch (KeyChainException e) {            e.printStackTrace();        } catch (CryptoInitializationException e) {            e.printStackTrace();        }        return false;    }}