Facebook 开源数据加密API-Conceal
来源:互联网 发布:php curl rest 编辑:程序博客网 时间:2024/05/03 17:11
一.什么是Concel
Conceal是 Facebook 推出的一个用来对数据进行快速加密和认证的开发包,应用可以使用它来加密数据和大文件存储。Conceal是一套用于Android上的文件加密和鉴权的Java API,它使用OpenSSL算法的子集以及一些预先定义的选项,以便让库保持在较小的体积——目前仅有85KB大小。对于比较古老的Android设备——从Froyo到JellyBean——据Facebook表示,该库在这些设备上的性能要优于Android提供的原生支持。Conceal 和其他加密软件不同的是它提供了Smorgasbord 加密算法和选项,这不是一个一般意义上的数据加密包,只是提供一些有用的方法。
二.性能比较
上图中的基准测试,展示了在Galaxy Y终端上,对原生Android算法(ES-CTR-HMAC-SHA1)、Bouncycastle((AES-GCM)和Concea进行对比的结果l。
Google在KitKat中引入了对OpenSSL的支持,但是其中使用的默认密码流则“表现差强人意”——在Facebook看来,“当使用我们的密码流(参阅BetterCipherInputStream.java文件)来替换它时,将使得默认实现能够与Conceal相媲美。”
三.简单使用
引用
compile 'com.facebook.conceal:conceal:1.1.2'
'
// 加密 byte[] cipherText = mCrypto.encrypt(plainText, mEntity); // 解密 byte[] plainText = mCrypto.decrypt(cipherText, mEntity);
//使用秘钥链和原生库的默认实现,来创建一个新的加密对象 Crypto crypto = new Crypto( new SharedPrefsBackedKeyChain(context), new SystemNativeCryptoLibrary()); //检查加密功能是否可用 //如果Android没有正确载入库,则此步骤可能失败 if (!crypto.isAvailable()) { return; } OutputStream fileStream = new BufferedOutputStream( new FileOutputStream(file)); //创建输出流,当数据写入流的时候进行加密,并将加密后的数据输出到文件 OutputStream outputStream = crypto.getCipherOutputStream( fileStream, entity);//将纯文本写入其中 outputStream.write(plainText); outputStream.close();
相关问题
能不能将密文直接写入SharedPreferences中呢? 答案是可以的,但是不能简单的实用API提供的加密解密方法,那样是不行的。(以 Hello Word!为例)思路是这样的:由于加密、加密结果、解密三个操作参数都是byte,而SharedPreferences接受的参数是String类型,因此我们需要进行转换,newString(bytes, "utf-8") 和 string.getBytes("utf-8") , 当你这么做的时候,你会发现解密死活不成功。原因很简单,一个utf-8编码汉字是2bytes,如果我们的加密结果不足2byte,我们强转时不足部分就会被补齐,当再转换回来的时候就和原来的不一样了。解决办法:用Base64对byte数组进行编码,具体怎么做可以参考Conceal源码中的SharedPrefsBackedKeyChain。
/**
* Visible for testing.
*/
/* package */ String encodeForPrefs(byte[]key) {
if (key==null) {
return null;
}
return Base64.encodeToString(key, Base64.DEFAULT);
}
异常
java.lang.NoSuchFieldError:no field with name='mCtxPtr' signature='J' in classLcom/facebook/crypto/cipher/NativeGC
This happensbecause native code needs to refer to Java fields/methods. For doing so it usestypical JNI functions which receive the name and signature. At the same timetools like proguard trim off or rename class members in order to get smallerexecutables. Normally this process is run on release versions. When native coderequest the member, it's not present anymore.
To avoid thiskind of problems exceptions can be defined. You will need to configure proguardwith the rules defined inproguard_annotations.pro. You can use the file as is, or you can include its content in your ownproguard configuration file.
这是因为本地代码需要引用java字段/方法。因此它使用典型的JNI函数接收签名。同时工具像ProGuard的修剪或重命名类成员为了获得小的可执行文件。通常这个过程是在发布版本上运行的。当本地代码请求成员时,它不存在了。
为了避免这种问题的异常可以被定义。你需要在proguard_annotations.pro定义的规则配置ProGuard。你可以使用文件,也可以包含在你自己的混淆器配置文件内容。
# Keep our interfaces so they can be used by otherProGuard rules.
# See http://sourceforge.net/p/proguard/bugs/466/
-keep,allowobfuscation @interfacecom.facebook.crypto.proguard.annotations.DoNotStrip
-keep,allowobfuscation @interfacecom.facebook.crypto.proguard.annotations.KeepGettersAndSetters
# Do not strip any method/class that is annotated with @DoNotStrip
-keep @com.facebook.crypto.proguard.annotations.DoNotStripclass *
-keepclassmembers class * {
@com.facebook.crypto.proguard.annotations.DoNotStrip *;
}
-keepclassmembers @com.facebook.crypto.proguard.annotations.KeepGettersAndSettersclass * {
void set*(***);
*** get*();
}
- Facebook 开源数据加密API-Conceal
- (4.2.35.1)数据加密:Conceal
- (4.2.35)数据加密:SQLCipher和Conceal
- 加密工具 conceal使用
- Android使用Facebook Conceal进行文件或文本内容的加密解密
- android 加密 SQLCipher和Conceal
- IOS加密api数据接口
- Android本地数据安全尝试(中)——Conceal
- Facebook 正式开源其大数据查询引擎 Presto
- Presto Facebook 开源的大数据查询引擎
- api接口数据加密和身份验证
- facebook 开源项目
- Facebook开源成就
- Facebook 开源图片库
- facebook开源项目
- Facebook开源项目
- Facebook API Developers Guide
- Facebook API Begining
- Spring配置数据源的三种方式 (dbcp,c3p0,jndi)
- [系统操作][Linux]配置git
- php --- get参数的传递和接收
- 数据结构复习之图(2)
- 01-线性可分条件下判别函数权矢量算法
- Facebook 开源数据加密API-Conceal
- 非刚性的人脸追踪(上)
- Web,tomcat
- leetcode 14. Longest Common Prefix
- json - 使用jackson进行序列化/反序列化
- 苹果清理xcode导出的版本
- Java提高篇之详解内部类
- 关于 C# 中 Struct 和 C++/Cli 中 value class 的 new 和 gcnew 的区别
- 使用phantomJs服务器端导出Echarts图片