CryptoSys API Library Manual

DES_Bytes

Encrypts or decrypts an array of Bytes in one step in Electronic Codebook (EBC) mode.

VB6/VBA Syntax

Public Declare Function DES_Bytes Lib "diCryptoSys.dll" (ByRef abOutput As Byte, ByRef abData As Byte, ByVal nDataLen As Long, ByRef abKey As Byte, ByVal bEncrypt As Boolean) As Long

nRet = DES_Bytes(abOutput(0), abData(0), nDataLen, abKey(0), bEncrypt)

Parameters

abOutput
[out] Byte array of sufficient length to receive the output.
abData
[in] Byte array containing the input data.
nDataLen
[in] Long equal to length of the input data in bytes.
abKey
[in] Byte array containing the key.
bEncrypt
[in] Boolean direction flag: set as True to encrypt or False to decrypt.

C/C++ Syntax

long _stdcall DES_Bytes(unsigned char *output, const unsigned char *input, long nbytes, const unsigned char *key, int bEncrypt);

Returns (VB6/C)

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

Remarks

The input data abData must be an exact multiple of 8 bytes long. If not, an error code will be returned. The key abKey must be exactly 8 bytes long. The output array abOutput must be at least nDataLen bytes long. abOutput and abData may be the same.

VB6/VBA users: Note the '(0)' after the byte array variables.

Example

This example uses a test vector from [FIPS 81].

    Dim nRet As Long
    Dim strInput As String
    Dim strKey As String
    Dim sCorrect As String
    Dim nKeyLen As Long
    Dim abKey() As Byte
    Dim abOutput() As Byte
    Dim abData() As Byte
    Dim nDataLen As Long
    
    ' Define test vectors in hex
    strKey = "0123456789abcdef"
    strInput = "4e6f77206973207468652074696d6520666f7220616c6c20"
    sCorrect = "3FA40E8A984D48156A271787AB8883F9893D51EC4B563B53"
    
    ' Convert to byte arrays and compute lengths
    nKeyLen = Len(strKey) \ 2
    abKey = cnvBytesFromHexStr(strKey)
    nDataLen = Len(strInput) \ 2
    abData = cnvBytesFromHexStr(strInput)
    
    ' Dimension array for output
    ReDim abOutput(nDataLen - 1)
    
    Debug.Print "KY="; cnvHexStrFromBytes(abKey)
    Debug.Print "PT="; cnvHexStrFromBytes(abData)
    ' Encrypt in one-off process
    nRet = DES_Bytes(abOutput(0), abData(0), nDataLen, abKey(0), ENCRYPT)
    Debug.Print "CT="; cnvHexStrFromBytes(abOutput), nRet
    Debug.Print "OK="; sCorrect
    Debug.Assert (sCorrect = cnvHexStrFromBytes(abOutput))
    
    ' Now decrypt back
    nRet = DES_Bytes(abData(0), abOutput(0), nDataLen, abKey(0), DECRYPT)
    Debug.Print "P'="; cnvHexStrFromBytes(abData), nRet
    Debug.Assert (strInput = cnvHexStrFromBytes(abData))

This should result in output as follows:

KY=0123456789ABCDEF
PT=4E6F77206973207468652074696D6520666F7220616C6C20
CT=3FA40E8A984D48156A271787AB8883F9893D51EC4B563B53 0
OK=3FA40E8A984D48156A271787AB8883F9893D51EC4B563B53
P'=4E6F77206973207468652074696D6520666F7220616C6C20 0

See Also

DES_BytesMode

[Contents] [Index]

[HOME]   [NEXT: DES_BytesMode...]

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