CryptoKey Object

Overview

The CryptoKey object represents a cryptographic key. Depending on how an instance of the CryptoKey object was created it can encapsulate a handle to a symmetric key, a key pair, or a public key alone. You can use CryptoKey to encrypt and decrypt files and text.

Member List

Properties


EffectiveLength As Long (Read/Write)

Specifies the effective size of an RC2 key. Applying this property to a key of any cipher other than RC2 will throw an error.

On Windows NT/2000, all RC2 keys have the effective length of 40 bits by default, regardless of what Key.Length returns. On Windows XP/2003, on the other hand, the effective key length matches Key.Length. As a result, data encrypted on an NT/2000 machine with a default 128-bit RC2 key will refuse to be decrypted with an identical key on an XP/2003 as the effective lengths do not match. Use this method to ensure key compatibility among various Windows platforms.


Length As Long (Read-only)
Returns the size of the underlying key (in bits).

Mode As CryptoCipherModes (Read/Write)

Specifies the cipher mode. The following values are defined for this property:

ccmCBC = 1 (Cipher block chaining)
ccmECB = 2 (Electronic code book)
ccmOFB = 3 (Output feedback mode)
ccmCFB = 4 (Cipher feedback mode)
ccmCTS = 5 (Ciphertext stealing mode)

By default, the CBC mode is used.


Padding As CryptoCipherPadding (Read/Write)

Specifies padding for the cipher. The following values are defined for this property:

ccpPKCS5 = 1 (PKCS#5 Padding)
ccpRandom = 2 (Random Padding)
ccpZero = 3 (Zero Padding)

By default, PKCS#5 Padding is used.


RawBits As CryptoBlob (Read-only)
Returns a CryptoBlob object containing this key's raw bits.

Methods


Function DecryptBinary(Blob As CryptoBlob) As CryptoBlob

Decrypts a blob specified by Blob and places the resultant decrypted blob into a CryptoBlob object.

Return Value: a CryptoBlob containing the decrypted buffer.

Usage:

Set blob = Key.DecryptBinary(EncryptedBlob)

Related Sections: 2.3 Encrypting Text and Files, 9.5 Public-key Encryption without Certificates.


Sub DecryptFile(InputPath As String, OutputPath As String)

Decrypts a file specified by InputPath and saves the decrypted file to OutputPath.

Usage:

Key.DecryptFile "c:\file.txt.xxx", "c:\file.txt"

Related Section: 2.3 Encrypting Text and Files.


Function DecryptText(Blob As CryptoBlob) As String

Decrypts an encrypted blob specified by Blob.

Return Value: a decrypted text string.

Usage:

Text = Key.DecryptText(Blob)

Related Sections: 2.3 Encrypting Text and Files, 9.5 Public-key Encryption without Certificates.


Function DecryptTextWide(Blob As CryptoBlob) As String

Same as DecryptText, but decrypts a text string encrypted with EncryptTextWide. Obsolete, use DecryptText instead.


Function EncryptBinary(Blob As CryptoBlob) As CryptoBlob

Encrypts a binary sequence specified by Blob and places the resultant encrypted blob into a CryptoBlob object. As of version 2.6, this method can also be used to encrypt directly with a public key.

Return Value: a CryptoBlob containing the encrypted buffer.

Usage:

Set EncryptedBlob = Key.EncryptBinary(blob)

Related Sections: 2.3 Encrypting Text and Files, 9.5 Public-key Encryption without Certificates.


Sub EncryptFile(InputPath As String, OutputPath As String)

Encrypts a file specified by InputPath and saves the encrypted file to OutputPath.

Return Value: a CryptoBlob containing the encrypted buffer.

Usage:

Key.EncryptFile "c:\file.txt", "c:\file.txt.xxx"

Related Section: 2.3 Encrypting Text and Files.


Function EncryptText(Text As String) As CryptoBlob

Encrypts a text string specified by Text and places the encrypted blob into a CryptoBlob object. As of version 2.6, this method can also be used to encrypt directly with a public key.

Return Value: a CryptoBlob containing the encrypted buffer.

Usage:

Set Blob = Key.EncryptText("my secret text")

Related Sections: 2.3 Encrypting Text and Files, 9.5 Public-key Encryption without Certificates.


Function EncryptTextWide(Text As String) As CryptoBlob

Same as EncryptText but uses UTF-16 encoding instead of UTF-8 when dealing with Unicode strings. Obsolete. Use EncryptText instead.


Function ExportToBlob(ExchangeKey As CryptoKey, BlobType As CryptoBlobTypes) As CryptoBlob

Identical to ExportToFile except that the key blob is transferred to a CryptoBlob object rather than a file

Return Value: a CryptoBlob object which contains the key blob.

Usage:

Set Blob = Key.ExportToBlob(ExchangeKey, cbtSimpleBlob )

Related Section: 6.3 Creating and Verifying Digital Signatures.


Sub ExportToFile(ExchangeKey As CryptoKey, Path As String, BlobType As CryptoBlobTypes)

Exports the underlying key to a binary file securely.

If the key being exported is a symmetric key, ExchangeKey should be set to a key pair so that the key can be encrypted with the pair's public key, and BlobType should be set to cbtSimpleBlob. If you do not wish to encrypt a symmetric key being exported, you should set the first argument to the special "Exponent 1" key pair. The Exponent 1 key pair does nothing to information being encrypted. Create an instance of the Exponent 1 key pair via the CryptoContext's CreateExponentOneKey method.

If the key being exported is a key pair, ExchangeKey should be set to an symmetric key which will be used to encrypt the key pair, and BlobType should be set to cbtPrivateKeyBlob.

If the key being exported is a public key, ExchangeKey should be set to Nothing as no encryption is necessary, and BlobType should be set to cbtPublicKeyBlob.

Path specifies the file that the key blob will be saved to.

Usage:

Key.ExportToFile(ExchangeKey, "c:\mykey.key", cbtSimpleBlob )

Related Section: 6.2 OpenContext Method Revisited.


Sub SetIV(IV As CryptoBlob)

Specifies an initialization vector for the key. The intialization vector is usually an 8-byte binary sequence which is to be passed to the key via a CryptoBlob object.

Usage:

...
Set IVBlob = CM.CreateBlob
IVBlob.Base64 = "X6PfOtMlNmk="
Key.SetIV IVBlob

Related Section: 2.4 Cipher Modes of Operation, Padding, Initialization Vectors.

CryptoContext CryptoHash