Udostępnij za pośrednictwem


Cipher Class

Definition

This class provides the functionality of a cryptographic cipher for encryption and decryption.

[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
Inheritance
Cipher
Derived
Attributes

Remarks

This class provides the functionality of a cryptographic cipher for encryption and decryption. It forms the core of the Java Cryptographic Extension (JCE) framework.

In order to create a Cipher object, the application calls the Cipher's getInstance method, and passes the name of the requested transformation to it. Optionally, the name of a provider may be specified.

A transformation is a string that describes the operation (or set of operations) to be performed on the given input, to produce some output. A transformation always includes the name of a cryptographic algorithm (e.g., DES), and may be followed by a feedback mode and padding scheme.

A transformation is of the form:

<ul> <li>"algorithm/mode/padding" or

<li>"algorithm" </ul>

(in the latter case, provider-specific default values for the mode and padding scheme are used). For example, the following is a valid transformation:

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

Using modes such as CFB and OFB, block ciphers can encrypt data in units smaller than the cipher's actual block size. When requesting such a mode, you may optionally specify the number of bits to be processed at a time by appending this number to the mode name as shown in the "DES/CFB8/NoPadding" and "DES/OFB32/PKCS5Padding" transformations. If no such number is specified, a provider-specific default is used. (For example, the SunJCE provider uses a default of 64 bits for DES.) Thus, block ciphers can be turned into byte-oriented stream ciphers by using an 8 bit mode such as CFB8 or OFB8.

Modes such as Authenticated Encryption with Associated Data (AEAD) provide authenticity assurances for both confidential data and Additional Associated Data (AAD) that is not encrypted. (Please see RFC 5116 for more information on AEAD and AEAD algorithms such as GCM/CCM.) Both confidential and AAD data can be used when calculating the authentication tag (similar to a Mac). This tag is appended to the ciphertext during encryption, and is verified on decryption.

AEAD modes such as GCM/CCM perform all AAD authenticity calculations before starting the ciphertext authenticity calculations. To avoid implementations having to internally buffer ciphertext, all AAD data must be supplied to GCM/CCM implementations (via the updateAAD methods) <b>before</b> the ciphertext is processed (via the update and doFinal methods).

Note that GCM mode has a uniqueness requirement on IVs used in encryption with a given key. When IVs are repeated for GCM encryption, such usages are subject to forgery attacks. Thus, after each encryption operation using GCM mode, callers should re-initialize the cipher objects with GCM parameters which has a different IV value.

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 provides the following Cipher transformations: <table> <thead> <tr> <th>Algorithm</th> <th>Modes</th> <th>Paddings</th> <th>Supported API Levels</th> <th>Notes</th> </tr> </thead> <tbody> <tr> <td rowspan="2"><span style="white-space: nowrap">AES</span></td> <td><span style="white-space: nowrap">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">ECB</span><br><span style="white-space: nowrap">OFB</span></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><span style="white-space: nowrap">NoPadding</span></td> <td><span style="white-space: nowrap">10+</span></td> <td></td> </tr> <tr> <td rowspan="2"><span style="white-space: nowrap">AES_128</span></td> <td><span style="white-space: nowrap">CBC</span><br><span style="white-space: nowrap">ECB</span></td> <td><span style="white-space: nowrap">NoPadding</span><br><span style="white-space: nowrap">PKCS5Padding</span></td> <td><span style="white-space: nowrap">26+</span></td> <td></td> </tr> <tr> <td><span style="white-space: nowrap">GCM</span></td> <td><span style="white-space: nowrap">NoPadding</span></td> <td><span style="white-space: nowrap">26+</span></td> <td></td> </tr> <tr> <td rowspan="2"><span style="white-space: nowrap">AES_256</span></td> <td><span style="white-space: nowrap">CBC</span><br><span style="white-space: nowrap">ECB</span></td> <td><span style="white-space: nowrap">NoPadding</span><br><span style="white-space: nowrap">PKCS5Padding</span></td> <td><span style="white-space: nowrap">26+</span></td> <td></td> </tr> <tr> <td><span style="white-space: nowrap">GCM</span></td> <td><span style="white-space: nowrap">NoPadding</span></td> <td><span style="white-space: nowrap">26+</span></td> <td></td> </tr> <tr> <td rowspan="2"><span style="white-space: nowrap">ARC4</span></td> <td><span style="white-space: nowrap">ECB</span></td> <td><span style="white-space: nowrap">NoPadding</span></td> <td><span style="white-space: nowrap">10+</span></td> <td></td> </tr> <tr> <td><span style="white-space: nowrap">NONE</span></td> <td><span style="white-space: nowrap">NoPadding</span></td> <td><span style="white-space: nowrap">28+</span></td> <td></td> </tr> <tr> <td><span style="white-space: nowrap">BLOWFISH</span></td> <td><span style="white-space: nowrap">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">ECB</span><br><span style="white-space: nowrap">OFB</span></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">10+</span></td> <td></td> </tr> <tr> <td><span style="white-space: nowrap">ChaCha20</span></td> <td><span style="white-space: nowrap">NONE</span><br><span style="white-space: nowrap">Poly1305</span></td> <td><span style="white-space: nowrap">NoPadding</span></td> <td><span style="white-space: nowrap">28+</span></td> <td>ChaCha with 20 rounds, 96-bit nonce, and 32-bit counter as described in RFC 7539.</td> </tr> <tr> <td><span style="white-space: nowrap">DES</span></td> <td><span style="white-space: nowrap">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">ECB</span><br><span style="white-space: nowrap">OFB</span></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">DESede</span></td> <td><span style="white-space: nowrap">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">ECB</span><br><span style="white-space: nowrap">OFB</span></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 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="white-space: 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><span style="white-space: nowrap">10+</span></td> <td></td> </tr> <tr> <td><span style="white-space: nowrap">OAEPwithSHA-224andMGF1Padding</span><br><span style="white-space: nowrap">OAEPwithSHA-384andMGF1Padding</span><br><span style="white-space: nowrap">OAEPwithSHA-512andMGF1Padding</span></td> <td><span style="white-space: nowrap">23+</span></td> <td></td> </tr> </tbody> </table>

These transformations are described in the Cipher section of the Java Cryptography Architecture Standard Algorithm Name Documentation.

Added in 1.4.

Java documentation for javax.crypto.Cipher.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Constructors

Cipher(CipherSpi, Provider, String)

Creates a Cipher object.

Cipher(IntPtr, JniHandleOwnership)

A constructor used when creating managed representations of JNI objects; called by the runtime.

Fields

DecryptMode
Obsolete.

Constant used to initialize cipher to decryption mode.

EncryptMode
Obsolete.

Constant used to initialize cipher to encryption mode.

PrivateKey
Obsolete.

Constant used to indicate the to-be-unwrapped key is a "private key".

PublicKey
Obsolete.

Constant used to indicate the to-be-unwrapped key is a "public key".

SecretKey
Obsolete.

Constant used to indicate the to-be-unwrapped key is a "secret key".

UnwrapMode
Obsolete.

Constant used to initialize cipher to key-unwrapping mode.

WrapMode
Obsolete.

Constant used to initialize cipher to key-wrapping mode.

Properties

Algorithm

Returns the algorithm name of this Cipher object.

BlockSize

Returns the block size (in bytes).

Class

Returns the runtime class of this Object.

(Inherited from Object)
ExemptionMechanism

Returns the exemption mechanism object used with this cipher.

Handle

The handle to the underlying Android instance.

(Inherited from Object)
JniIdentityHashCode (Inherited from Object)
JniPeerMembers
Parameters

Returns the parameters used with this cipher.

PeerReference (Inherited from Object)
Provider

Returns the provider of this Cipher object.

ThresholdClass

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

ThresholdType

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Methods

Clone()

Creates and returns a copy of this object.

(Inherited from Object)
Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
DoFinal()

Finishes a multiple-part encryption or decryption operation, depending on how this cipher was initialized.

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

Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.

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

Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.

DoFinal(Byte[], Int32, Int32)

Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.

DoFinal(Byte[], Int32)

Finishes a multiple-part encryption or decryption operation, depending on how this cipher was initialized.

DoFinal(Byte[])

Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.

DoFinal(ByteBuffer, ByteBuffer)

Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.

Equals(Object)

Indicates whether some other object is "equal to" this one.

(Inherited from Object)
GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
GetInstance(String, Provider)

Returns a Cipher object that implements the specified transformation.

GetInstance(String, String)

Returns a Cipher object that implements the specified transformation.

GetInstance(String)

Returns a Cipher object that implements the specified transformation.

GetIV()

Returns the initialization vector (IV) in a new buffer.

GetMaxAllowedKeyLength(String)

Returns the maximum key length for the specified transformation according to the installed JCE jurisdiction policy files.

GetMaxAllowedParameterSpec(String)

Returns an AlgorithmParameterSpec object which contains the maximum cipher parameter value according to the jurisdiction policy file.

GetOutputSize(Int32)

Returns the length in bytes that an output buffer would need to be in order to hold the result of the next update or doFinal operation, given the input length inputLen (in bytes).

Init(CipherMode, Certificate, SecureRandom)

Initializes this cipher with the public key from the given certificate and a source of randomness.

Init(CipherMode, Certificate)

Initializes this cipher with the public key from the given certificate.

Init(CipherMode, IKey, AlgorithmParameters, SecureRandom)

Initializes this cipher with a key, a set of algorithm parameters, and a source of randomness.

Init(CipherMode, IKey, AlgorithmParameters)

Initializes this cipher with a key and a set of algorithm parameters.

Init(CipherMode, IKey, IAlgorithmParameterSpec, SecureRandom)

Initializes this cipher with a key, a set of algorithm parameters, and a source of randomness.

Init(CipherMode, IKey, IAlgorithmParameterSpec)

Initializes this cipher with a key and a set of algorithm parameters.

Init(CipherMode, IKey, SecureRandom)

Initializes this cipher with a key and a source of randomness.

Init(CipherMode, IKey)

Initializes this cipher with a key.

JavaFinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object)
Notify()

Wakes up a single thread that is waiting on this object's monitor.

(Inherited from Object)
NotifyAll()

Wakes up all threads that are waiting on this object's monitor.

(Inherited from Object)
SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
ToArray<T>() (Inherited from Object)
ToString()

Returns a string representation of the object.

(Inherited from Object)
UnregisterFromRuntime() (Inherited from Object)
Unwrap(Byte[], String, KeyType)

Unwrap a previously wrapped key.

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

Continues a multiple-part encryption or decryption operation (depending on how this cipher was initialized), processing another data part.

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

Continues a multiple-part encryption or decryption operation (depending on how this cipher was initialized), processing another data part.

Update(Byte[], Int32, Int32)

Continues a multiple-part encryption or decryption operation (depending on how this cipher was initialized), processing another data part.

Update(Byte[])

Continues a multiple-part encryption or decryption operation (depending on how this cipher was initialized), processing another data part.

Update(ByteBuffer, ByteBuffer)

Continues a multiple-part encryption or decryption operation (depending on how this cipher was initialized), processing another data part.

UpdateAAD(Byte[], Int32, Int32)

Continues a multi-part update of the Additional Authentication Data (AAD), using a subset of the provided buffer.

UpdateAAD(Byte[])

Continues a multi-part update of the Additional Authentication Data (AAD).

UpdateAAD(ByteBuffer)

Continues a multi-part update of the Additional Authentication Data (AAD).

Wait()

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

(Inherited from Object)
Wait(Int64, Int32)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wait(Int64)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wrap(IKey)

Wrap a key.

Explicit Interface Implementations

IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)

Applies to