0

Android KeyPairGeneratorSpecのインスタンスを作成する必要があります。以前はこのビルダークラスを使用して作成できましたが、API 23 で非推奨になりました。では、今作成する正しい方法は何ですか?

一般に、キー サイズを指定してKeyPairGeneratorSpecを作成する必要があります。今それを行う方法は?

4

1 に答える 1

0

KeyPairGeneratorSpecKeyGenParameterSpecを支持して廃止されました。

非推奨クラスの使用を避け、同時に後方互換性を維持したい場合は、2 つのコード パスを別々に記述する必要があるため、非推奨というKeyGenParameterSpec理由だけで必ずしも使用に移行するとは限りません。KeyPairGeneratorSpec

new を使用したコード例を次に示します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);
    }
}
于 2016-05-09T15:54:26.903 に答える