VERSION 5.00
Begin VB.Form Form1 
   Caption         =   "Form1"
   ClientHeight    =   4590
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   7875
   LinkTopic       =   "Form1"
   ScaleHeight     =   4590
   ScaleWidth      =   7875
   StartUpPosition =   3  'Windows Default
   Begin VB.CommandButton TestBtn 
      Caption         =   "Test"
      Height          =   375
      Left            =   3000
      TabIndex        =   1
      Top             =   4080
      Width           =   1575
   End
   Begin VB.ListBox List1 
      Height          =   3765
      Left            =   120
      TabIndex        =   0
      Top             =   120
      Width           =   7575
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
' ==========================================================================
' DES and 3DES Encryption and Decryption
' Description: 
' If DES_RSA library is loaded, the DES, 3DES and RSA can be implemented by the following steps:
'  (1) creat a internal data file (supper password is needed)
'  (2) write DES, RSA key (supper password is needed)
'  (3) encrypt/decrypt based on file ID and key ID. 
'	when encrypt, the length of data should be A*8, where A is an integer, otherwise the data will be filled automatically.
' 	when decrypt, please cut off the corresponding data.
' Note:
' (1)DES Key is 8 bytes, and 3DES key is 16 bytes
' (2)The struct for DES and 3DES key
'	DES Key struct (10 bytes): key ID (1 byte)+key length(1 byte, value is 8)+key(8 bytes)
'	3DES Key struct (18 bytes): key ID (1 byte)+key length(1 byte, value is 16)+key(16 byte)
' ==========================================================================

Private Sub TestBtn_Click()
Dim errcode, num, hic, i As Long
Dim cmddata(0 To 1024) As Byte ' command buffer
Dim buffer(0 To 512) As Byte 'user buffer

Dim line As String
Dim curline As Integer

Const bufLen = 8
Dim key(0 To 30) As Byte
For i = 0 To 29
    key(i) = i
Next i

key(0) = 0
key(1) = 8
key(10) = 1
key(11) = 16


num = DIC_Find()
For i = 0 To num - 1
    hic = DIC_Open(i, 0&)
    If (hic >= 0) Then
        Exit For
    End If
Next i
If (i = num) Then
    Exit Sub
End If

' a. verify supper password
For i = 0 To 7
    cmddata(i) = 255
Next i
errcode = DIC_Command(hic, CHECK_SUPER_PASS, cmddata(0))

' b. format the ROCKEY, all info will be deleted, you can set new ROCKEY info
Call Str2Array("DICSYSTEM", buffer)
errcode = DIC_Set(cmddata(0), VOLUME, BY_ARRAY, 0, buffer(0))
Call Str2Array("DIC Co.", buffer)
errcode = DIC_Set(cmddata(0), ATR, BY_ARRAY, 0, buffer(0))
errcode = DIC_Command(hic, FORMAT_CARD, cmddata(0))

' c. create a key file for DES or TDES(3DES)
errcode = DIC_Set(cmddata(0), FILL, 512, 0, 0&)   				' clear
errcode = DIC_Set(cmddata(0), FILE_ID, BY_VALUE, &H5000, 0&)                    ' file ID
errcode = DIC_Set(cmddata(0), FILE_CLASS, BY_VALUE, &HFF, 0&)                 	' file class
errcode = DIC_Set(cmddata(0), FILE_ATTRIBUTE, BY_VALUE, 0, 0&)    		' file attribute, internal
errcode = DIC_Set(cmddata(0), FILE_SIZE, BY_VALUE, 30, 0&)         		' file size
Call Str2Array("desKey", buffer)
errcode = DIC_Set(cmddata(0), FILE_NAME, BY_ARRAY, 0, buffer(0))              	' file name

errcode = DIC_Command(hic, CREATE_FILE, cmddata(0))

errcode = DIC_Set(cmddata(0), WRITE_DATA, 30, 0, key(0))
errcode = DIC_Command(hic, WRITE_FILE, cmddata(0))


' i. cancel supper password status
errcode = DIC_Close(hic)
errcode = DIC_Open(hic, 0&)

'================================des encrypt =8=========================================//
For i = 0 To 7
    buffer(i) = i
Next i

For j = 0 To 1 'j is key id

errcode = DIC_Set(cmddata(0), DES_KEYFILEID, 0, &H5000, Null)
errcode = DIC_Set(cmddata(0), DES_KEYINDEX, 0, j, Null)
errcode = DIC_Set(cmddata(0), DES_DATA, BY_ARRAY Or bufLen, 0, buffer(0))
errcode = DIC_Command(hic, DESENC, cmddata(0))
wRet = DIC_Get(cmddata(0), AFTER_ENCDEC_DATA, BY_ARRAY, buffer(0))

If (j = 0) Then
    List1.List(curline) = "des encryption/decryption:"
Else
    List1.List(curline) = "3des encryption/decryption"
End If

curline = curline + 1
line = "encrypted data£º"
For i = 0 To wRet - 1
    line = line + Hex(buffer(i)) + " "
Next i

List1.List(curline) = line
curline = curline + 1

'================================des decrypt =8=========================================//
errcode = DIC_Set(cmddata(0), DES_KEYFILEID, 0, &H5000, Null)
errcode = DIC_Set(cmddata(0), DES_KEYINDEX, 0, j, 0)
errcode = DIC_Set(cmddata(0), DES_DATA, BY_ARRAY Or bufLen, 0, buffer(0))
errcode = DIC_Command(hic, DESDEC, cmddata(0))
wRet = DIC_Get(cmddata(0), AFTER_ENCDEC_DATA, BY_ARRAY, buffer(0))

line = "plaintext:£º"
For i = 0 To wRet - 1
    line = line + Hex(buffer(i)) + " "
Next i

List1.List(curline) = line
curline = curline + 1

Next j
' m. close
errcode = DIC_Close(hic)

End Sub

