android - Android-如何在本地存储和检索 SecretKey 对象并检索它
问题描述
我正在创建一个应该允许加密音频文件的应用程序。我正在创建一个 SecretKey 对象并使用它来加密和解密文件。一切正常,但现在我需要一种方法在本地存储 SecretKey 对象,以便在再次打开应用程序时,可以使用它来解密数据。
我尝试了几种方法,包括以下方法:
final byte[] filesBytes = encodeFile(mySecretKey, stringToSave);
SharedPreferences settings = context.getSharedPreferences(KEY_PREFS, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("cryptoKey", Arrays.toString(filesBytes));
为了检索数据,我正在尝试以下操作:
SharedPreferences settings = context.getSharedPreferences(KEY_PREFS, 0);
String stringArray = settings.getString("cryptoKey", null);
if (stringArray != null) {
String[] split = stringArray.substring(1, stringArray.length() - 1).split(", ");
byte[] array = new byte[split.length];
for (int i = 0; i < split.length; i++) {
array[i] = Byte.parseByte(split[i]);
}
localSecretKey = new SecretKeySpec(array, "AES");
}
我还看过以下帖子:如何在 KeyStore 中存储 secretKey 并检索它,但没有帮助。
我需要知道的是,如何在本地存储 SecretKey 对象,以便稍后在重新打开应用程序时检索它?
解决方案
推荐阅读
- javascript - 观察 HTMLElements 上的隐式大小变化
- javascript - 使用 .filter() 过滤 javascript 对象
- android - 在 Gradle 中添加 dataBinding 时,无法在 null 对象上调用方法 dataBinding()
- ios - 在“CLUSTERED”附近抛出 SQL 语法错误:语法错误
- javascript - Fetch API 在 Chrome 中泄漏内存
- user-interface - 为什么 Google、Bing、Yahoo 等搜索引擎不尝试使用左侧(徽标下方)的可用空间?
- qt - 使用 Qt3D 加载 .FBX 文件
- html - 您如何将 html 中的 div 元素编码为链接?
- python - “res_model”列中的空值违反 Odoo11 中的非空约束
- java - 无法访问位于 src/main/resources/ 下的文件