2016-05-09 15 views

答えて

0

KeyPairGeneratorSpecは、KeyGenParameterSpecを推奨していません。

あなたは推奨されないクラスを使用しないようしたい場合は2のための別々のコードパスを記述する必要があり、同時にバックの互換性を維持したいので、私は必ずしも、KeyPairGeneratorSpecが推奨されていませんという理由だけでKeyGenParameterSpecを使用して上に移動しません。ここで

は(hereから)新しいKeyGenParameterSpecを使用して、いくつかのサンプルコードです:

/** 
* Creates a symmetric key in the Android Key Store which can only be used after the user has 
* authenticated with fingerprint. 
*/ 
public void createKey() { 
    // The enrolling flow for fingerprint. This is where you ask the user to set up fingerprint 
    // for your flow. Use of keys is necessary if you need to know if the set of 
    // enrolled fingerprints has changed. 
    try { 
     // Set the alias of the entry in Android KeyStore where the key will appear 
     // and the constrains (purposes) in the constructor of the Builder 
     mKeyGenerator = KeyGenerator.getInstance(
       KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore"); 
     mKeyGenerator.init(new KeyGenParameterSpec.Builder(KEY_NAME, 
       KeyProperties.PURPOSE_ENCRYPT | 
         KeyProperties.PURPOSE_DECRYPT) 
       .setBlockModes(KeyProperties.BLOCK_MODE_CBC) 
         // Require the user to authenticate with a fingerprint to authorize every use 
         // of the key 
       .setUserAuthenticationRequired(true) 
       .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7) 
       .build()); 
     mKeyGenerator.generateKey(); 
    } catch (NoSuchProviderException | NoSuchAlgorithmException | InvalidAlgorithmParameterException e) { 
     throw new RuntimeException(e); 
    } 
} 
+0

こんにちは、おかげで、私は明日試してみるだろう、あなたの答えを受け入れる:) –

+0

あなたは、私が「言う私はなぜ理解していませんKeyPairGeneratorSpecが推奨されていないため、必ずしもKeyGenParameterSpecを使用する必要はありません」、推奨されていない場合、新しいOS(api 23+)用に別のコードパスを持つ必要がありません。これはAndroid 6.0以降では使用できません。 –

+2

@ Leem.fin、no、 'deprecated'はそれが利用できないということを意味するものではありません - それを使用することをお勧めします(https://en.wikipedia.org/wiki/Deprecation)。別の(より良い)方法を使用してください。彼らは将来のバージョンでそれをうまく取り除くかもしれませんが、 'deprecated'は開発者がそれをやめるのをやめさせるヒントです。ベストプラクティスに従うならば、 'deprecated'クラスの使用を止めようとするべきです。そうしないと、あなたのターゲットsdkを将来更新し、もはやサポートされなくなるかもしれないからです。 – roarster

関連する問題