gpt4 book ai didi

java - 在 Android keystore 中存储 hmac key

转载 作者:太空宇宙 更新时间:2023-11-03 13:52:06 25 4
gpt4 key购买 nike

我正在使用以下代码创建一个 hmac key 并将其作为字符串返回。

KeyGenerator keyGen = null;
try {
keyGen = KeyGenerator.getInstance("HmacSHA256");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
SecretKey key = keyGen.generateKey();
byte[] encoded = key.getEncoded();
String s=Base64.encodeToString(encoded, Base64.DEFAULT);
Log.i("Hmac key before encrypt",s);

try {
KeyStore keystore = KeyStore.getInstance("AndroidKeyStore");
keystore.load(null, null);
KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry) keystore.getEntry("temp", null);
RSAPublicKey publicKey = (RSAPublicKey) privateKeyEntry.getCertificate().getPublicKey();

Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] cipherBytes = cipher.doFinal(encoded);

return Base64.encodeToString(cipherBytes,Base64.DEFAULT);


} catch (UnrecoverableEntryException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

如何将其存储在 android keystore 中?。我尝试使用以下代码:

KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);

KeyStore.ProtectionParameter param = new KeyStore.PasswordProtection("test".toCharArray());
keyStore.setEntry("key1",hmacKey,param);

无论 hmacKey 的格式是什么,我都会收到错误消息:String/Bytes 或 javax.crypto.SecretKey。以下是错误:如果传递 key hmacKey:

Wrong 2nd argument type. Found: 'java.security.Key', required: 'java.security.KeyStore.Entry'

在我传递字符串或字节数组的情况下也是如此。

如果我将参数类型转换为 java.security.KeyStore.Entry,它仍然不起作用。

这是正确的做法吗?任何人都可以指出如何使用别名将 HMAC key 存储在 keystore 中。如何将 hmack key 转换为 java.security.KeyStore.Entry 格式?

最佳答案

创建 Android keystore 是为了让您可以在您的应用程序代码之外使用非对称 key 和对称 key 。如指定in the training material :

Key material never enters the application process. When an application performs cryptographic operations using an Android Keystore key, behind the scenes plaintext, ciphertext, and messages to be signed or verified are fed to a system process which carries out the cryptographic operations. If the app's process is compromised, the attacker may be able to use the app's keys but will not be able to extract their key material (for example, to be used outside of the Android device).

因此,在应用程序代码内部生成 key 的想法 - 因此在 key 存储之外 - 不是一个好主意。 API for the KeyGenParameterSpec class 中为 HMAC key 定义了如何在 keystore 中生成 key 。 :

KeyGenerator keyGenerator = KeyGenerator.getInstance(
KeyProperties.KEY_ALGORITHM_HMAC_SHA256, "AndroidKeyStore");
keyGenerator.initialize(
new KeyGenParameterSpec.Builder("key2", KeyProperties.PURPOSE_SIGN).build());
SecretKey key = keyGenerator.generateKey();
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(key);
...

// The key can also be obtained from the Android Keystore any time as follows:
KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);
key = (SecretKey) keyStore.getKey("key2", null);

可以找到其他 key 类型in the KeyProperties class

关于java - 在 Android keystore 中存储 hmac key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33976356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com