CryptoSys API examples VB6 to VB.NET

TDEA_Update

Carries out the TDEA transformation function on a byte array according to the direction and mode set up by an earlier call to TDEA_Init or TDEA_InitHex.

VB6/VBA

Debug.Print "Testing TDEA_Update ..."
Dim nRet As Long
Dim hContext As Long
Dim sCorrect As String
Dim j As Integer
Dim abKey() As Byte
Dim abInitV() As Byte
Dim aBlock() As Byte
Dim aNext() As Byte
Dim aLast() As Byte
aBlock() = StrConv("Now is t", vbFromUnicode)
abKey = cnvBytesFromHexStr( _
    "0123456789abcdef23456789abcdef01456789abcdef0123")
abInitV = cnvBytesFromHexStr("1234567890abcdef")
sCorrect = "cb191f85d1ed8439"
Debug.Print "TDEA Monte Carlo TCBC Mode Encrypt:"
Debug.Print "KY=", cnvHexStrFromBytes(abKey)
Debug.Print "IV=", cnvHexStrFromBytes(abInitV)
Debug.Print "PT=", cnvHexStrFromBytes(aBlock)
hContext = TDEA_Init(abKey(0), True, "CBC", abInitV(0))
If hContext = 0 Then
    nRet = TDEA_InitError()
    Debug.Print "TDEA_Init Failed: " & apiErrorLookup(nRet)
    Exit Sub
End If
' Do 10,000 times
aNext() = aBlock()
For j = 0 To 9999
    aBlock() = aNext()
    nRet = TDEA_Update(hContext, aBlock(0), 8)
    If j = 0 Then
        aNext() = abInitV()
    Else
        aNext() = aLast()
    End If
    aLast() = aBlock()
Next
Debug.Print "CT=", cnvHexStrFromBytes(aBlock)
Debug.Print "OK=", sCorrect
nRet = TDEA_Final(hContext)

Debug.Assert (sCorrect = cnvHexStrFromBytes(aBlock))

Output

Testing TDEA_Update ...
TDEA Monte Carlo TCBC Mode Encrypt:
KY=   0123456789ABCDEF23456789ABCDEF01456789ABCDEF0123
IV=   1234567890ABCDEF
PT=   4E6F772069732074
CT=   CB191F85D1ED8439
OK=   cb191f85d1ed8439

VB.NET

Console.WriteLine("Testing TDEA_Update ...")
Dim nRet As Integer
''Dim hContext As Integer
Dim sCorrect As String
Dim j As Integer
Dim abKey() As Byte
Dim abInitV() As Byte
Dim aBlock() As Byte
Dim aNext() As Byte
Dim aLast() As Byte
Dim oTdea As Tdea = Tdea.Instance()
aBlock = System.Text.Encoding.Default.GetBytes("Now is t")
abKey = Cnv.FromHex( _
    "0123456789abcdef23456789abcdef01456789abcdef0123")
abInitV = Cnv.FromHex("1234567890abcdef")
sCorrect = "cb191f85d1ed8439"
Console.WriteLine("TDEA Monte Carlo TCBC Mode Encrypt:")
Console.WriteLine("KY=" & " " & Cnv.ToHex(abKey))
Console.WriteLine("IV=" & " " & Cnv.ToHex(abInitV))
Console.WriteLine("PT=" & " " & Cnv.ToHex(aBlock))
nRet = oTdea.InitEncrypt(abKey, Mode.CBC, abInitV)
If nRet <> 0 Then
    nRet = oTdea.ErrCode()
    Console.WriteLine("TDEA_Init Failed: " & General.ErrorLookup(nRet))
    Exit Sub
End If
' [VB.NET] We must treat byte arrays differently in VB.NET...
ReDim aNext(aBlock.Length - 1)
ReDim aLast(aBlock.Length - 1)
''aNext = aBlock
Buffer.BlockCopy(aBlock, 0, aNext, 0, aBlock.Length)
' Do 10,000 times
For j = 0 To 9999
    ''aBlock = aNext
    Buffer.BlockCopy(aNext, 0, aBlock, 0, aNext.Length)
    aBlock = oTdea.Update(aBlock)
    If j = 0 Then
        ''aNext() = abInitV()
        Buffer.BlockCopy(abInitV, 0, aNext, 0, abInitV.Length)
    Else
        ''aNext() = aLast()
        Buffer.BlockCopy(aLast, 0, aNext, 0, aLast.Length)
    End If
    ''aLast() = aBlock()
    Buffer.BlockCopy(aBlock, 0, aLast, 0, aBlock.Length)
Next
Console.WriteLine("CT=" & " " & Cnv.ToHex(aBlock))
Console.WriteLine("OK=" & " " & sCorrect)
oTdea.Dispose()
Debug.Assert(sCorrect.ToUpper = Cnv.ToHex(aBlock))

[Contents]

[HOME]   [NEXT: WIPE_Data...]

Copyright © 2009-20 D.I. Management Services Pty Ltd. All rights reserved.