Enciphers an array of Bytes in one step using the RC4-compatible 'PC1' algorithm.
Public Declare Function PC1_Bytes Lib "diCryptoSys.dll"
(ByRef abOutput As Byte, ByRef abData As Byte, ByVal nDataLen As Long,
ByRef abKey As Byte, ByVal nKeyLen As Long) As Long
nRet = PC1_Bytes(abOutput(0), abData(0), nDataLen, abKey(0), nKeyLen)
Byte array of sufficient length to receive the output.Byte array containing the input data.Long equal to length of the input data in bytes.Byte array containing the key.Long equal to length of the key in bytes.
long _stdcall PC1_Bytes(unsigned char *output, unsigned char *input,
long nbytes, unsigned char *key, long keyBytes);
Long: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Pc1.Encrypt Method (Byte[], Byte[])
pc1.EncryptHex
Public Function EncryptHex(ByVal sInputHex As String, ByVal strHexKey As String) As String
See pc1.EncryptHex.
The input data abData can be of any length provided abOutput is at least as long. The key abKey can be any length. abOutput and abData may be the same.
VB6/VBA users: Note the '(0)' after the byte array variables.
Dim abKey() As Byte
Dim abInput() As Byte
Dim abOutput() As Byte
Dim nRet As Long
Dim nDataLen As Long
Dim nKeyLen As Long
Dim sCorrect As String
abKey = cnvBytesFromHexStr("0123456789abcdef")
abInput = cnvBytesFromHexStr("0123456789abcdef")
sCorrect = "75b7878099e0c596"
ReDim abOutput(UBound(abInput))
nDataLen = UBound(abInput) - LBound(abInput) + 1
nKeyLen = UBound(abKey) - LBound(abKey) + 1
Debug.Print "KY="; cnvHexStrFromBytes(abKey)
Debug.Print "PT="; cnvHexStrFromBytes(abInput)
' Encipher using PC1
nRet = PC1_Bytes(abOutput(0), abInput(0), nDataLen, abKey(0), nKeyLen)
Debug.Print "CT="; cnvHexStrFromBytes(abOutput)
Debug.Print "OK="; sCorrect
' Now decipher just by calling again. Use same output as input.
nRet = PC1_Bytes(abOutput(0), abOutput(0), nDataLen, abKey(0), nKeyLen)
Debug.Print "P'="; cnvHexStrFromBytes(abOutput)
This should result in output as follows:
KY=0123456789ABCDEF PT=0123456789ABCDEF CT=75B7878099E0C596 OK=75b7878099e0c596 P'=0123456789ABCDEF