语言

KeyProtection 类

定义

指定在导入 Android 密钥存储系统时如何保护密钥或密钥对。

[Android.Runtime.Register("android/security/keystore/KeyProtection", ApiSince=23, DoNotGenerateAcw=true)]
public sealed class KeyProtection : Java.Lang.Object, IDisposable, Java.Security.KeyStore.IProtectionParameter
[<Android.Runtime.Register("android/security/keystore/KeyProtection", ApiSince=23, DoNotGenerateAcw=true)>]
type KeyProtection = class
    inherit Object
    interface KeyStore.IProtectionParameter
    interface IJavaObject
    interface IDisposable
    interface IJavaPeerable
继承
KeyProtection
特性
实现

注解

指定在导入 Android 密钥存储系统时如何保护密钥或密钥对。 此类指定已导入密钥的授权使用,例如用户身份验证是使用密钥所必需的,密钥是授权哪些操作(例如解密,但未签名),以及具有哪些参数(例如,仅具有特定填充方案或摘要),以及密钥的有效性开始日期和结束日期。 此类中表示的密钥使用授权仅适用于密钥和私钥 -- 公钥可用于任何受支持的操作。

若要将密钥或密钥对导入到 Android 密钥存储中,请使用 Builder 此类的实例并使用导入的密钥或密钥对来创建 java.security.KeyStore#setEntry(String, java.security.KeyStore.Entry, ProtectionParameter) KeyStore.setEntry 此类的实例。

若要从 Android 密钥存储获取机密/对称或私钥,请使用 java.security.KeyStore#getKey(String, char[]) KeyStore.getKey(String, null) 或 java.security.KeyStore#getEntry(String, java.security.KeyStore.ProtectionParameter) KeyStore.getEntry(String, null)。 从 Android 密钥存储使用 java.security.KeyStore#getCertificate(String) 中获取公钥,然后 Certificate#getPublicKey()获取公钥。

为了帮助获取存储在 Android 密钥存储中的密钥对的特定于算法的公共参数,其私钥实现java.security.interfaces.ECKey或接口,而其公钥实现java.security.interfaces.RSAKey或java.security.interfaces.ECPublicKeyjava.security.interfaces.RSAPublicKey接口。

注意:Android 密钥存储中存储的密钥的密钥材料不可访问。

此类的实例是不可变的。

<h3>已知问题</h3> Android 6.0(API 级别 23)中的已知 bug 会导致即使对公钥强制实施与用户身份验证相关的授权。 若要解决此问题,请提取公钥材料,以在 Android 密钥存储外部使用。 例如:

{@code
            PublicKey unrestrictedPublicKey =
                    KeyFactory.getInstance(publicKey.getAlgorithm()).generatePublic(
                            new X509EncodedKeySpec(publicKey.getEncoded()));
            }

<h3>示例:GCM 模式</h3 中用于加密/解密的 AES 密钥/h3> 演示了如何在授权仅用于 GCM 模式下的加密/解密的别名 key1 下将 AES 密钥导入 Android KeyStore,而无需填充。 密钥必须以Key#getEncoded()RAW格式导出其密钥材料。

{@code
            SecretKey key = ...; // AES key

            KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
            keyStore.load(null);
            keyStore.setEntry(
                    "key1",
                    new KeyStore.SecretKeyEntry(key),
                    new KeyProtection.Builder(KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
                            .setBlockMode(KeyProperties.BLOCK_MODE_GCM)
                            .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
                            .build());
            // Key imported, obtain a reference to it.
            SecretKey keyStoreKey = (SecretKey) keyStore.getKey("key1", null);
            // The original key can now be discarded.

            Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
            cipher.init(Cipher.ENCRYPT_MODE, keyStoreKey);
            ...
            }

<h3>示例:用于使用 SHA-512/h3> 生成 MAC 的 HMAC 密钥此示例演示如何在授权仅用于使用 SHA-512< 摘要生成 MAC 的别名key1下将 HMAC 密钥导入 Android KeyStore。 密钥必须以Key#getEncoded()RAW格式导出其密钥材料。

{@code
            SecretKey key = ...; // HMAC key of algorithm "HmacSHA512".

            KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
            keyStore.load(null);
            keyStore.setEntry(
                    "key1",
                    new KeyStore.SecretKeyEntry(key),
                    new KeyProtection.Builder(KeyProperties.PURPOSE_SIGN).build());
            // Key imported, obtain a reference to it.
            SecretKey keyStoreKey = (SecretKey) keyStore.getKey("key1", null);
            // The original key can now be discarded.

            Mac mac = Mac.getInstance("HmacSHA512");
            mac.init(keyStoreKey);
            ...
            }

<h3>示例:使用 ECDSA</h3> 进行签名/验证的 EC 密钥对演示了如何使用 SHA-256 或 SHA-512 摘要将 EC 密钥对导入到别名 key2 下的 Android KeyStore 中。 公钥的使用不受限制。 私钥和公钥必须分别通过Key#getEncoded()PKCS#8和X.509格式导出其密钥材料。

{@code
            PrivateKey privateKey = ...;   // EC private key
            Certificate[] certChain = ...; // Certificate chain with the first certificate
                                           // containing the corresponding EC public key.

            KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
            keyStore.load(null);
            keyStore.setEntry(
                    "key2",
                    new KeyStore.PrivateKeyEntry(privateKey, certChain),
                    new KeyProtection.Builder(KeyProperties.PURPOSE_SIGN)
                            .setDigests(KeyProperties.DIGEST_SHA256, KeyProperties.DIGEST_SHA512)
                            .build());
            // Key pair imported, obtain a reference to it.
            PrivateKey keyStorePrivateKey = (PrivateKey) keyStore.getKey("key2", null);
            PublicKey publicKey = keyStore.getCertificate("key2").getPublicKey();
            // The original private key can now be discarded.

            Signature signature = Signature.getInstance("SHA256withECDSA");
            signature.initSign(keyStorePrivateKey);
            ...
            }

<h3>示例:使用 PKCS#1 填充<进行签名/验证的 RSA 密钥对/h3> 演示了如何将 RSA 密钥对导入到别名 key2 下的 Android KeyStore 中,该密钥对的别名授权仅用于使用具有 SHA-256 摘要的 PKCS#1 签名填充方案进行签名,并且仅当用户在过去 10 分钟内进行身份验证。 公钥的使用不受限制(请参阅已知问题)。 私钥和公钥必须分别通过Key#getEncoded()PKCS#8和X.509格式导出其密钥材料。

{@code
            PrivateKey privateKey = ...;   // RSA private key
            Certificate[] certChain = ...; // Certificate chain with the first certificate
                                           // containing the corresponding RSA public key.

            KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
            keyStore.load(null);
            keyStore.setEntry(
                    "key2",
                    new KeyStore.PrivateKeyEntry(privateKey, certChain),
                    new KeyProtection.Builder(KeyProperties.PURPOSE_SIGN)
                            .setDigests(KeyProperties.DIGEST_SHA256)
                            .setSignaturePaddings(KeyProperties.SIGNATURE_PADDING_RSA_PKCS1)
                            // Only permit this key to be used if the user
                            // authenticated within the last ten minutes.
                            .setUserAuthenticationRequired(true)
                            .setUserAuthenticationValidityDurationSeconds(10 * 60)
                            .build());
            // Key pair imported, obtain a reference to it.
            PrivateKey keyStorePrivateKey = (PrivateKey) keyStore.getKey("key2", null);
            PublicKey publicKey = keyStore.getCertificate("key2").getPublicKey();
            // The original private key can now be discarded.

            Signature signature = Signature.getInstance("SHA256withRSA");
            signature.initSign(keyStorePrivateKey);
            ...
            }

<h3>示例:使用 PKCS#1 填充<进行加密/解密的 RSA 密钥对/h3> 此示例演示了如何使用 PKCS#1 加密填充方案将 RSA 密钥对导入到别名 key2 下的 Android KeyStore 中,私钥仅用于解密。 公钥的使用不受限制,因此允许使用任何填充方案和摘要进行加密。 私钥和公钥必须分别通过Key#getEncoded()PKCS#8和X.509格式导出其密钥材料。

{@code
            PrivateKey privateKey = ...;   // RSA private key
            Certificate[] certChain = ...; // Certificate chain with the first certificate
                                           // containing the corresponding RSA public key.

            KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
            keyStore.load(null);
            keyStore.setEntry(
                    "key2",
                    new KeyStore.PrivateKeyEntry(privateKey, certChain),
                    new KeyProtection.Builder(KeyProperties.PURPOSE_DECRYPT)
                            .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1)
                            .build());
            // Key pair imported, obtain a reference to it.
            PrivateKey keyStorePrivateKey = (PrivateKey) keyStore.getKey("key2", null);
            PublicKey publicKey = keyStore.getCertificate("key2").getPublicKey();
            // The original private key can now be discarded.

            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            cipher.init(Cipher.DECRYPT_MODE, keyStorePrivateKey);
            ...
            }

Java文档android.security.keystore.KeyProtection。

本页的某些部分是根据 创建和共享的工作进行的修改,并根据 许可证中所述的条款使用。

属性

名称 说明
Class

返回此 Object的运行时类。

(继承自 Object)
Handle

基础 Android 实例的句柄。

(继承自 Object)
IsDigestsSpecified

返回 true 是否可以使用密钥的摘要算法集。

IsInvalidatedByBiometricEnrollment

返回 true 在注册新的生物识别或删除所有已注册生物识别时,密钥是否不可逆转地失效。

IsMgf1DigestsSpecified

如果 true 指定了可以使用密钥的 MGF1 掩码生成函数的摘要集,则返回。

IsRandomizedEncryptionRequired

如果 true 使用此密钥的加密必须足够随机化,以便每次为同一纯文本生成不同的密码文本,则返回。

IsUnlockedDeviceRequired

如果 true 仅当设备解锁时,才授权使用密钥,则返回。

IsUserAuthenticationRequired

如果 true 仅当用户已经过身份验证时,才授权使用该密钥。

IsUserAuthenticationValidWhileOnBody

当 true 从用户的正文中删除设备时,是否将取消授权密钥。

IsUserConfirmationRequired

如果 true 授权密钥仅用于用户确认的消息,则返回。

IsUserPresenceRequired

如果true仅当在和Signature.initSign()调用之间Signature.sign()执行用户状态测试时,才授权使用该密钥。

JniIdentityHashCode

获取互操作运行时分配给此Java对等的标识哈希代码。

(继承自 Object)
JniManagedPeerState

获取运行时上次分配的状态标志。

(继承自 JavaObject)
JniPeerMembers

获取用于访问此Java绑定成员的 JNI 元数据。

KeyValidityForConsumptionEnd

获取密钥在解密和验证后不再有效的时间。

KeyValidityForOriginationEnd

获取密钥对加密和签名无效的时间。

KeyValidityStart

获取密钥尚无效之前的时间。

MaxUsageCount

返回允许使用有限使用密钥的最大次数,或者 KeyProperties#UNRESTRICTED_USAGE_COUNT 对可以使用密钥的次数没有限制。

Mgf1Digests

返回 MGF1 掩码生成函数(例如, SHA-256) SHA-384可以使用键使用的摘要集。

PeerReference

获取此Java对等方的 JNI 对象引用。

(继承自 Object)
Purposes

获取可以使用密钥的用途集(例如加密、解密、签名)。

ThresholdClass

此 API 支持 Mono for Android 基础结构,不打算直接从代码使用。

(继承自 Object)
ThresholdType

此 API 支持 Mono for Android 基础结构,不打算直接从代码使用。

(继承自 Object)
UserAuthenticationType

获取可授权使用密钥的身份验证类型集。

UserAuthenticationValidityDurationSeconds

获取在用户成功进行身份验证后授权使用此密钥的时间(秒)。。

方法

名称 说明
Clone()

创建并返回此对象的副本。

(继承自 Object)
Construct(JniObjectReference, JniObjectReferenceOptions)

将此对等方的构造委托给运行时的值管理器。

(继承自 JavaObject)
Dispose()

释放此Java对等方持有的资源。

(继承自 Object)
Dispose(Boolean)

为子类提供处置挂钩。 此实现不执行任何操作。

(继承自 Object)
DisposeUnlessReferenced()

要求值管理器释放此对等方,除非其映射保留此相同的托管实例。

(继承自 JavaObject)
Equals(Object)

测试另一个托管对等方是否引用同一Java对象。

(继承自 JavaObject)
Equals(Object)

指示其他对象是否“等于”此对象。

(继承自 Object)
GetBlockModes()

获取加密/解密时可以使用密钥的块模式集(例如 GCM, CBC)。

GetDigests()

获取可以使用密钥的摘要算法集(例如 SHA-256, SHA-384)。

GetEncryptionPaddings()

获取一组填充方案(例如 PKCS7Padding, PKCS1Padding), NoPadding在加密/解密时可以使用密钥。

GetHashCode()

返回对象的哈希代码值。

(继承自 Object)
GetSignaturePaddings()

获取在签名/验证时可以使用密钥的填充方案集(例如 PSS, PKCS#1)。

JavaFinalize()
已过时.

当垃圾回收确定不再引用该对象时,由对象上的垃圾回收器调用。

(继承自 Object)
Notify()

唤醒正在等待此对象的监视器的单个线程。

(继承自 Object)
NotifyAll()

唤醒正在等待此对象的监视器的所有线程。

(继承自 Object)
SetHandle(IntPtr, JniHandleOwnership)

设置 Handle 属性。

(继承自 Object)
SetPeerReference(JniObjectReference, JniObjectReferenceOptions)

在不创建 JNI 引用副本的情况下,将引用值存储在控制块中。

(继承自 JavaObject)
ToArray<T>()

从此Java数组包装器创建托管数组。

(继承自 Object)
ToString()

返回对象的字符串表示形式。

(继承自 Object)
UnregisterFromRuntime()

从互操作运行时取消注册此Java对等。

(继承自 Object)
Wait()

导致当前线程等待,直到它被唤醒,通常是通过 通知 或 中断。

(继承自 Object)
Wait(Int64, Int32)

使当前线程等待直到唤醒,通常是通过 通知 或 中断,或直到经过一定数量的实时。

(继承自 Object)
Wait(Int64)

使当前线程等待直到唤醒,通常是通过 通知 或 中断,或直到经过一定数量的实时。

(继承自 Object)

显式接口实现

名称 说明
IJavaPeerable.Disposed()

将此对等方标记为已释放,并将disposing调用Dispose设置为 true.

(继承自 JavaObject)
IJavaPeerable.Finalized()

将此对等方标记为已释放,并将disposing调用Dispose设置为 false.

(继承自 JavaObject)
IJavaPeerable.JniObjectReferenceControlBlock

获取此对等方使用的本机控制块指针。

(继承自 JavaObject)
IJavaPeerable.SetJniIdentityHashCode(Int32)

存储运行时提供的标识哈希代码。

(继承自 JavaObject)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates)

替换存储的托管对等状态标志。

(继承自 JavaObject)
IJavaPeerable.SetPeerReference(JniObjectReference)

更新对等引用,并在引用无效时释放已释放的对等方控制块。

(继承自 JavaObject)
IJavaPeerable.UnregisterFromRuntime()

从运行时的Java对象查找中删除此实例。

扩展方法

名称 说明
GetJniTypeName(IJavaPeerable)

获取实例 self类型的 JNI 名称。

JavaAs<TResult>(IJavaPeerable)

尝试强制self键入TResult,检查强制是否在Java端有效。

JavaCast<TResult>(IJavaObject)

执行 Android 运行时检查的类型转换。

JavaCast<TResult>(IJavaObject)

重复使用兼容的托管对等,或获取请求类型的包装器。

TryJavaCast<TResult>(IJavaPeerable, TResult)

尝试强制self键入TResult,检查强制是否在Java端有效。

适用于