来自密钥库的密钥的编码文本在 android M 中为空

encoded text of secretkey from keystore is null in android M

我需要实现 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();

当我从密钥库导入密钥的编码值时,它 returns 对我来说是空的。

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

我在 logcat.Please 中发现 key3.getEncoded() 的空值给我一些建议。

密钥库中生成的对称密钥在 Android M 中不可导出。因此它完全按照预期工作。您仍然可以使用带有 Cipher 到 encrypt/decrypt 的密钥,但您无法获取原始密钥字节。通常你需要两者之一。