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
' ==========================================================================
' RSA encryption/decryption
' Description: 
' If DES_RSA library is loaded, the DES, 3DES and RSA can be implemented by the following steps:
' (1) creat RSA key pair
' (2) encrypt/decrypt based on public key file ID or private key file ID
' Note:
' (1) the unit of key is bit, only support 1024 or 512 bits key
' (2) the default value of e is 65537, and cannot be changed
' (3) when encrypt data, if the data is longer than n, please set the high byte of data to 0
' ==========================================================================


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 = 128


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. creat RSA key
errcode = DIC_Set(cmddata(0), RSA_PUBKEYID, BY_VALUE, &H7F01, Null) 'public key file ID
errcode = DIC_Set(cmddata(0), RSA_PUBKEYSIZE, BY_VALUE, 1024, Null) 'public key size in bit
errcode = DIC_Set(cmddata(0), RSA_PIVKEYID, BY_VALUE, &H7F02, Null) 'private key file ID
errcode = DIC_Command(hic, RSAGENKEY, cmddata(0))

For i = 0 To 128
    buffer(i) = i
Next i
line = "RSA encryption/decryption£º"
List1.List(curline) = line
curline = curline + 1

    'RSA encryption, either public key or private key
    errcode = DIC_Set(cmddata(0), RSA_KEYFILEID, BY_VALUE, &H7F01, Null) 'use public key here
    errcode = DIC_Set(cmddata(0), RSA_DATA, BY_ARRAY Or 128, 0, buffer(0))
    errcode = DIC_Command(hic, RSAENC, cmddata(0))
    
    wRetLen = DIC_Get(cmddata(0), AFTER_ENCDEC_DATA, BY_ARRAY, buffer(0))
    List1.List(curline) = "encrypted data"
    curline = curline + 1
    line = ""
    For i = 0 To wRetLen - 1
    
    line = line + Hex(buffer(i)) + " "
    If (((i + 1) Mod 16) = 0) Then
        List1.List(curline) = line
        curline = curline + 1
        line = ""
    End If
    Next i
    
       
    'RSA decrypt
    errcode = DIC_Set(cmddata(0), RSA_KEYFILEID, BY_VALUE, &H7F02, Null)
    errcode = DIC_Set(cmddata(0), RSA_DATA, BY_ARRAY Or 128, 0, buffer(0))
    errcode = DIC_Command(hic, RSADEC, cmddata(0))
   
    wRetLen = DIC_Get(cmddata(0), AFTER_ENCDEC_DATA, BY_ARRAY, buffer(0))
    List1.List(curline) = "plaintext"
    curline = curline + 1
    line = ""
    For i = 0 To wRetLen - 1
    
    line = line + Hex(buffer(i)) + " "
    If (((i + 1) Mod 16) = 0) Then
        List1.List(curline) = line
        curline = curline + 1
        line = ""
    End If
    Next i


' m. close
errcode = DIC_Close(hic)

End Sub

