Persits Software, Inc. Web Site
Main Menu:  Home |  Tasks |  Object Reference |  Crypto 101 |  FAQ |  Download & Buy |  Clients |  Live Demo |  Contact
 Navigator:  Home |  Object Reference |  CryptoKey Object
CryptoHash CryptoContext
  CryptoKey Object
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.

Properties Methods
EffectiveLength
Length
Mode
Padding
RawBits
EncryptFile
DecryptFile
EncryptText
DecryptText
EncryptTextWide
DecryptTextWide
EncryptBinary
DecryptBinary
ExportToFile
ExportToBlob
SetIV

  CryptoKey Properties

Length As Long (Read-Only)

Returns the size of the underlying key (in bits).

EffectiveLength As Long (Read/Write) New in AspEncrypt 2.2

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.


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.

  CryptoKey Methods


Sub EncryptFile ( InputPath As String, OutputPath As String )
Encrypts a file specified by InputPath and saves the encrypted file to OutputPath.

Usage:

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

Related Task: Encrypt and Decrypt Files and Messages.


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 Task: Encrypt and Decrypt Files and Messages.


Function EncryptText ( Text As String ) As CryptoBlob
Encrypts an ANSI text string specified by Text and places the encrypted blob into a CryptoBlob object.

Return Value: a CryptoBlob containing the encrypted buffer.

Usage:

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

Related Tasks: Encrypt and Decrypt Files and Messages,
Encrypt Credit Card Info with a Random Key.


Function DecryptText ( Blob As CryptoBlob ) As String
Decrypts an encrypted blob specified by Blob

Return Value: a decrypted ANSI text string.

Usage:

Text = Key.DecryptText(Blob)

Related Task: Encrypt and Decrypt Files and Messages,
Encrypt Credit Card Info with a Random Key.


Function EncryptTextWide ( Text As String ) As CryptoBlob
Same as EncryptText, but encrypts a Unicode text string. You should always use this method instead of EncryptText if encrypting a non-English text. Decryption must be performed with DecryptTextWide.


Function DecryptTextWide ( Blob As CryptoBlob ) As String
Same as DecryptText, but decrypts a Unicode text string encrypted with EncryptTextWide.

Function EncryptBinary ( Blob As CryptoBlob ) As CryptoBlob
Encrypts a binary sequence specified by Blob and places the resultant encrypted blob into a CryptoBlob object.

Return Value: a CryptoBlob containing the encrypted buffer.

Usage:

Set EncryptedBlob = Key.EncryptBinary(blob)


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)


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 Task: Encrypt and Decrypt Files and Messages.


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 Task: Encrypt and Decrypt Files and Messages.


Sub SetIV ( IV As CryptoBlob ) New in AspEncrypt 2.1.0.2
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="
' Base64-encoded 8-byte IV value
Key.SetIV IVBlob

Related KB article: Initialization Vector (IV) support in AspEncrypt.


CryptoContext CryptoHash

Search this Site
  This site is owned and maintained by Persits Software, Inc. Copyright © 2000. All Rights Reserved.