Initializes the context with the key ready for repeated operations of GCM_NextEncrypt and GCM_NextDecrypt.
VB6/VBA
Debug.Print "Testing GCM_InitKey ..." Dim abKey() As Byte Dim nKeyLen As Long Dim hContext As Long Dim nCode As Long ' Initialise with a valid key abKey = cnvBytesFromHexStr("feffe9928665731c6d6a8f9467308308") nKeyLen = UBound(abKey) + 1 hContext = GCM_InitKey(abKey(0), nKeyLen, 0) Debug.Print "GCM_InitKey returns " & Hex(hContext) & " (expected non-0)" If hContext <> 0 Then '... do something here... ' Destroy the key Call GCM_FinishKey(hContext) End If ' Now try an invalid key (length must be 16,24, or 32 bytes) abKey = cnvBytesFromHexStr("badace") nKeyLen = UBound(abKey) + 1 hContext = GCM_InitKey(abKey(0), nKeyLen, 0) Debug.Print "GCM_InitKey returns " & Hex(hContext) ' Use API_ErrorCode to find the error value nCode = API_ErrorCode() Debug.Print "API_ErrorCode returns " & nCode & ": " & apiErrorLookup(nCode)
Output
Testing GCM_InitKey ... GCM_InitKey returns 3FFE5A71 (expected non-0) GCM_InitKey returns 0 API_ErrorCode returns 33: Invalid key length (BAD_KEY_LEN_ERROR)
VB.NET
Console.WriteLine("Testing GCM_InitKey ...")
Dim abKey() As Byte
Dim nKeyLen As Integer
''Dim hContext As Integer
Dim nCode As Integer
Dim nRet As Integer
Dim oGcm As Gcm = Gcm.Instance()
' Initialise with a valid key
abKey = Cnv.FromHex("feffe9928665731c6d6a8f9467308308")
nKeyLen = UBound(abKey) + 1
nRet = oGcm.InitKey(abKey)
Console.WriteLine("GCM_InitKey returns " & nRet & " (expected zero)")
If nRet = 0 Then
'... do something here...
' Destroy the key
oGcm.Dispose()
End If
' Now try an invalid key (length must be 16,24, or 32 bytes)
abKey = Cnv.FromHex("badace")
nKeyLen = UBound(abKey) + 1
nRet = oGcm.InitKey(abKey)
Console.WriteLine("GCM_InitKey returns " & nRet)
' Use API_ErrorCode to find the error value
nCode = oGcm.ErrCode()
Console.WriteLine("API_ErrorCode returns " & nCode & ": " & General.ErrorLookup(nCode))
[Contents]