CryptoSys API Library Manual

RNG_KeyHex

Generates a random set of data in hexadecimal format suitable for cryptographic keys.

VBA/VB6 Syntax

Public Declare Function RNG_KeyHex Lib "diCryptoSys.dll" (ByVal strOutput As String, ByVal nMaxChars As Long, ByVal nBytes As Long, ByVal strSeed As String, ByVal nSeedLen As Long) As Long

nRet = RNG_KeyHex(strOutput, nMaxChars, nBytes, strSeed, nSeedLen)

C/C++ Syntax

long __stdcall RNG_KeyHex(char *szOutput, long nMaxChars, long nBytes, const void *lpSeed, long nSeedLen);

Parameters

szOutput
[out] of sufficient length to receive the output.
nMaxChars
[in] specifying the maximum number of characters in szOutput.
nBytes
[in] value of the required key length in bytes.
lpSeed
[in] containing an (optional) user-specified seed to be used by the random number generator. Specify an empty string ("") or NULL to ignore..
nSeedLen
[in] specifying the size of the seed in bytes.

Returns (VBA/C)

Always returns zero. If the function fails its continuous random number generator test, a critical error will occur. See Self Tests for more details.

VBA Wrapper Syntax

Public Function rngKeyHex(nBytes As Long, Optional szSeed = "") As String

.NET Equivalent

Rng.KeyHex Method (Int32, Byte[])
Rng.KeyHex Method (Int32, String)

COM/ASP Equivalent

rng.KeyHex
Public Function KeyHex(ByVal nBytes As Long) As String

See rng.KeyHex.

Remarks

The output string szOutput should be pre-dimensioned to be at least double the required key length in bytes. Hint: specify nMaxChars as Len(strOutput). C/C++ users must add one to this value when allocating memory. The seed szSeed is optional and is added to the automatic seed values generated internally. The seed cannot directly affect the value of the output; it will just ensure that it will be different.

Example (VBA core function)

Dim strHexKey As String
Dim nRet As Long
Dim nKeyBytes As Long
Dim strSeed As String
Dim i As Integer

nKeyBytes = 24
' Pre-dimension hex string to be DOUBLE key length in bytes
strHexKey = String(nKeyBytes * 2, " ")

nRet = RNG_KeyHex(strHexKey, Len(strHexKey), nKeyBytes, "", 0)
Debug.Print strHexKey

' Generate three successive 192-bit random keys with no seeding
For i = 1 To 3
    nRet = RNG_KeyHex(strHexKey, Len(strHexKey), nKeyBytes, "", 0)
    Debug.Print strHexKey
Next

' Generate three more 192-bit random keys using the counter as a seed
For i = 1 To 3
    strSeed = CStr(i)
    nRet = RNG_KeyHex(strHexKey, Len(strHexKey), nKeyBytes, strSeed, Len(strSeed))
    Debug.Print strHexKey
Next

' Generate a DES key and check if it's a weak key
strHexKey = String(8 * 2, " ")
nRet = RNG_KeyHex(strHexKey, Len(strHexKey), 8, "", 0)
Debug.Print strHexKey
nRet = DES_CheckKeyHex(strHexKey)
Debug.Print "DES_CheckKeyHex returns " & nRet & " (" & apiErrorLookup(nRet) & ")"

' Test a known weak key
strHexKey = "fefefefefefefefe"
nRet = DES_CheckKeyHex(strHexKey)
Debug.Print "DES_CheckKeyHex returns " & nRet & " (" & apiErrorLookup(nRet) & ")"

This will produce output that of the following form with obviously different values:

307152819FE0C2E718A1A6D4FA49AABB36CF274EABA77038
A02FE37FB1BB2F26636B2FAB79D3502617F5FF1761056D0A
E936ED0A805CB5D8190C33B24CE90B9C47CD75627D559E17
5A630F6D97CA0DF22649FABFFF555D91820797C6378C0972
F82B84DD477F065A5D9D2B8B4C625977B5433EA50671A28B
BB54598AA5457F46EAEEB7BD78DA1B1C34F81894FEB216FB
E8CB635CC3D3571DD44D8C982B887E35F00252DF336DAC5A
C7412767A925CA74
DES_CheckKeyHex returns 0 (OK, success, no error)
DES_CheckKeyHex returns 52 (Weak key)

Example (VBA wrapper function)

Dim i As Integer
For i = 1 To 10
    Debug.Print rngKeyHex(16)
Next

See Also

RNG_KeyBytes

[Contents] [Index]

[PREV: RNG_KeyBytes...]   [Contents]   [Index]   
   [NEXT: RNG_MakeSeedFile...]

Copyright © 2001-24 D.I. Management Services Pty Ltd. All rights reserved. Generated 2024-01-07T07:42:00Z.