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 時,請確保遵循最佳安全實踐,以保護您的應用程序和用戶數據的安全。