【发布时间】:2020-05-26 13:01:35
【问题描述】:
问题描述:
我正在开发一个将生成 AES 密钥并将其存储在密钥库中的 android 应用程序。
每当我需要向服务器发送数据时,我使用 AES 密钥对数据进行加密,使用服务器公钥对 AES 密钥进行加密,然后将加密数据 + 加密密钥发送到服务器以解密全部负载。
在android中生成Key的代码如下:
public void generateKey()
{
try {
if (!keyStore.containsAlias(KEY_ALIAS)) {
KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, AndroidKeyStore);
keyGenerator.init(
new KeyGenParameterSpec.Builder(KEY_ALIAS,
KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_GCM).setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
.setRandomizedEncryptionRequired(false)
.build());
keyGenerator.generateKey();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
当我需要加密数据时,我使用此函数获取密钥
private java.security.Key getSecretKey(Context context) throws Exception {
return keyStore.getKey(KEY_ALIAS, null);
}
使用这个密钥我能够加密数据。但问题是尝试加密密钥以将其发送到服务器。
我试图将密钥作为字节 [] 来加密它,但使用函数
key.getEncoded();
生成的字节数组始终为空。
这里出了什么问题以及如何解决?
应用适用于 Android 23+
【问题讨论】:
标签: java android encryption