語言

Cipher 類別

定義

此類別提供加密與解密的密碼學密碼功能。

[Android.Runtime.Register("javax/crypto/Cipher", DoNotGenerateAcw=true)]
public class Cipher : Java.Lang.Object
[<Android.Runtime.Register("javax/crypto/Cipher", DoNotGenerateAcw=true)>]
type Cipher = class
    inherit Object
繼承
衍生
屬性

備註

此類別提供加密與解密的密碼學密碼功能。 它構成了 Java 密碼擴展(JCE)框架的核心。

為了建立密碼物件,應用程式會呼叫該密碼 getInstance 的方法,並將所請求 的轉換 名稱傳給它。 可選擇性地指定提供者名稱。

轉換是描述對給定輸入要執行的操作(或一組操作)以產生某個輸出的字串。 轉換過程總是包含密碼演算法名稱(例如 DES),之後可能接著反饋模式和填充方案。

變換形式為:

<ul><li>「演算法/模式/填充」或

<li>“algorithm” </ul>

(在後者情況下,模式與填充方案使用提供者專屬的預設值。) 例如,以下是一個有效的轉換:

{@code
                Cipher c = Cipher.getInstance("DES/CBC/PKCS5Padding");
            }

利用 和 CFB等模式OFB,區塊密碼可以以比實際區塊大小更小的單位加密資料。 在請求此類模式時,您可以選擇性地在模式名稱DES/CFB8/NoPadding中附加該數字,如「」和「DES/OFB32/PKCS5Padding」轉換所示,指定每次要處理的位元數。 若未指定此號碼,則使用提供者專屬的預設值。 (例如,SunJCE 供應商對 DES 預設為 64 位元。)因此,區塊加密器可以透過使用 8 位元模式(如 CFB8 或 OFB8)轉換成位元組導向串流密碼。

像是認證加密與相關資料(AEAD)等模式,能為機密資料及未加密的附加關聯資料(AAD)提供真實性保證。 (有關 AEAD 及 AEAD 演算法如 GCM/CCM 的更多資訊,請參閱 RFC 5116 。)計算認證標籤時,可同時使用機密資料與 AAD 資料(類似於 Mac)。 此標籤在加密時附加於密文上,並在解密時進行驗證。

AEAD 模式如 GCM/CCM 會在開始密文真實性計算前,先完成所有 AAD 的真實性計算。 為避免實作必須內部緩衝密文,所有 AAD 資料必須先提供給 GCM/CCM 實作(透過updateAAD方法)<>b,然後才<>處理(透過 update and doFinal 方法)。

請注意,GCM 模式對於用於加密的 IV 具有唯一性要求。 當 IV 重複用於 GCM 加密時,這類使用會受到偽造攻擊。 因此,每次使用 GCM 模式進行加密操作後,呼叫者應以 GCM 參數重新初始化密碼物件,該參數具有不同的 IV 值。

GCMParameterSpec s = ...;
                cipher.init(..., s);

                // If the GCM parameters were generated by the provider, it can
                // be retrieved by:
                // cipher.getParameters().getParameterSpec(GCMParameterSpec.class);

                cipher.updateAAD(...);  // AAD
                cipher.update(...);     // Multi-part update
                cipher.doFinal(...);    // conclusion of operation

                // Use a different IV value for every encryption
                byte[] newIv = ...;
                s = new GCMParameterSpec(s.getTLen(), newIv);
                cipher.init(..., s);
                ...

Android 提供以下Cipher轉換功能:<table><thead><tr><演算法></th><模式></th><th>填充/<th><th>支援的 API 等級</th><筆記></th></tr></thead><tbody><tr><td rowspan=“3”><span style=“white-space: nowrap”AES></span></td><td span><style=“空白:不包裹”CBC>/span<br><span style=“white-space: nowrap”><CFB>/span<br><span style=“white-space: nowrap”><CTR/span>br<span style=“white-space: nowrap”CTS></span br span style=“white-space: nowrap”><CTS>/span<br><span style=“white-space: nowrap”ECB></span>br span<style=“white-space: nowrap”><><OFB>/span</td><>td td<span style=“white-space: nowrap”><ISO10126Padding></span><br><span style=“white-space: nowrap”>noPadding/<span><br><span style=“white-space: nowrap”>PKCS5Padding</span></td td><><span style=“white-space: nowrap”>1+</span></td><td></><td/tr><tr><td><span style=“white-space: nowrap”>GCM</span></td td><td><span style=“white-space: nowrap”>NoPadding/<span></td><TD><張幅樣式=「空白區間:nowrap」10>+</span></td><td></td></tr><tr><TD><張廣樣式=「空白區間:無包包」>GCM-SIV</span></td><td><張距樣式=「空白區段:nowrap」>NoPadding</span></td><td><張成樣式=「空白區:nowrap」>30+</span></td td/td><td></td 格式/tr><tr><td rowspan=“3”>span style=“white-space: nowrap”<AES_128></span>/td<><td td>span style=“white-space: nowrap”<CBC></span>br<span style=“white-space: nowrap”><ECB></span>/td<><td td>span style=“white-space: nowrap”<NoPadding></span>br span<style=“white-space: nowrap”><PKCS5Padding></span style =><></結束><TD><span 風格=「空白區:無包裹」26>+</span></td><td></td></tr><tr><TD 張><度風格=「空白區:無包裹」>GCM</span></td><TD><張度樣式=「空白區:無包裹」>NoPadding</span></td><td><長度範圍樣式=「空白區:無包裹」>26+</span></td><td></td></tr tr><td>span style=“white-space: nowrap”GCM-SIV<></span>/td<><td td>span style=“white-space: nowrap”<NoPadding></span>/td<><td td>span style=“white-space: nowrap”<30+></span>/td<><td><></td: td/tr><tr><td rowspan=“3”>span style=“white-space: nowrap”<AES_256></span>/td<><td td><><span style=“white-space: nowrap”CBC></span><BR><span style=“white-space: nowrap”>ECB</span></td><td><td span style=“white-space: nowrap”>noPadding</span><br><span style=“white-space: nowrap”>PKCS5Padding</span></td td><td><span style=“white-space: nowrap”>26+</span></td><td></><td/tr>tr<>td span style=“white-space: nowrap”GCM<></span>/td<><td td>span style=“white-space: nowrap”<NoPadding></span>/td<><td td>span style=“white-space: nowrap”<26+></span>/td<><td><></td: td/tr><tr><td>td span style=“white-space: nowrap”< GCM-SIV></span>/td td<><td>span style=“white-space:<><nowrap“NoPadding/span>/td<><td td>span style=”white-space: nowrap“<30+></span>/td<><td>/td<></tr><tr><td rowspan=”2“>span style=”white-space: nowrap“<ARC4></span>/td<><td>td span style=”white-space: nowrap“<ECB></span>/td<><td td>span style=”white-space: nopacking”<><><span></td><TD><張幅樣式=“空白區間:無包裹”>10+</span></td><td></td></tr><tr><td><span style=“空白區:無摺疊”>none</span></td><td><張幅樣式=“空白區:nowrap”>NoPadding</span></td><td td 範圍><樣式=“空白:無換頁”>28+</span></td><td/td(td></td) td/td></tr><tr><td><span style=“white-space: nowrap”>BLOWFISH</span></td><td td><span style=“white-space: nowrap”>CBC</span><br><span style=“white-space: nowrap”CFB/span br span style=“white-space: nowrap”>CFB</span><br><span style=“white-space: nowrap”>CTR</span><br><span style=“white-space: nowrap”>CTS</span><br><span style=“whitespace: nowrap”ECB>/span<br><span style=“white-space: nowrap”><OFB>/span</td><>td td<span style=“white-space: nowrap”><ISO10126 Padding>/span<br><span style=“white-space: nowrap”><noPadding>/span<br><span style=“white-space: nowrap”><PKCS5Padding>/span</td><>td td<span style=“white-space: nowrap”><10+>/span</td><>t <></結束></tr><tr><td><span style=“white-space: nowrap”>ChaCha20</span></td><td td><span style=“white-space: nowrap”>none</span><br><span style=“white-space: nowrap”>Poly1305</span></td><td td><span style=“white-space: nowrap”>noPadding</span></td><td td><span style=“white-space: nowrap”>28+</span></td><td>ChaCha,具備 20 輪、96 位元的 nonce 和 32 位元計數器,詳見 RFC 7539。</td></tr><tr><td><span style=“white-space: nowrap”>DES</span></td><td td><span style=“white-space: nowrap”>CBC</span><br><span style=“white-space: nowrap”CFB/span br span style=“white-space: nowrap”>CFB</span><br><span style=“white-space: nowrap”>CTR</span><br><span style=“white-space: nowrap”>CTS</span><br span><style=“white-space: nowrap”ECB>/span<br><span style=“white-space: nowrap”><OFB>/span</td><>td td<span style=“white-space: nowrap”><ISO10126 Padding>/span<br><span style=“white-space: nowrap”noPadding></span>br<span style=“white-space: nowrap”><><PKCS5Padding>/span</td><>td td<span style=“white-space: nowrap”><1+>/span</td><>t <></結束></tr><tr><td><span style=“white-space: nowrap”>DESede</span></td><td td><span style=“white-space: nowrap”>CBC</span><br><span style=“white-space: nowrap”CFB/span br span style=“white-space: nowrap”>CFB</span><br><span style=“white-space: nowrap”>CTR</span><br><span style=“white-space: nowrap”>CTS</span><br span><style=“white-space: nowrap”>ECB/<span><br span style =“空白空間:nowrap”OFB><>/span</td><>TD<span style=“white-space: nowrap”><ISO10126 Padding>/span<br><span style=“white-space: nowrap”noPadding></span>br<span style=“white-space: nowrap”><><PKCS5Padding/span>/<td td><>td<span style=“white-space: nowrap”><1+>/span</td><>td<></td/td。></tr><TR><td rowspan=“3”><span style=“white-space: nowrap”>RSA</span></td><td rowspan=“3”><span style=“white-space: nowrap”>ECB</span><br span><style=“white-space: nowrap”>none</span></td><td><span style=“white-space: nowrap”>noPadding</span><br span><style=“white-space: nowrap”>OAEPPadding</span><br span><style=”空白:nowrap“PKCS1Padding/span>/td<><td>span style =”white-space: nowrap“<1+></span>/td<><td>/td<></tr><tr><td>span style=”white-space: nowrap“<OAEPwithSHA-1andMGF1Padding></span>BR span<style=”white-space: nowrap“><OAEPwithSHA-256andMGF1Padding></span>/td<><td td>span style=”<><空白:無包裹“10+>/span</td><>td</td><>/tr<>tr<>TD<span style=”white-space: nowrap“><OAEPwithSHA-224andMGF1Padding>/span<bra span><style=”white-space: nowrap“><OAEPwithSHA-384andMGF1Padding>/span<br span><style style=”white-space: nowrap“><OAEPwithSHA-512andMGF1Padding>/span</td><>td td<span style=”><空白空間:nowrap“23+/span>/td<><TD>/TD/tr<><></tbody></table><>

這些轉換在 Java 密碼學架構標準演算法名稱文件的密碼部分有詳細說明。

在 1.4 版本加入。

Java 文件 javax.crypto.Cipher。

本頁部分內容為基於 Open Source Project 所創建與分享的作品,並依授權條款所描述的使用進行修改。

建構函式

名稱 Description
Cipher(CipherSpi, Provider, String)

建立一個密碼物件。

Cipher(IntPtr, JniHandleOwnership)

建立 JNI 物件的 Managed 表示法時使用的建構函式;由運行時間呼叫。

欄位

名稱 Description
DecryptMode
已淘汰.

常數用於將密碼初始化為解密模式。

EncryptMode
已淘汰.

常數用於將密碼初始化為加密模式。

PrivateKey
已淘汰.

用來表示 to-be-unwrapped金鑰的常數是「私鑰」。

PublicKey
已淘汰.

用來表示 to-be-未封裝金鑰的常數是「公鑰」。

SecretKey
已淘汰.

用來表示 to-be-unwrapped 金鑰的常數是「秘密金鑰」。

UnwrapMode
已淘汰.

常數用於將密碼初始化為金鑰展開模式。

WrapMode
已淘汰.

常數用於將密碼初始化為密鑰包裝模式。

屬性

名稱 Description
Algorithm

回傳此 Cipher 物件的演算法名稱。

BlockSize

回傳區塊大小(以位元組為單位)。

Class

回傳此 Object的執行時類別。

(繼承來源 Object)
ExemptionMechanism

回傳用於此密碼的豁免機制物件。

Handle

底層 Android 實例的帳號。

(繼承來源 Object)
JniIdentityHashCode

取得由互通執行時指派給此 Java 對等端的身份雜湊碼。

(繼承來源 Object)
JniManagedPeerState

此類別提供加密與解密的密碼學密碼功能。

(繼承來源 JavaObject)
JniPeerMembers

此類別提供加密與解密的密碼學密碼功能。

Parameters

回傳使用此密碼所使用的參數。

PeerReference

取得這個 Java 節點的 JNI 物件參考。

(繼承來源 Object)
Provider

回傳此 Cipher 物件的提供者。

ThresholdClass

此 API 支援 Mono for Android 基礎架構,並非直接從你的程式碼中使用。

ThresholdType

此 API 支援 Mono for Android 基礎架構,並非直接從你的程式碼中使用。

方法

名稱 Description
Clone()

建立並回傳此物件的副本。

(繼承來源 Object)
Construct(JniObjectReference, JniObjectReferenceOptions)

此類別提供加密與解密的密碼學密碼功能。

(繼承來源 JavaObject)
Dispose()

釋放該 Java 節點所持有的資源。

(繼承來源 Object)
Dispose(Boolean)

釋放該 Java 節點所持有的資源。

(繼承來源 Object)
DisposeUnlessReferenced()

此類別提供加密與解密的密碼學密碼功能。

(繼承來源 JavaObject)
DoFinal()

完成多段加密或解密操作,視密碼初始化方式而定。

DoFinal(Byte[], Int32, Int32, Byte[], Int32)

在單一步驟操作中加密或解密資料,或完成多步驟操作。

DoFinal(Byte[], Int32, Int32, Byte[])

在單一步驟操作中加密或解密資料,或完成多步驟操作。

DoFinal(Byte[], Int32, Int32)

在單一步驟操作中加密或解密資料,或完成多步驟操作。

DoFinal(Byte[], Int32)

完成多段加密或解密操作,視密碼初始化方式而定。

DoFinal(Byte[])

在單一步驟操作中加密或解密資料,或完成多步驟操作。

DoFinal(ByteBuffer, ByteBuffer)

在單一步驟操作中加密或解密資料,或完成多步驟操作。

Equals(Object)

此類別提供加密與解密的密碼學密碼功能。

(繼承來源 JavaObject)
Equals(Object)

表示是否有其他物體「等同」於此物。

(繼承來源 Object)
GetHashCode()

回傳物件的雜湊碼值。

(繼承來源 Object)
GetInstance(String, Provider)

回傳 Cipher 一個實作指定轉換的物件。

GetInstance(String, String)

回傳 Cipher 一個實作指定轉換的物件。

GetInstance(String)

回傳 Cipher 一個實作指定轉換的物件。

GetIV()

在新的緩衝區中回傳初始化向量(IV)。

GetMaxAllowedKeyLength(String)

根據已安裝的 JCE 管轄權政策檔案,回傳指定轉換的最大金鑰長度。

GetMaxAllowedParameterSpec(String)

回傳一個 AlgorithmParameterSpec 物件,該物件包含根據管轄區政策檔案的最大密碼參數值。

GetOutputSize(Int32)

回傳輸出緩衝區為了儲存下一updatedoFinal操作結果所需的位元組長度,前提是輸入長度inputLen(以位元組為單位)。

Init(CipherMode, Certificate, SecureRandom)

以所給憑證的公鑰及隨機性來源初始化此密碼。

Init(CipherMode, Certificate)

以該憑證的公鑰初始化此密碼。

Init(CipherMode, IKey, AlgorithmParameters, SecureRandom)

以金鑰、一組演算法參數及隨機性來源初始化此密碼。

Init(CipherMode, IKey, AlgorithmParameters)

以金鑰及一組演算法參數初始化此密碼。

Init(CipherMode, IKey, IAlgorithmParameterSpec, SecureRandom)

以金鑰、一組演算法參數及隨機性來源初始化此密碼。

Init(CipherMode, IKey, IAlgorithmParameterSpec)

以金鑰及一組演算法參數初始化此密碼。

Init(CipherMode, IKey, SecureRandom)

初始化此密碼時,擁有金鑰與隨機來源。

Init(CipherMode, IKey)

用金鑰初始化此密碼。

JavaFinalize()
已淘汰.

當垃圾回收判定該物件不再有相關參考時,由垃圾回收器呼叫。

(繼承來源 Object)
Notify()

喚醒一個正在該物件監視器上等待的執行緒。

(繼承來源 Object)
NotifyAll()

喚醒所有等待該物件監視器的執行緒。

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

設定 Handle 屬性。

(繼承來源 Object)
SetPeerReference(JniObjectReference, JniObjectReferenceOptions)

此類別提供加密與解密的密碼學密碼功能。

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

從這個 Java 陣列包裝器建立一個受管理陣列。

(繼承來源 Object)
ToString()

回傳物件的字串表示。

(繼承來源 Object)
UnregisterFromRuntime()

將此 Java 節點從互通執行時中取消註冊。

(繼承來源 Object)
Unwrap(Byte[], String, KeyType)

拆開先前包好的鑰匙。

Update(Byte[], Int32, Int32, Byte[], Int32)

繼續進行多部分加密或解密操作(視密碼初始化方式而定),處理另一部分資料。

Update(Byte[], Int32, Int32, Byte[])

繼續進行多部分加密或解密操作(視密碼初始化方式而定),處理另一部分資料。

Update(Byte[], Int32, Int32)

繼續進行多部分加密或解密操作(視密碼初始化方式而定),處理另一部分資料。

Update(Byte[])

繼續進行多部分加密或解密操作(視密碼初始化方式而定),處理另一部分資料。

Update(ByteBuffer, ByteBuffer)

繼續進行多部分加密或解密操作(視密碼初始化方式而定),處理另一部分資料。

UpdateAAD(Byte[], Int32, Int32)

繼續多階段更新額外認證資料(AAD),使用提供的緩衝區子集。

UpdateAAD(Byte[])

繼續進行額外認證資料(AAD)的多階段更新。

UpdateAAD(ByteBuffer)

繼續進行額外認證資料(AAD)的多階段更新。

Wait()

導致目前執行緒等待被喚醒,通常是透過 <em>通知</><em 或 em>中斷</em> 來喚醒。

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

會讓目前執行緒等待喚醒,通常是透過 <em>通知</><em 或 em>中斷</em>,或是經過一定的真實時間。

(繼承來源 Object)
Wait(Int64)

會讓目前執行緒等待喚醒,通常是透過 <em>通知</><em 或 em>中斷</em>,或是經過一定的真實時間。

(繼承來源 Object)
Wrap(IKey)

包一把鑰匙。

明確介面實作

名稱 Description
IJavaPeerable.Disposed()

此類別提供加密與解密的密碼學密碼功能。

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

此類別提供加密與解密的密碼學密碼功能。

(繼承來源 JavaObject)
IJavaPeerable.JniObjectReferenceControlBlock

此類別提供加密與解密的密碼學密碼功能。

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

此類別提供加密與解密的密碼學密碼功能。

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

此類別提供加密與解密的密碼學密碼功能。

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

此類別提供加密與解密的密碼學密碼功能。

(繼承來源 JavaObject)

擴充方法

名稱 Description
GetJniTypeName(IJavaPeerable)

取得實例 self類型的 JNI 名稱。

JavaAs<TResult>(IJavaPeerable)

試著強制self輸入 TResult,檢查 強制在 Java 端是否有效。

JavaCast<TResult>(IJavaObject)

執行 Android 執行時檢查型別轉換。

JavaCast<TResult>(IJavaObject)

此類別提供加密與解密的密碼學密碼功能。

TryJavaCast<TResult>(IJavaPeerable, TResult)

試著強制self輸入 TResult,檢查 強制在 Java 端是否有效。

適用於