代码之家  ›  专栏  ›  技术社区  ›  Archana vishwakarma

android M中密钥库中secretkey的编码文本为空

  •  4
  • Archana vishwakarma  · 技术社区  · 9 年前

    我需要实现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.getEncode()的空值。请给我一些建议。

    1 回复  |  直到 9 年前
        1
  •  8
  •   Nikolay Elenkov    9 年前

    密钥库中生成的对称密钥在Android M中是不可移植的。因此,它的工作原理与预期完全相同 Cipher 加密/解密,但无法获取原始密钥字节。通常情况下,您需要两者之一。