Checks for weak or invalid-length DES or TDEA keys in hexadecimal format.
Public Declare Function DES_CheckKeyHex Lib "diCryptoSys.dll"
(ByVal strHexKey As String) As Long
nRet = DES_CheckKeyHex(strHexKey)
long __stdcall DES_CheckKeyHex(const char *szHexKey);
If successful, the return value is 0; otherwise it returns a non-zero error code.
Des.CheckKey Method (String)
Tdea.CheckKey Method (String)
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 nRet As Long Dim strKeyHex As String ' Weak key strKeyHex = "0101010101010101" nRet = DES_CheckKeyHex(strKeyHex) Debug.Print strKeyHex & " is " & IIf(nRet = 0, "OK", "BAD") & " (" & apiErrorLookup(nRet) & ")" ' Valid key by one bit strKeyHex = "0101010101010102" nRet = DES_CheckKeyHex(strKeyHex) Debug.Print strKeyHex & " is " & IIf(nRet = 0, "OK", "BAD") & " (" & apiErrorLookup(nRet) & ")" ' Another weak key strKeyHex = "01fe01fe01fe01fe" nRet = DES_CheckKeyHex(strKeyHex) Debug.Print strKeyHex & " is " & IIf(nRet = 0, "OK", "BAD") & " (" & apiErrorLookup(nRet) & ")" ' Weak double key in 1st half strKeyHex = "01010101010101010001112223334455" nRet = DES_CheckKeyHex(strKeyHex) Debug.Print strKeyHex & " is " & IIf(nRet = 0, "OK", "BAD") & " (" & apiErrorLookup(nRet) & ")" ' Weak triple key in 3rd part strKeyHex = "000111222333444555666777888999aa0101010101010101" nRet = DES_CheckKeyHex(strKeyHex) Debug.Print strKeyHex & " is " & IIf(nRet = 0, "OK", "BAD") & " (" & apiErrorLookup(nRet) & ")" ' Valid key strKeyHex = "000111222333444555666777888999aaabbbcccdddeeefff" nRet = DES_CheckKeyHex(strKeyHex) Debug.Print strKeyHex & " is " & IIf(nRet = 0, "OK", "BAD") & " (" & apiErrorLookup(nRet) & ")" ' Wrong key length (missing 'f' at end) strKeyHex = "000111222333444555666777888999aaabbbcccdddeeeff" nRet = DES_CheckKeyHex(strKeyHex) Debug.Print strKeyHex & " is " & IIf(nRet = 0, "OK", "BAD") & " (" & apiErrorLookup(nRet) & ")"
This should produce the output:
0101010101010101 is BAD (Weak key) 0101010101010102 is OK (OK, success, no error) 01fe01fe01fe01fe is BAD (Weak key) 01010101010101010001112223334455 is BAD (Weak key) 000111222333444555666777888999aa0101010101010101 is BAD (Weak key) 000111222333444555666777888999aaabbbcccdddeeefff is OK (OK, success, no error) 000111222333444555666777888999aaabbbcccdddeeeff is BAD (Invalid key length)