加密 SharedPreferences 中的数据 [英] encrypt data in SharedPreferences

查看:19
本文介绍了加密 SharedPreferences 中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个 oAuth 1 和 2 访问网络服务的框架,我的问题是,我如何以安全的方式存储敏感数据,如 oAuth 访问密钥?这个密钥的问题是像 twitter 这样的一些平台使用一个永久密钥,如果有人可以访问这个密钥,他可以对用户的 twitter 帐户做任何他想做的事情.

Im currently developing a framework for oAuth 1 and 2 access to webservices and my question is, how do i store sensitive data like an oAuth access key in a secure way? the problem with this keys is that some platforms like twitter use a permanent key and if someone would get access to this key he could do whatever he wants with the users twitter account..

那么是否可以在将数据存储到共享首选项之前自动加密数据?或者有没有更好的方法/地方来存储非常重要的数据?

so is it possible to automatically encrypt the data before it is stored in the shared preferences? Or is there a better way/place to store very important data?

更新 - 另请阅读:在 Android 应用程序中存储用户设置的最合适方式是什么

推荐答案

1).如何加密?

在 Android 上,加密是通过 Java 密码体系结构 (JCA) 完成的.主要是javax.crypto.*包.

On Android the encryption is done via Java Cryptography Architecture (JCA). Mainly it is the javax.crypto.* package.

JCA 参考指南

这是一个 示例 JCA API 使用(特别是 AES 算法).

Here is an example of JCA API usage (AES alrorithm in particular).

2).存储在哪里?

加密 API 使用字节数组(不是字符串)进行操作.这意味着您可以使用 SharedPreferences,但您需要在加密字节数组上应用 Base-64 编码,然后再将其放入 SharedPreferences(否则 XML 解析器将无法读取共享首选项文件).然后要阅读,您将需要使用 Base-64 解码.请注意,默认情况下,大多数 Android 操作系统版本没有内置的 Base-64 API(请参阅更新部分).因此,要消除此 Base-64 开销,我建议仅将您的字节存储在 私人文件.

Encryption API manipulates with byte arrays (not strings). This means you can use SharedPreferences, but you'll need to apply Base-64 encoding on the encrypted byte array before putting it into SharedPreferences (otherwise XML parser will fail to read the shared preferences file). Then to read you will need to use Base-64 decoding. Note that by default most Android OS versions do not have a built in Base-64 API (see UPDATE section). So to remove this Base-64 overhead I would recommend just to store your bytes in a private file.

更新:从 API 级别 8 开始,API 具有 android.util.Base64.

UPDATE: Since API Level 8, the API has android.util.Base64.

这篇关于加密 SharedPreferences 中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆