CryptoSys API Library Manual

DES_HexMode

Encrypts or decrypts data represented as a hexadecimal string using a specified mode. The key and initialization vector are represented as a hexadecimal string.

VB6/VBA Syntax

Public Declare Function DES_HexMode Lib "diCryptoSys.dll" (ByVal strOutput As String, ByVal strInput As String, ByVal strHexKey As String, ByVal bEncrypt As Boolean, ByVal strMode As String, ByVal strHexIV As String) As Long

nRet = DES_HexMode(strOutput, strInput, strHexKey, bEncrypt, strMode, strHexIV)

Parameters

strOutput
[out] String of sufficient length to receive the output.
strInput
[in] String containing the input data in hexadecimal.
strHexKey
[in] String containing the key in hexadecimal.
bEncrypt
[in] Boolean direction flag: set as True to encrypt or False to decrypt.
strMode
[in] String specifying the confidentiality mode:
"ECB" for Electronic Codebook mode,
"CBC" for Cipher Block Chaining mode,
"CFB" for 64-bit Cipher Feedback mode,
"OFB" for Output Feedback mode, or
"CTR" for Counter mode.
strHexIV
[in] String containing the initialization vector (IV) in hexadecimal.

C/C++ Syntax

long _stdcall DES_HexMode(char *lpszOutput, const char *lpszInput, const char *lpszKey, int bEncrypt, const char *lpszMode, const char *lpszIV);

Returns (VB6/C)

Long: If successful, the return value is 0; otherwise it returns a non-zero error code.

.NET Equivalent

Des.Encrypt Method (String, String, Mode, String)
Des.Decrypt Method (String, String, Mode, String)

COM/ASP Equivalent

des.HexMode
Public Function HexMode(ByVal strInput As String, ByVal strHexKey As String, ByVal bEncrypt As Boolean, ByVal strMode As String, ByVal strHexIV As String) As String

See des.HexMode.

Remarks

The length of the input string strInput must be a multiple of 16 hex characters long (i.e. representing a multiple of 8 bytes). If not, an error code will be returned. Both the key string strHexKey and the initialization vector strHexIV must be exactly 16 hex characters long (i.e. representing exactly 8 bytes), unless strMode is ECB, in which case strIV is ignored (use ""). Valid hexadecimal characters are [0-9A-Fa-f]. The output string strOutput must be set up with at least the same number of characters as the input string before calling. The variables strOutput and strInput should be different.

Example

    Dim nRet As Long
    Dim strOutput As String
    Dim strInput As String
    Dim strKey As String
    Dim strIV As String
    Dim sCorrect As String

    ' "Now is the time for all " in hex
    strInput = "4e6f77206973207468652074696d6520666f7220616c6c20"
    strKey = "0123456789abcdef"
    strIV = "1234567890abcdef"
    sCorrect = "e5c7cdde872bf27c43e934008c389c0f683788499a7c05f6"
    ' Set strOutput to be same length as strInput
    strOutput = String(Len(strInput), " ")

    Debug.Print "KY="; strKey
    Debug.Print "IV="; strIV
    Debug.Print "PT="; strInput
    ' Encrypt in one-off process
    nRet = DES_HexMode(strOutput, strInput, strKey, True, "CBC", strIV)
    Debug.Print "CT="; strOutput
    Debug.Print "OK="; sCorrect

    ' Now decrypt back to plain text
    strInput = strOutput
    nRet = DES_HexMode(strOutput, strInput, strKey, DECRYPT, "CBC", strIV)
    Debug.Print "P'="; strOutput

This should result in output as follows:

KY=0123456789abcdef
IV=1234567890abcdef
PT=4e6f77206973207468652074696d6520666f7220616c6c20
CT=E5C7CDDE872BF27C43E934008C389C0F683788499A7C05F6
OK=e5c7cdde872bf27c43e934008c389c0f683788499a7c05f6
P'=4E6F77206973207468652074696D6520666F7220616C6C20

See Also

DES_Hex

[Contents] [Index]

[HOME]   [NEXT: DES_Init...]

Copyright © 2001-11 D.I. Management Services Pty Ltd. All rights reserved.