我在我的项目中使用Google/Tink的确定性对称密钥加密。就像这样-
byte[] ciphertext;
Context context = getApplicationContext();
String plainText="Hello World";
try {
DeterministicAeadConfig.register();
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
try {
KeysetHandle keysetHandle = KeysetHandle.generateNew(
KeyTemplates.get("AES256_SIV"));
Log.d("TAG",keysetHandle.toString());
DeterministicAead daead =
keysetHandle.getPrimitive(DeterministicAead.class);
ciphertext = daead.encryptDeterministically(plainText.getBytes(),null);
String c= new String(Base64.getEncoder().encodeToString(ciphertext));
Log.d("TAG",c);
MasterKey mainKey = new MasterKey.Builder(context)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build();
Log.d("TAG",mainKey.toString());
String filePath = Environment.getExternalStorageDirectory() + "/my_keyset.json";
String masterKeyUri = "android-keystore://_androidx_security_master_key_";
keysetHandle.write(JsonKeysetWriter.withFile(new File(filePath)),
new AndroidKeystoreKmsClient().getAead(masterKeyUri));
} catch (GeneralSecurityException | IOException e) {
e.printStackTrace();
}一切都很好。现在我正在为Android keyStore创建哪个主键,如果用户重置手机或发生任何其他事故(其他原因),可以删除/丢失。那么丁克的密钥集(键)将不可用.是否有任何方法来保持主密钥的备份或从用户输入创建主密钥或任何其他解决方案?
注意: AWS KMS或GCP KMS不是我的解决方案。作为密码学的新手,任何建议或建议都将受到欢迎。
发布于 2021-12-05 04:06:14
我第一次遇到Tink库是使用我的应用程序来解码我们合资伙伴IP的一个受保护的SNMP SMI树(我们的密文),然后才能用于SNMP操作。我发现我需要为相同的密文保留相同的主密钥。
因此,每次我的应用程序打开时执行KeysetHandle.generateNew()方法不再是一个选项,因为它生成一个新的主密钥,它不再与存储在应用程序私有文件夹中的现有密文一起工作。
我发现Tink库允许我们以我们选择的密钥集文件格式保存主键,然后我们可以在我们的应用程序中使用它来为应用程序的密钥集句柄重新加载同一个主键。
String keysetFileName =
StorageUtils.getPrivateKeyPath()
+ File.separator + "smi.json";
//write it as a file
CleartextKeysetHandle
.write(keysetHandle, JsonKeysetWriter.withFile(new File(keysetFileName)));要将其重新加载到密钥集句柄中,我们使用以下代码:
String keysetFileName =
StorageUtils.getPrivateKeyPath()
+ File.separator + "smi.json";
//read it as a file
keysetHandle =
CleartextKeysetHandle.read(JsonKeysetReader.withFile(new File(keysetFileName)));请记住,我实际上并没有将密钥集文件存储在设备存储中。我使用EncryptedSharedPreferences脚本将密钥集文件的内容复制到应用程序的build.gradle对象中,并以字符串的形式检索它,如下所示:
// onCreate()
securedSharedPreferences =
StorageUtils.getEncryptedSharedPreferences(context);
securedSharedPreferences.edit()
.putString("key", BuildConfig.SECRET_KEY).apply();
// somewhere else when we need it...
String keysetFileName =
securedSharedPreferences.getString("key", null);
//read it as a string
keysetHandle =
CleartextKeysetHandle.read(JsonKeysetReader.withString(keysetFileName));上述方法仍然不安全,因为我们可能在代码中公开BuildConfig.SECRET KEY。处理此问题的理想方法是要求用户加载密钥集文件并将其保存在安全的EncryptedSharedPreferences对象中。
https://stackoverflow.com/questions/69442684
复制相似问题