【问题标题】:KeyPairGeneratorSpec deprecatedKeyPairGeneratorSpec 已弃用
【发布时间】:2016-02-08 12:41:46
【问题描述】:

KeyPairGeneratorSpec 自 API 23 起已弃用。您如何处理此警告?

示例代码:

KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "AndroidKeyStore");
kpg.initialize(new KeyPairGeneratorSpec.Builder(context).build());

【问题讨论】:

  • 重点是我现在正在使用 Kotlin 进行开发,但这些警告仍在后面:D

标签: android security


【解决方案1】:

根据documentation,您应该改用 KeyGenParameterSpec。例如(对于 RSA 签名密钥):

KeyPairGenerator kpg = KeyPairGenerator.getInstance(
        KeyProperties.KEY_ALGORITHM_RSA, "AndroidKeyStore");
kpg.initialize(new KeyGenParameterSpec.Builder(
        "mykey", KeyProperties.PURPOSE_SIGN)
        .setDigests(KeyProperties.DIGEST_SHA256)
        .setSignaturePaddings(KeyProperties.SIGNATURE_PADDING_RSA_PSS)
        .build());

需要设置摘要和填充模式的附加选项。这是因为,遵循良好的加密安全实践,AndroidKeyStore 现在将密钥的使用方式(签名与解密、摘要和填充模式等)锁定到指定的集合。如果您尝试以创建时未指定的方式使用密钥,它将失败。此故障实际上是​​由安全硬件强制执行的,如果您的设备有它,因此即使攻击者启动设备,密钥仍然只能以定义的方式使用。

KeyGenParameterSpec 还支持创建 ECDSA、AES 和 HMAC 密钥,并允许您对密钥的使用方式设置其他限制。例如,如果您使用setUserAuthenticationRequired 方法,则除非用户在附近进行身份验证,否则无法使用密钥。

【讨论】:

  • 如果您的目标是早期版本的 android 怎么办?
  • @Oximer KeyGenParameterSpec Javadoc 中有示例。
  • @filthy_wizard 如果您的目标 SDK 级别较低,则必须使用 KeyPairGeneratorSpec。
【解决方案2】:

从 SDK 18 到 23 = KeyPairGeneratorSpec

SDK 23 和 以上 = KeyGenParameterSpec

@RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
            fun setAlgorithmParameterSpec(context: Context?) {
                val start = GregorianCalendar();
                val end = GregorianCalendar();
                end.add(Calendar.YEAR, 10);
                val spec: AlgorithmParameterSpec?
                if (Build.VERSION.SDK_INT < 23) {
                    spec = context?.let {
                        android.security.KeyPairGeneratorSpec.Builder(it)
                            // Alias - is a key for your KeyPair, to obtain it from Keystore in future.
                            .setAlias(alias ?: "")
                            // The subject used for the self-signed certificate of the generated pair
                            .setSubject(X500Principal("CN=$alias"))
                            // The serial number used for the self-signed certificate of the generated pair.
                            .setSerialNumber(BigInteger.valueOf(1337))
                            // Date range of validity for the generated pair.
                            .setStartDate(start.time).setEndDate(end.time)
                            .build()
                    };
                } else {
                    spec = KeyGenParameterSpec.Builder(alias ?: "", KeyProperties.PURPOSE_DECRYPT)
                        .setDigests(KeyProperties.DIGEST_SHA256, KeyProperties.DIGEST_SHA512)
                        .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1)
                        .build();
                }
            }

【讨论】:

    猜你喜欢
    • 2014-02-12
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-20
    • 1970-01-01
    相关资源
    最近更新 更多