Security 库提供了与读取和写入静态数据以及密钥创建和验证相关的安全最佳做法的实现。

该库使用构建器模式为以下安全等级提供安全的默认设置:

在可靠加密和良好性能之间取得平衡的强安全性。这种安全等级适用于银行应用和聊天应用等消费者应用,以及执行证书吊销检查的企业应用。

最高安全性。这种安全等级适用于需要由硬件支持的密钥库和需要用户介入以提供密钥访问权限的应用。

本指南介绍了如何使用 Security 库的建议安全配置,以及如何轻松且安全地读取和写入存储在文件和共享偏好设置中的加密数据。

注意:Security 库版本 1.0.0 仅适用于运行 Android 6.0(API 级别 23)和更高版本的设备。更高版本的库支持更多 Android 版本。

密钥管理

Security 库使用一个由两部分组成的密钥管理系统:

包含一个或多个密钥的密钥集,用于对文件或共享偏好设置数据进行加密。密钥集本身存储在 SharedPreferences 中。

用于加密所有密钥集的主 (master) 密钥。此密钥使用 Android 密钥库系统进行存储。

库中包含的类

Security 库包含以下类,旨在提供更安全的静态数据机制:

为了提供安全的文件流读写操作,Security 库使用了流式 AEAD(带关联数据的认证加密)基元。如需详细了解该基元,请参阅 GitHub 上的 Tink 库文档。

封装 SharedPreferences 类,并使用双重方案方法自动加密密钥和值:

密钥使用确定性加密算法进行加密,这样便可以加密并正确查找密钥。

值使用 AES-256 GCM 加密,并且具有不确定性。

注意:EncryptedFile 类和 EncryptedSharedPreferences 类中的方法不是线程安全的。

以下部分介绍了如何使用这些类对文件和共享偏好设置执行常见操作。

将此库包含到您的项目中

如需使用 Security 库,请将以下依赖项添加到应用模块的 build.gradle 文件:

dependencies {
implementation "androidx.security:security-crypto:1.0.0"
// For Identity Credential APIs
implementation "androidx.security:security-identity-credential:1.0.0-alpha02"
}

读取文件

以下代码段演示了如何使用 EncryptedFile 以更安全的方式读取文件内容:

Kotlin
// Although you can define your own key generation parameter specification, it's
// recommended that you use the value specified here.
val mainKey = MasterKey.Builder(applicationContext)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build()
val fileToRead = "my_sensitive_data.txt"
val encryptedFile = EncryptedFile.Builder(
applicationContext,
File(DIRECTORY, fileToRead),
mainKey,
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
).build()
val inputStream = encryptedFile.openFileInput()
val byteArrayOutputStream = ByteArrayOutputStream()
var nextByte: Int = inputStream.read()
while (nextByte != -1) {
byteArrayOutputStream.write(nextByte)
nextByte = inputStream.read()
}
val plaintext: ByteArray = byteArrayOutputStream.toByteArray()Java
// Although you can define your own key generation parameter specification, it's
// recommended that you use the value specified here.
Context context = getApplicationContext();
MasterKey mainKey = new MasterKey.Builder(context)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build();
String fileToRead = "my_sensitive_data.txt";
EncryptedFile encryptedFile = new EncryptedFile.Builder(context,
new File(DIRECTORY, fileToRead),
mainKey,
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
).build();
InputStream inputStream = encryptedFile.openFileInput();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int nextByte = inputStream.read();
while (nextByte != -1) {
byteArrayOutputStream.write(nextByte);
nextByte = inputStream.read();
}
byte[] plaintext = byteArrayOutputStream.toByteArray();

写入文件

以下代码段演示了如何使用 EncryptedFile 以更安全的方式写入文件内容:

Kotlin
// Although you can define your own key generation parameter specification, it's
// recommended that you use the value specified here.
val mainKey = MasterKey.Builder(applicationContext)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build()
// Create a file with this name, or replace an entire existing file
// that has the same name. Note that you cannot append to an existing file,
// and the file name cannot contain path separators.
val fileToWrite = "my_sensitive_data.txt"
val encryptedFile = EncryptedFile.Builder(
applicationContext,
File(DIRECTORY, fileToWrite),
mainKey,
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
).build()
val fileContent = "MY SUPER-SECRET INFORMATION"
.toByteArray(StandardCharsets.UTF_8)
encryptedFile.openFileOutput().apply {
write(fileContent)
flush()
close()
}Java
// Although you can define your own key generation parameter specification, it's
// recommended that you use the value specified here.
Context context = getApplicationContext();
MasterKey mainKey = new MasterKey.Builder(context)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build();
// Create a file with this name, or replace an entire existing file
// that has the same name. Note that you cannot append to an existing file,
// and the file name cannot contain path separators.
String fileToWrite = "my_sensitive_data.txt";
EncryptedFile encryptedFile = new EncryptedFile.Builder(context,
new File(DIRECTORY, fileToWrite),
mainKey,
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
).build();
byte[] fileContent = "MY SUPER-SECRET INFORMATION"
.getBytes(StandardCharsets.UTF_8);
OutputStream outputStream = encryptedFile.openFileOutput();
outputStream.write(fileContent);
outputStream.flush();
outputStream.close();

对于需要额外安全性的用例,请完成以下步骤:

修改共享偏好设置

以下代码段演示了如何使用 EncryptedSharedPreferences 以更安全的方式修改用户的一组共享偏好设置:

Kotlin
val sharedPrefsFile: String = FILE_NAME
val sharedPreferences: SharedPreferences = EncryptedSharedPreferences.create(
applicationContext,
sharedPrefsFile,
mainKey,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
with (sharedPreferences.edit()) {
// Edit the user's shared preferences...
apply()
}Java
String sharedPrefsFile = FILE_NAME;
SharedPreferences sharedPreferences = EncryptedSharedPreferences.create(
context,
sharedPrefsFile,
mainKey,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
);
SharedPreferences.Editor sharedPrefsEditor = sharedPreferences.edit();
// Edit the user's shared preferences...
sharedPrefsEditor.apply();