Android Keymaster 是一种用于加密和解密数据的 Android API,它可以在设备的安全硬件中生成、存储和管理密钥
dependencies {
implementation 'androidx.security:security-keymaster-core:3.0.0'
}
import androidx.security.keymaster.KeymasterManager;
import androidx.security.keymaster.KeyProperties;
public class MyApplication extends Application {
private KeymasterManager keymasterManager;
@Override
public void onCreate() {
super.onCreate();
keymasterManager = (KeymasterManager) getSystemService(Context.KEYMASTER_SERVICE);
}
}
import androidx.security.keymaster.KeyGenParameterSpec;
import androidx.security.keymaster.KeyProperties;
private void generateKey() throws Exception {
KeyGenParameterSpec keyGenParameterSpec = new KeyGenParameterSpec.Builder(
"myKeyAlias",
KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
.setDigests(KeyProperties.DIGEST_SHA256, KeyProperties.DIGEST_SHA512)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
.setUserAuthenticationRequired(true)
.build();
KeymasterManager keymasterManager = (KeymasterManager) getSystemService(Context.KEYMASTER_SERVICE);
keymasterManager.keyGen(keyGenParameterSpec, null);
}
extractKey
方法从 Keymaster 模块中提取密钥。为了提取密钥,您需要知道密钥的别名和密钥的派生参数。import androidx.security.keymaster.KeymasterKey;
private KeymasterKey extractKey() throws Exception {
KeymasterManager keymasterManager = (KeymasterManager) getSystemService(Context.KEYMASTER_SERVICE);
KeyProperties keyProperties = new KeyProperties.Builder()
.setAlias("myKeyAlias")
.setDigests(KeyProperties.DIGEST_SHA256, KeyProperties.DIGEST_SHA512)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
.setUserAuthenticationRequired(true)
.build();
return keymasterManager.extractKey(null, keyProperties);
}
import androidx.security.crypto.EncryptedSharedPreferences;
import androidx.security.crypto.MasterKeys;
private String encryptData(String data, KeymasterKey keymasterKey) throws Exception {
EncryptedSharedPreferences encryptedSharedPreferences = EncryptedSharedPreferences.create(
"myEncryptedPreferences",
MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC),
getApplicationContext(),
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM);
SharedPreferences.Editor editor = encryptedSharedPreferences.edit();
editor.putString("myKeyAlias", keymasterKey.getKey());
editor.apply();
return encryptedSharedPreferences.getString("myKeyAlias", null);
}
private String decryptData(String encryptedData, KeymasterKey keymasterKey) throws Exception {
EncryptedSharedPreferences encryptedSharedPreferences = EncryptedSharedPreferences.create(
"myEncryptedPreferences",
MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC),
getApplicationContext(),
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM);
String decryptedData = encryptedSharedPreferences.getString("myKeyAlias", null);
if (decryptedData != null) {
return decryptedData;
} else {
byte[] encryptedBytes = encryptedSharedPreferences.getString("myKeyAlias", null).getBytes();
return new String(encryptedBytes);
}
}
请注意,这个示例仅用于演示目的,实际应用中可能需要根据具体需求进行调整。在使用 Keymaster 时,请确保遵循最佳安全实践,以保护您的应用程序和用户数据的安全。