Checks for weak or invalid-length DES or TDEA keys.
Public Declare Function DES_CheckKey Lib "diCryptoSys.dll"
(ByRef lpKey As Byte, ByVal nKeyBytes As Long) As Long
nRet = DES_CheckKey(abKey(0), nKeyBytes)
long __stdcall DES_CheckKey(const unsigned char *lpKey, long nKeyLen);
If successful, the return value is 0; otherwise it returns a non-zero error code.
Des.CheckKey Method (Byte[])
Tdea.CheckKey Method (Byte[])
DES_CheckKey verifies that the key size is valid for a single, double or triple DES key -
i.e. is 8, 16 or 24 bytes long - and that no part of the key is a weak or semi-weak DES key.
Dim abKey() As Byte
Dim nKeyBytes As Long
Dim nRet As Long
nKeyBytes = 8
ReDim abKey(nKeyBytes - 1)
' Generate a random DES key
nRet = RNG_KeyBytes(abKey(0), nKeyBytes, "", 0)
Debug.Print cnvHexStrFromBytes(abKey)
' Check if it's OK
nRet = DES_CheckKey(abKey(0), nKeyBytes)
Debug.Print "DES_CheckKey returns " & nRet & " (" & apiErrorLookup(nRet) & ")"
If nRet <> 0 Then
' Error...
End If
This should produce output similar to:
5305A0FA4CC94A82 DES_CheckKey returns 0 (OK, success, no error)
unless the key generated is weak (a 1 in 4500 billion chance!).