加密工具 conceal使用
来源:互联网 发布:淘宝产品详情图片尺寸 编辑:程序博客网 时间:2024/05/18 05:46
android 加密工具 Conceal
Conceal
的介绍
Conceal
提供了一系列的java api
给android
设备实现加解密操作,而其设计目的是为了更快和更高效的使用内存对硬盘上的大文件进行加密。
至于这个加密速度有多快,贴张官方的比较图,大家自己体会体会。
使用方法:
在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; }}
阅读全文
0 0
- 加密工具 conceal使用
- android 加密 SQLCipher和Conceal
- Android使用Facebook Conceal进行文件或文本内容的加密解密
- Facebook 开源数据加密API-Conceal
- (4.2.35.1)数据加密:Conceal
- (4.2.35)数据加密:SQLCipher和Conceal
- gpg 加密工具的使用
- GOOGLE Keyczar加密工具使用小贴士
- 加密工具和unlocker的使用
- 加密.NET工具Dotfuscator使用教程
- 使用DES加密解密的工具类
- 使用DES加密解密的工具类
- ruby 加密工具ruby encoder 使用相关。
- gpg(加密签名工具)使用
- DigestUtils加密工具类的使用
- Android加密工具--APKProtect的使用
- MD5加密工具类(直接使用)
- 使用Java写的MD5加密工具
- Altera Stratix V FPGA硬件加速平台
- c语言三子棋游戏
- Python学习篇之-手机号尾号4位自增
- Android studio +sd卡地址读取+录像机
- linux查看进程常用命令
- 加密工具 conceal使用
- C#学习笔记2-静态常量和动态常量
- czl蒻蒟的OI之路10、11、12
- 备份压缩命令
- JS实现表单多文件上传样式美化支持选中文件后删除相关项
- JAVA设计模式之单例模式
- Photoshop-Apple所有设备和切图尺寸(更新中)
- TP5 列出记录集
- 从nvm安装到vue-cli构建项目