Encrypts or decrypts an array of Bytes in one step in Electronic Codebook (EBC) mode.
@deprecated use
DES_BytesMode
with strMode="ECB"
.
Public Declare Function DES_Bytes Lib "diCryptoSys.dll"
(ByRef lpOutput As Byte, ByRef lpData As Byte, ByVal nDataLen As Long,
ByRef lpKey As Byte, ByVal bEncrypt As Boolean) As Long
nRet = DES_Bytes(lpOutput(0), abData(0), nDataLen, abKey(0), bEncrypt)
' Note the "(0)" after the byte array parameters
long __stdcall DES_Bytes(unsigned char *lpOutput, const unsigned char *lpInput, long nBytes, const unsigned char *lpKey, int fEncrypt);
If successful, the return value is 0; otherwise it returns a non-zero error code.
Des.Encrypt Method (Byte[], Byte[], Mode, Byte[])
Des.Decrypt Method (Byte[], Byte[], Mode, Byte[])
with mode=Mode.ECB
.
The input data lpData must be an exact multiple of 8 bytes long. If not, an error code will be returned. The key lpKey must be exactly 8 bytes long. The output array lpOutput must be at least nDataLen bytes long. lpOutput and lpData may be the same.
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 lpOutput() 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 lpOutput(nDataLen - 1) Debug.Print "KY=" & cnvHexStrFromBytes(abKey) Debug.Print "PT=" & cnvHexStrFromBytes(abData) ' Encrypt in one-off process nRet = DES_Bytes(lpOutput(0), abData(0), nDataLen, abKey(0), ENCRYPT) Debug.Print "CT=" & cnvHexStrFromBytes(lpOutput), nRet Debug.Print "OK=" & sCorrect Debug.Assert (sCorrect = cnvHexStrFromBytes(lpOutput)) ' Now decrypt back nRet = DES_Bytes(abData(0), lpOutput(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