4

我需要实现 256 位 AES 加密,我参考这个 - http://nelenkov.blogspot.jp/2015/06/keystore-redesign-in-android-m.html KeyGenerator 看起来像这样。

KeyGenerator keyGenerator = KeyGenerator
                    .getInstance(KeyProperties.KEY_ALGORITHM_AES,"AndroidKeyStore");
            KeyGenParameterSpec spec = new KeyGenParameterSpec.Builder(keyName,
                     KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)  

                     .setUserAuthenticationValidityDurationSeconds(5 *11160)
                     .build();
            keyGenerator.init(spec);
            SecretKey key1 =   keyGenerator.generateKey();

当我从密钥库导入密钥的编码值时,它返回 null 给我。

KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
               keyStore.load(null); 
SecretKey key3 = (SecretKey) keyStore.getKey(keyName, null);
Log.d("Test MM",key3.getEncoded()+",");

我在 logcat 中找到了 key3.getEncoded() 的空值。请给我一些建议。

4

1 回答 1

8

在密钥库中生成的对称密钥在 Android M 中是不可导出的。因此它的工作方式与预期完全一致。您仍然可以使用带有 a 的密钥Cipher来加密/解密,但您无法获取原始密钥字节。一般来说,你应该需要。

于 2015-08-21T17:25:38.903 回答