KeyGenParameterSpec 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
AlgorithmParameterSpec 用于初始化 KeyPairGenerator Android 密钥存储系统或 KeyGenerator Android 密钥存储系统。
[Android.Runtime.Register("android/security/keystore/KeyGenParameterSpec", ApiSince=23, DoNotGenerateAcw=true)]
public sealed class KeyGenParameterSpec : Java.Lang.Object, IDisposable, Java.Security.Spec.IAlgorithmParameterSpec
[<Android.Runtime.Register("android/security/keystore/KeyGenParameterSpec", ApiSince=23, DoNotGenerateAcw=true)>]
type KeyGenParameterSpec = class
inherit Object
interface IAlgorithmParameterSpec
interface IJavaObject
interface IDisposable
interface IJavaPeerable
- 继承
- 特性
- 实现
注解
AlgorithmParameterSpec用于初始化 KeyPairGeneratorAndroid 密钥存储系统或 KeyGenerator Android 密钥存储系统。 该规范确定密钥的授权使用,例如,使用密钥是否需要用户身份验证、授权的操作(例如签名或解密),以及具有哪些参数(例如,仅具有特定填充方案或摘要),以及密钥的有效性开始日期和结束日期。 规范中表示的密钥使用授权仅适用于密钥和私钥 -- 公钥可用于任何受支持的操作。
若要生成非对称密钥对或对称密钥,请使用Builder、初始化KeyPairGeneratorKeyGenerator所需密钥类型(例如,EC或 AES -- see KeyProperties.KEY_ALGORITHMconstants)从AndroidKeyStore提供程序使用KeyGenParameterSpec实例创建此类的实例,然后使用或或生成KeyGenerator#generateKey()密钥对KeyPairGenerator#generateKeyPair()。
生成的密钥对或密钥将由生成器返回,并且也存储在 Android 密钥存储中,该别名在此规范中指定的别名下。从 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接口。
对于非对称密钥对,还会生成 X.509 证书并将其存储在 Android 密钥存储中。 这是因为 java.security.KeyStore 抽象不支持在没有证书的情况下存储密钥对。 可以在此规范中自定义证书的主题、序列号和有效性日期。证书稍后可能会被证书颁发机构(CA)签名的证书替换。
注意:如果未使用 Builder#setAttestationChallenge(byte[])证明,则生成的证书可能是自签名的。 如果私钥无权对证书进行签名,则会使用无效的签名创建证书,该签名将不进行验证。 此类证书仍然很有用,因为它提供对公钥的访问权限。 若要为证书生成有效的签名,密钥需要针对以下所有项进行授权:<ul><li>KeyProperties#PURPOSE_SIGN,</li li><>操作无需对用户进行身份验证(请参阅Builder#setUserAuthenticationRequired(boolean)),</li><>li 签名/发源目前(见Builder#setKeyValidityStart(Date)和Builder#setKeyValidityForOriginationEnd(Date)),</li><>li 合适摘要,</li><>(仅 RSA 密钥)填充方案。KeyProperties#SIGNATURE_PADDING_RSA_PKCS1</li></ul>
注意:无法访问生成的对称密钥和私钥的密钥材料。 公钥的密钥材料是可访问的。
此类的实例是不可变的。
<h3>已知问题</h3> Android 6.0(API 级别 23)中的已知 bug 会导致即使对公钥强制实施与用户身份验证相关的授权。 若要解决此问题,请提取公钥材料,以在 Android 密钥存储外部使用。 例如:
{@code
PublicKey unrestrictedPublicKey =
KeyFactory.getInstance(publicKey.getAlgorithm()).generatePublic(
new X509EncodedKeySpec(publicKey.getEncoded()));
}
<h3>示例:NIST P-256 EC 密钥对,用于使用 ECDSA</h3> 进行签名/验证。此示例演示了如何在 Android KeyStore 系统中的别名 key1 下生成 NIST P-256r1(aka secp256r1 aka prime256v1)EC 密钥对,其中私钥被授权仅用于使用 SHA-256 进行签名, SHA-384 或 SHA-512 摘要,仅当用户在过去五分钟内进行身份验证时。 公钥的使用不受限制(请参阅已知问题)。
{@code
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(
KeyProperties.KEY_ALGORITHM_EC, "AndroidKeyStore");
keyPairGenerator.initialize(
new KeyGenParameterSpec.Builder(
"key1",
KeyProperties.PURPOSE_SIGN)
.setAlgorithmParameterSpec(new ECGenParameterSpec("secp256r1"))
.setDigests(KeyProperties.DIGEST_SHA256,
KeyProperties.DIGEST_SHA384,
KeyProperties.DIGEST_SHA512)
// Only permit the private key to be used if the user authenticated
// within the last five minutes.
.setUserAuthenticationRequired(true)
.setUserAuthenticationValidityDurationSeconds(5 * 60)
.build());
KeyPair keyPair = keyPairGenerator.generateKeyPair();
Signature signature = Signature.getInstance("SHA256withECDSA");
signature.initSign(keyPair.getPrivate());
...
// The key pair can also be obtained from the Android Keystore any time as follows:
KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);
PrivateKey privateKey = (PrivateKey) keyStore.getKey("key1", null);
PublicKey publicKey = keyStore.getCertificate("key1").getPublicKey();
}
<h3>示例:使用 RSA-PSS</h3> 进行签名/验证的 RSA 密钥对演示了如何在经过授权仅用于使用 SHA-256 或 SHA-512 摘要的 RSA-PSS 签名填充方案在 Android KeyStore 系统中 key1 生成 RSA 密钥对。 公钥的使用不受限制。
{@code
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(
KeyProperties.KEY_ALGORITHM_RSA, "AndroidKeyStore");
keyPairGenerator.initialize(
new KeyGenParameterSpec.Builder(
"key1",
KeyProperties.PURPOSE_SIGN)
.setDigests(KeyProperties.DIGEST_SHA256, KeyProperties.DIGEST_SHA512)
.setSignaturePaddings(KeyProperties.SIGNATURE_PADDING_RSA_PSS)
.build());
KeyPair keyPair = keyPairGenerator.generateKeyPair();
Signature signature = Signature.getInstance("SHA256withRSA/PSS");
signature.initSign(keyPair.getPrivate());
...
// The key pair can also be obtained from the Android Keystore any time as follows:
KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);
PrivateKey privateKey = (PrivateKey) keyStore.getKey("key1", null);
PublicKey publicKey = keyStore.getCertificate("key1").getPublicKey();
}
<h3>示例:使用 RSA OAEP</h3> 进行加密/解密的 RSA 密钥对演示了如何在具有 key1 SHA-256 或 SHA-512 摘要的 RSA 密钥存储系统中生成 RSA 密钥对,其中私钥仅用于使用 RSA OAEP 加密填充方案进行解密。 公钥的使用不受限制。
{@code
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(
KeyProperties.KEY_ALGORITHM_RSA, "AndroidKeyStore");
keyPairGenerator.initialize(
new KeyGenParameterSpec.Builder(
"key1",
KeyProperties.PURPOSE_DECRYPT)
.setDigests(KeyProperties.DIGEST_SHA256, KeyProperties.DIGEST_SHA512)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_OAEP)
.build());
KeyPair keyPair = keyPairGenerator.generateKeyPair();
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
cipher.init(Cipher.DECRYPT_MODE, keyPair.getPrivate());
...
// The key pair can also be obtained from the Android Keystore any time as follows:
KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);
PrivateKey privateKey = (PrivateKey) keyStore.getKey("key1", null);
PublicKey publicKey = keyStore.getCertificate("key1").getPublicKey();
}
<h3>示例:GCM 模式</h3> 中加密/解密的 AES 密钥演示了如何在经过授权仅用于 GCM 模式下的加密/解密的别名 key2 下的 Android KeyStore 系统中生成 AES 密钥,而无需填充。
{@code
KeyGenerator keyGenerator = KeyGenerator.getInstance(
KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
keyGenerator.init(
new KeyGenParameterSpec.Builder("key2",
KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_GCM)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
.build());
SecretKey key = keyGenerator.generateKey();
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key);
...
// The key can also be obtained from the Android Keystore any time as follows:
KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);
key = (SecretKey) keyStore.getKey("key2", null);
}
<h3>示例:用于使用 SHA-256</h3> 生成 MAC 的 HMAC 密钥此示例演示如何在经过授权仅用于使用 SHA-256 生成 HMAC 的别名 key2 下的 Android KeyStore 系统中生成 HMAC 密钥。
{@code
KeyGenerator keyGenerator = KeyGenerator.getInstance(
KeyProperties.KEY_ALGORITHM_HMAC_SHA256, "AndroidKeyStore");
keyGenerator.init(
new KeyGenParameterSpec.Builder("key2", KeyProperties.PURPOSE_SIGN).build());
SecretKey key = keyGenerator.generateKey();
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(key);
...
// The key can also be obtained from the Android Keystore any time as follows:
KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);
key = (SecretKey) keyStore.getKey("key2", null);
}
<h3 id=“example:ecdh”>示例:ECDH 密钥协议<的 EC 密钥/h3> 此示例演示如何生成椭圆曲线密钥对,用于使用 ECDH 密钥协议与另一方建立共享机密。
{@code
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(
KeyProperties.KEY_ALGORITHM_EC, "AndroidKeyStore");
keyPairGenerator.initialize(
new KeyGenParameterSpec.Builder(
"eckeypair",
KeyProperties.PURPOSE_AGREE_KEY)
.setAlgorithmParameterSpec(new ECGenParameterSpec("secp256r1"))
.build());
KeyPair myKeyPair = keyPairGenerator.generateKeyPair();
// Exchange public keys with server. A new ephemeral key MUST be used for every message.
PublicKey serverEphemeralPublicKey; // Ephemeral key received from server.
// Create a shared secret based on our private key and the other party's public key.
KeyAgreement keyAgreement = KeyAgreement.getInstance("ECDH", "AndroidKeyStore");
keyAgreement.init(myKeyPair.getPrivate());
keyAgreement.doPhase(serverEphemeralPublicKey, true);
byte[] sharedSecret = keyAgreement.generateSecret();
// sharedSecret cannot safely be used as a key yet. We must run it through a key derivation
// function with some other data: "salt" and "info". Salt is an optional random value,
// omitted in this example. It's good practice to include both public keys and any other
// key negotiation data in info. Here we use the public keys and a label that indicates
// messages encrypted with this key are coming from the server.
byte[] salt = {};
ByteArrayOutputStream info = new ByteArrayOutputStream();
info.write("ECDH secp256r1 AES-256-GCM-SIV\0".getBytes(StandardCharsets.UTF_8));
info.write(myKeyPair.getPublic().getEncoded());
info.write(serverEphemeralPublicKey.getEncoded());
// This example uses the Tink library and the HKDF key derivation function.
AesGcmSiv key = new AesGcmSiv(Hkdf.computeHkdf(
"HMACSHA256", sharedSecret, salt, info.toByteArray(), 32));
byte[] associatedData = {};
return key.decrypt(ciphertext, associatedData);
}
Java文档android.security.keystore.KeyGenParameterSpec。
本页的某些部分是根据 创建和共享的工作进行的修改,并根据 许可证中所述的条款使用。
属性
| 名称 | 说明 |
|---|---|
| AlgorithmParameterSpec |
返回将用于创建密钥的密钥算法, |
| AttestKeyAlias |
返回将用于对生成的密钥的证明证书进行签名的证明密钥的别名。 |
| CertificateNotAfter |
返回要放入 X.509 |
| CertificateNotBefore |
返回要放入 X.509 |
| CertificateSerialNumber |
返回要放入 |
| CertificateSubject |
返回要放入 |
| Class |
返回此 |
| Handle |
基础 Android 实例的句柄。 (继承自 Object) |
| IsDevicePropertiesAttestationIncluded |
返回 |
| IsDigestsSpecified |
返回 |
| IsInvalidatedByBiometricEnrollment |
返回 |
| IsMgf1DigestsSpecified |
如果 |
| IsRandomizedEncryptionRequired |
如果 |
| IsStrongBoxBacked |
返回 |
| IsUnlockedDeviceRequired |
如果 |
| IsUserAuthenticationRequired |
如果 |
| IsUserAuthenticationValidWhileOnBody |
返回 |
| IsUserConfirmationRequired |
如果 |
| IsUserPresenceRequired |
如果 |
| JniIdentityHashCode |
获取互操作运行时分配给此Java对等的标识哈希代码。 (继承自 Object) |
| JniManagedPeerState |
获取运行时上次分配的状态标志。 (继承自 JavaObject) |
| JniPeerMembers |
获取用于访问此Java绑定成员的 JNI 元数据。 |
| KeySize |
返回请求的密钥大小。 |
| KeystoreAlias |
返回将 |
| KeyValidityForConsumptionEnd |
返回密钥不再对解密和验证 |
| KeyValidityForOriginationEnd |
返回密钥不再对加密和签名 |
| KeyValidityStart |
返回密钥尚无效或 |
| MaxUsageCount |
返回允许使用有限使用密钥的最大次数,或者 |
| Mgf1Digests |
返回 MGF1 掩码生成函数(例如, |
| 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) |
| GetAttestationChallenge() |
返回将置于此密钥对的证明证书中的证明质询值。 |
| GetBlockModes() |
获取加密/解密时可以使用密钥的块模式集(例如 |
| GetDigests() |
返回摘要算法集(e. |
| GetEncryptionPaddings() |
返回一组填充方案(例如,, |
| GetHashCode() |
返回对象的哈希代码值。 (继承自 Object) |
| GetSignaturePaddings() |
获取在签名/验证时可以使用密钥的填充方案集(例如 |
| 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() |
将此对等方标记为已释放,并将 |
| IJavaPeerable.Finalized() |
将此对等方标记为已释放,并将 |
| IJavaPeerable.JniObjectReferenceControlBlock |
获取此对等方使用的本机控制块指针。 (继承自 JavaObject) |
| IJavaPeerable.SetJniIdentityHashCode(Int32) |
存储运行时提供的标识哈希代码。 (继承自 JavaObject) |
| IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) |
替换存储的托管对等状态标志。 (继承自 JavaObject) |
| IJavaPeerable.SetPeerReference(JniObjectReference) |
更新对等引用,并在引用无效时释放已释放的对等方控制块。 (继承自 JavaObject) |
| IJavaPeerable.UnregisterFromRuntime() |
从运行时的Java对象查找中删除此实例。 |
扩展方法
| 名称 | 说明 |
|---|---|
| GetJniTypeName(IJavaPeerable) |
获取实例 |
| JavaAs<TResult>(IJavaPeerable) |
尝试强制 |
| JavaCast<TResult>(IJavaObject) |
执行 Android 运行时检查的类型转换。 |
| JavaCast<TResult>(IJavaObject) |
重复使用兼容的托管对等,或获取请求类型的包装器。 |
| TryJavaCast<TResult>(IJavaPeerable, TResult) |
尝试强制 |