To use CryptoSys API in VB.NET, you call .NET methods instead of VB6 functions.
In almost all cases, a VB6 function like FOO_Barbaz
is replaced by the .NET method Foo.Barbaz
.
The .NET methods generally have fewer parameters and different return values than the equivalent VB6 function.
See the List of .NET Methods and
Cross-reference between Functions and .NET Methods.
Long
types in VB6 must be changed to Integer
in VB.NET.
All integers in CryptoSys API are 32-bit signed integers.
You will get an exception or garbage results if you use a VB.NET Long
type.
Functions that in VB6 would have output to a pre-dimensioned string or byte array will return that string or byte array directly in VB.NET. For such methods in VB.NET, an error condition is indicated by returning an empty string or byte array.
VB6/VBA
ReDim abDigest(API_MAX_HASH_BYTES - 1)
nRet = HASH_Bytes(abDigest(0), API_MAX_HASH_BYTES, abMessage(0), nMsgLen, API_HASH_MD5)
VB.NET
abDigest = Hash.BytesFromBytes(abMessage, HashAlgorithm.Md5)
Annotated example:
VB6/VBA
1 Debug.Print "Testing HASH_HexFromHex ..."
2 Dim strDigest As String
3 Dim nRet As Long
4 Dim strData As String
5 strDigest = String(API_SHA1_CHARS, " ")
6 strData = "616263"
7 nRet = HASH_HexFromHex(strDigest, Len(strDigest), strData, API_HASH_SHA1)
8 Debug.Print strDigest
Comments:
strDigest
or else! nRet = 0
indicates success (for this function). API_HASH_SHA1
flag to specify hash algorithm option. Output
Testing HASH_HexFromHex ... a9993e364706816aba3e25717850c26c9cd0d89d
VB.NET
1 Console.WriteLine("Testing HASH_HexFromHex ...") 2 Dim strDigest As String 3 ''Dim nRet As Integer 4 Dim strData As String 5 ''strDigest = String(API_SHA1_CHARS, " ") 6 strData = "616263" 7 strDigest = Hash.HexFromHex(strData, HashAlgorithm.Sha1) 8 Console.WriteLine(strDigest)
Comments:
strDigest.Length > 0
indicates success.HashAlgorithm
property to specify hash algorithm option.Most methods in VB.NET are "all-in-one" stateless methods and so do not require the creation of an object. These are called in a one-off manner. The incremental stateful functions in VB6 that use a context handle (those created by an Init function and closed by a Final function) are replaced in VB.NET by objects that must be instantiated and then disposed of. Be careful with error conditions because the VB6 Init functions return zero on error, but the VB.NET Init methods return either True or zero on success.
VB6/VBA
Dim hContext as Long hContext = FOO_Init(...) If hContext = 0 Then nError = FOO_InitError() ... End If For i = 1 To n nRet = FOO_Update(hContext, abBlock(0), nBlockLen) Next nRet = FOO_Final(hContext)
VB.NET
Dim oFoo As Foo = Foo.Instance() nRet = oFoo.InitEncrypt(...) If nRet <> 0 nError = oFoo.ErrCode ... End If For i = 1 To n abBlock = oFoo.Update(abBlock) End If oFoo.Dispose()