共用方式為


KeyProtection 類別

定義

在匯入 Android Keystore 系統時,如何保護密鑰或密鑰組的規格。

[Android.Runtime.Register("android/security/keystore/KeyProtection", ApiSince=23, DoNotGenerateAcw=true)]
public sealed class KeyProtection : Java.Lang.Object, IDisposable, Java.Interop.IJavaPeerable, 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 Keystore 系統時,如何保護密鑰或密鑰組的規格。 這個類別會指定已匯入密鑰的授權使用方式,例如使用者驗證是否需要使用密鑰、授權金鑰的作業(例如解密,但未簽署),以及具有哪些參數(例如,只有特定填補配置或摘要),以及密鑰的有效性開始和結束日期。 在此類別中表示的金鑰使用授權僅適用於秘密金鑰和私鑰 -- 公鑰可用於任何支援的作業。

若要將金鑰或金鑰組匯入 Android Keystore,請使用 Builder 建立這個類別的實例,並將 實例 java.security.KeyStore#setEntry(String, java.security.KeyStore.Entry, ProtectionParameter) KeyStore.setEntry 傳入並匯入金鑰或金鑰組。

若要從 Android Keystore 取得秘密/對稱或私鑰,請使用 java.security.KeyStore#getKey(String, char[]) KeyStore.getKey(String, null)java.security.KeyStore#getEntry(String, java.security.KeyStore.ProtectionParameter) KeyStore.getEntry(String, null)。 若要從 Android Keystore 取得公鑰,請使用 java.security.KeyStore#getCertificate(String)Certificate#getPublicKey()

為了協助取得儲存在 Android Keystore 中之金鑰組的演算法特定公用參數,其私鑰會實java.security.interfaces.ECKey作或介面,而其公鑰則實java.security.interfaces.ECPublicKey作 或 java.security.interfaces.RSAKeyjava.security.interfaces.RSAPublicKey 介面。

注意:無法存取儲存在Android Keystore 中的金鑰密鑰內容。

這個類別的實例是不可變的。

<h3>已知問題</h3> Android 6.0 中的已知錯誤(API 層級 23)會導致即使公鑰強制執行使用者驗證相關授權。 若要解決此問題,請擷取公鑰數據,以在 Android Keystore 外部使用。 例如:

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

<h3>範例:GCM 模式<中加密/解密的 AES 金鑰/h3> 本範例說明如何將 AES 金鑰匯入 Android KeyStore 的別名 key1 下,授權只用於 GCM 模式中沒有填補的加密/解密。 金鑰必須透過 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 金鑰組:此範例說明如何在別名 key2 下將 EC 金鑰組匯入 Android KeyStore,並將私鑰授權僅用於使用 SHA-256 或 SHA-512 摘要進行簽署。 公鑰的使用不受限制。 私鑰和公鑰必須分別透過 Key#getEncoded()PKCS#8X.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 填補</h3> 進行簽署/驗證的 RSA 金鑰組:此範例說明如何將 RSA 金鑰組匯入別名 key2 下的 Android KeyStore 中,並將私鑰授權僅用於使用 PKCS#1 簽章填補配置搭配 SHA-256 摘要進行簽署,且只有在使用者在過去十分鐘內通過驗證時才使用。 公鑰的使用不受限制(請參閱已知問題)。 私鑰和公鑰必須分別透過 Key#getEncoded()PKCS#8X.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 填補</h3> 進行加密/解密的 RSA 金鑰組:此範例說明如何在別名 key2 下將 RSA 金鑰組匯入 Android KeyStore,並將私鑰授權僅用於使用 PKCS#1 加密填補配置進行解密。 公鑰的使用不受限制,因此允許使用任何填補配置和摘要進行加密。 私鑰和公鑰必須分別透過 Key#getEncoded()PKCS#8X.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);
            ...
            }

android.security.keystore.KeyProtectionJava 檔。

此頁面的部分是根據 Android 開放原始碼專案所建立和共用的工作進行修改,並根據 Creative Commons 2.5 屬性授權中所述的詞彙使用。

屬性

Class

傳回這個 Object的運行時間類別。

(繼承來源 Object)
Handle

基礎Android實例的句柄。

(繼承來源 Object)
IsDigestsSpecified

true如果已指定索引鍵的摘要演算法集合,則傳回 。

IsInvalidatedByBiometricEnrollment

true如果在註冊新的生物特徵辨識或移除所有已註冊的生物特徵辨識時,金鑰無法回復失效,則傳回 。

IsRandomizedEncryptionRequired

true如果使用此金鑰進行加密必須足夠隨機化,才能每次產生相同純文字的不同加密文字,則傳回 。

IsUnlockedDeviceRequired

true如果螢幕必須解除鎖定,此金鑰才能用於解密或簽署,則傳回 。

IsUserAuthenticationRequired

true只有在使用者已通過驗證時,才會傳回密鑰是否獲得授權。

IsUserAuthenticationValidWhileOnBody

true如果裝置從用戶的主體移除,則會傳回密鑰是否已取消授權。

IsUserConfirmationRequired

true如果金鑰已獲授權只用於使用者確認的訊息,則傳回 。

IsUserPresenceRequired

true只有在和呼叫之間Signature.initSign()Signature.sign()已執行使用者目前狀態測試時,才會傳回 。

JniIdentityHashCode

在匯入 Android Keystore 系統時,如何保護密鑰或密鑰組的規格。

(繼承來源 Object)
JniPeerMembers

在匯入 Android Keystore 系統時,如何保護密鑰或密鑰組的規格。

KeyValidityForConsumptionEnd

取得時間瞬間,金鑰在解密和驗證之後沒有很長的有效時間。

KeyValidityForOriginationEnd

取得時間瞬間,金鑰在加密和簽署之後就不再有效。

KeyValidityStart

取得金鑰尚未有效的時間瞬間。

MaxUsageCount

傳回允許使用有限使用密鑰的次數上限,或 KeyProperties#UNRESTRICTED_USAGE_COUNT 如果沒有限制可以使用密鑰的次數限制。

PeerReference

在匯入 Android Keystore 系統時,如何保護密鑰或密鑰組的規格。

(繼承來源 Object)
Purposes

取得目的集 (e.

ThresholdClass

此 API 支援適用於 Android 的 Mono 基礎結構,並不適合直接從您的程式代碼使用。

(繼承來源 Object)
ThresholdType

此 API 支援適用於 Android 的 Mono 基礎結構,並不適合直接從您的程式代碼使用。

(繼承來源 Object)
UserAuthenticationType

在匯入 Android Keystore 系統時,如何保護密鑰或密鑰組的規格。

UserAuthenticationValidityDurationSeconds

取得成功驗證用戶之後,此密鑰已獲授權使用的時間持續時間(秒)。

方法

Clone()

建立並傳回這個 對象的複本。

(繼承來源 Object)
Dispose()

在匯入 Android Keystore 系統時,如何保護密鑰或密鑰組的規格。

(繼承來源 Object)
Dispose(Boolean)

在匯入 Android Keystore 系統時,如何保護密鑰或密鑰組的規格。

(繼承來源 Object)
Equals(Object)

指出其他物件是否「等於」這個物件。

(繼承來源 Object)
GetBlockModes()

取得區塊模式集 (e.

GetDigests()

取得摘要演算法集合 (e.

GetEncryptionPaddings()

取得填補設定集 (e.

GetHashCode()

傳回此物件的雜湊碼值。

(繼承來源 Object)
GetSignaturePaddings()

取得填補設定集 (e.

JavaFinalize()

當垃圾收集決定不再參考物件時,垃圾收集行程在 物件上呼叫。

(繼承來源 Object)
Notify()

喚醒正在等候此物件監視器的單一線程。

(繼承來源 Object)
NotifyAll()

喚醒正在等候此物件監視器的所有線程。

(繼承來源 Object)
SetHandle(IntPtr, JniHandleOwnership)

設定 Handle 屬性。

(繼承來源 Object)
ToArray<T>()

在匯入 Android Keystore 系統時,如何保護密鑰或密鑰組的規格。

(繼承來源 Object)
ToString()

傳回物件的字串表示。

(繼承來源 Object)
UnregisterFromRuntime()

在匯入 Android Keystore 系統時,如何保護密鑰或密鑰組的規格。

(繼承來源 Object)
Wait()

讓目前線程等候直到喚醒為止,通常是藉由em <notified/em>或<em>interrupted</em> 來喚醒它。<>

(繼承來源 Object)
Wait(Int64)

讓目前的線程等到喚醒為止,通常是因為 <em>notified</em> 或 <em>interrupted</em>,或直到經過一定數量的實時為止。

(繼承來源 Object)
Wait(Int64, Int32)

讓目前的線程等到喚醒為止,通常是因為 <em>notified</em> 或 <em>interrupted</em>,或直到經過一定數量的實時為止。

(繼承來源 Object)

明確介面實作

IJavaPeerable.Disposed()

在匯入 Android Keystore 系統時,如何保護密鑰或密鑰組的規格。

(繼承來源 Object)
IJavaPeerable.DisposeUnlessReferenced()

在匯入 Android Keystore 系統時,如何保護密鑰或密鑰組的規格。

(繼承來源 Object)
IJavaPeerable.Finalized()

在匯入 Android Keystore 系統時,如何保護密鑰或密鑰組的規格。

(繼承來源 Object)
IJavaPeerable.JniManagedPeerState

在匯入 Android Keystore 系統時,如何保護密鑰或密鑰組的規格。

(繼承來源 Object)
IJavaPeerable.SetJniIdentityHashCode(Int32)

在匯入 Android Keystore 系統時,如何保護密鑰或密鑰組的規格。

(繼承來源 Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates)

在匯入 Android Keystore 系統時,如何保護密鑰或密鑰組的規格。

(繼承來源 Object)
IJavaPeerable.SetPeerReference(JniObjectReference)

在匯入 Android Keystore 系統時,如何保護密鑰或密鑰組的規格。

(繼承來源 Object)

擴充方法

JavaCast<TResult>(IJavaObject)

執行 Android 執行時間檢查的類型轉換。

JavaCast<TResult>(IJavaObject)

在匯入 Android Keystore 系統時,如何保護密鑰或密鑰組的規格。

GetJniTypeName(IJavaPeerable)

在匯入 Android Keystore 系統時,如何保護密鑰或密鑰組的規格。

適用於