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
' ==========================================================================
' Demo: Secure File Transfe
' Caution: no error check for the sake of briefness and understandability
' Tips:
' The ROCKEY5 dongle supports Secure File Transfer (SFT). The Remote Update Management (RUM)
' function will verify the user's identity, but it cannot update the dongle algorithm because the
' end user does not have access to the super password which is required to write to the dongle.
' The SFT process allows the software developer to securely update dongle algorithms or data files
' without compromising the super password.
' See the SFT operational overview described below:
' 1. The software developer generates the "cryptography file" by using with own dongle.
' 2. Transfer the file to the user;
' 3. The user transfers the cryptography file to dongle with the program provided by the software
'    developer. The dongle converts the file to clear text file internally.
' We adopts the following security policies:
' 1. The algorithms of cryptography files and clear text files are completely confidential, not
'    open to anyone;ĄŁ
' 2. The generation of cryptography files and clear text files is 100% performed inside dongle,
'    no other external work is involved;ĄŁ
' 3. The encryption / decryption key is related to dongle super password and the management code.
'    It is not necessary to worry that some one else could create a cryptography file valid in the
'    end user's dongle
' 4. The content of the cryptography file can not be modified, the file ID, its attribute, and
'    others are part of the cryptography file, and they are all protected by the file check
'    mechanism.
' Attention::
' 1. The software developer can only generate the cryptography file after super password
'    verification. The verified super password is part of the encryption key. That is to say,
'    the super password of the end user should be the same with the super password which the
'    software developer uses to generate the cryptography file.
' 2. During the cryptography file generation process, a temporary file with the same name will be
'    created in the software developer's dongle, because the IC card can transfer at most data of
'    250 bytes once, and many files are larger than that size. Therefore, dongle should have enough
'    space for the temporary file generation. The file will not be deleted automatically after the
'    "cryptography file" generation call and the return of the content; you need to delete it
'    manually.
' ==========================================================================
' Here two security files will be created: an executable file and a data file. The function
' of the executable file is to read and return the content of the data file.

'
' Executable file readdata.c, input parameters are the internal file IDs of dongle to be accessed
' Output parameters are the contents of the internal file of dongle.
'void main(void)
'{
' word fileID;
' word fileSize;
' byte b_fileSize;
' byte xdata out[30];
' byte xdata result[100],ret;
'
' get_input(&fileID,0,1,1);
' open_file(&fileID, 0);
' get_file_infor(out, 1);
' fileSize = (out[5]<<8)|out[4];
' read_file(0,result,fileSize);
' b_fileSize=(unsigned char)fileSize;
'
'set_response(b_fileSize, result);
' exit();
'}
' ==========================================================================

Private Sub TestBtn_Click()
Dim errcode, num, hic, i As Long
Dim cmddata(0 To 1024) As Byte 
Dim buffer(0 To 512) As Byte 
Dim exesize, datasize, readsize As Long
Dim c_exefile(0 To 1024) As Byte
Dim c_datafile(0 To 512) As Byte
Dim line As String

Dim curline As Integer

' convert from  readdata.bin
Const READDATA_SIZE = 276
Dim g_progReadData(0 To READDATA_SIZE - 1) As Byte
Dim pos As Long
Dim varArray() As Variant

pos = 0



varArray = Array(&H2, &H1, &H4, &H75, &HC, &H0, &H75, &HD, &H0, &H75, &HE, &H8, &HE4, &HF5, &HF, &H75)
pos = ArrayByte(varArray, pos, g_progReadData)
varArray = Array(&H10, &H1, &H75, &H11, &H1, &H12, &H0, &HA0, &H75, &HC, &H0, &H75, &HD, &H0, &H75, &HE)
pos = ArrayByte(varArray, pos, g_progReadData)
varArray = Array(&H8, &HE4, &HF5, &HF, &H12, &H0, &HBF, &H75, &HC, &H1, &H75, &HD, &H0, &H75, &HE, &H0)
pos = ArrayByte(varArray, pos, g_progReadData)
varArray = Array(&H75, &HF, &H1, &H12, &H0, &HED, &H90, &H0, &H4, &HE0, &HFE, &HA3, &HE0, &HFD, &HEE, &HF5)
pos = ArrayByte(varArray, pos, g_progReadData)
varArray = Array(&HA, &HED, &HF5, &HB, &HE4, &HF5, &HC, &HF5, &HD, &H75, &HE, &H1, &H75, &HF, &H0, &H75)
pos = ArrayByte(varArray, pos, g_progReadData)
varArray = Array(&H10, &HC, &H85, &HA, &H11, &H85, &HB, &H12, &H12, &H0, &H7D, &HD3, &HE5, &HB, &H94, &HFF)
pos = ArrayByte(varArray, pos, g_progReadData)
varArray = Array(&HE5, &HA, &H94, &H0, &H40, &H4, &H7F, &HFF, &H80, &H2, &HAF, &HB, &H8F, &HC, &H75, &HD)
pos = ArrayByte(varArray, pos, g_progReadData)
varArray = Array(&H1, &H75, &HE, &H0, &H75, &HF, &HC, &H12, &H0, &HD6, &H2, &H1, &H10, &H12, &HFF, &H0)
pos = ArrayByte(varArray, pos, g_progReadData)
varArray = Array(&HE5, &HC, &H24, &H0, &HE5, &HD, &H24, &H1, &HE5, &HE, &H24, &H2, &HE5, &HF, &H24, &H3)
pos = ArrayByte(varArray, pos, g_progReadData)
varArray = Array(&HE5, &H10, &H24, &H4, &HE5, &H11, &H24, &H5, &HE5, &H12, &H24, &H6, &H12, &HFF, &H3, &H22)
pos = ArrayByte(varArray, pos, g_progReadData)
varArray = Array(&H12, &HFF, &H0, &HE5, &HC, &H24, &H0, &HE5, &HD, &H24, &H1, &HE5, &HE, &H24, &H2, &HE5)
pos = ArrayByte(varArray, pos, g_progReadData)
varArray = Array(&HF, &H24, &H3, &HE5, &H10, &H24, &H4, &HE5, &H11, &H24, &H5, &H12, &HFF, &HB, &H22, &H12)
pos = ArrayByte(varArray, pos, g_progReadData)
varArray = Array(&HFF, &H0, &HE5, &HC, &H24, &H0, &HE5, &HD, &H24, &H1, &HE5, &HE, &H24, &H2, &HE5, &HF)
pos = ArrayByte(varArray, pos, g_progReadData)
varArray = Array(&H24, &H3, &H12, &HFF, &H2, &H22, &H12, &HFF, &H0, &HE5, &HD, &H24, &H0, &HE5, &HE, &H24)
pos = ArrayByte(varArray, pos, g_progReadData)
varArray = Array(&H1, &HE5, &HF, &H24, &H2, &HE5, &HC, &H24, &H3, &H12, &HFF, &HA, &H22, &H12, &HFF, &H0)
pos = ArrayByte(varArray, pos, g_progReadData)
varArray = Array(&HE5, &HC, &H24, &H0, &HE5, &HD, &H24, &H1, &HE5, &HE, &H24, &H2, &HE5, &HF, &H24, &H3)
pos = ArrayByte(varArray, pos, g_progReadData)
varArray = Array(&H12, &HFF, &H6, &H22, &H78, &H7F, &HE4, &HF6, &HD8, &HFD, &H75, &H81, &H12, &H2, &H0, &H3)
pos = ArrayByte(varArray, pos, g_progReadData)
varArray = Array(&H12, &HFF, &HD, &H22)
pos = ArrayByte(varArray, pos, g_progReadData)


curline = 0

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. Change to super user status (Only super users are allowed to create executable files)
For i = 0 To 7
    cmddata(i) = 255
Next i
errcode = DIC_Command(hic, CHECK_SUPER_PASS, cmddata(0))

' b. Format ROCKEY, all files and directorys will be cleared.
'    You can set ATR and VOLUME here.
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 the cryptography file of the executable file
'    Difference between creating cryptography file and common file is that after setting the
'    relevant file attributes FILE_DATA call is needed to transfer the file content.
errcode = DIC_Set(cmddata(0), FILL, 512, 0, 0&)   ' Clear buffer
errcode = DIC_Set(cmddata(0), FILE_ID, BY_VALUE, &H23, 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, FILEATTR_EXEC, 0&)    ' File attr
errcode = DIC_Set(cmddata(0), FILE_SIZE, BY_VALUE, READDATA_SIZE, 0&)         ' File size
Call Str2Array("6088", buffer)
errcode = DIC_Set(cmddata(0), FILE_NAME, BY_ARRAY, 0, buffer(0))              ' File name
errcode = DIC_Set(cmddata(0), FILE_DATA, BY_ARRAY + READDATA_SIZE, 0, g_progReadData(0))    ' File content
errcode = DIC_Command(hic, CRYPTOTEXT_FILE, cmddata(0))
exesize = DIC_Get(cmddata(0), FILE_DATA, BY_ARRAY, c_exefile(0))

' d. Create the cryptography file of the data file, data content is 0-9 repeated 8 times.
For i = 0 To 79
    buffer(i) = &H30 + (i Mod 10)
Next i
errcode = DIC_Set(cmddata(0), FILL, 256, 0, 0&)                   ' File ID
errcode = DIC_Set(cmddata(0), FILE_ID, BY_VALUE, &H57, 0&)        ' File class
errcode = DIC_Set(cmddata(0), FILE_CLASS, BY_VALUE, &HFF, 0&)     ' File attr
errcode = DIC_Set(cmddata(0), FILE_ATTRIBUTE, BY_VALUE, FILEATTR_NORMAL, 0&)  ' File size
errcode = DIC_Set(cmddata(0), FILE_SIZE, BY_VALUE, 80, 0&)        ' File name
Call Str2Array("File1", buffer)
errcode = DIC_Set(cmddata(0), FILE_NAME, BY_ARRAY, 0, buffer(0))     ' File content
errcode = DIC_Set(cmddata(0), FILE_DATA, BY_ARRAY + 80, 0, buffer(0))  ' File content
errcode = DIC_Command(hic, CRYPTOTEXT_FILE, cmddata(0))
datasize = DIC_Get(cmddata(0), FILE_DATA, BY_ARRAY, c_datafile(0))

' e. Select the temporary file of the executable cryptography file.(0x0023)
errcode = DIC_Set(cmddata(0), FILE_ID, BY_VALUE, &H23, 0&)
errcode = DIC_Command(hic, SET_CURRENT_FILE, cmddata(0))

' f. Delete temporary file of the executable cryptography file (0x0023)
errcode = DIC_Command(hic, REMOVE_FILE, cmddata(0))

' g. Select the temporary file of the data cryptography file (0x0057)
errcode = DIC_Set(cmddata(0), FILE_ID, BY_VALUE, &H57, 0&)
errcode = DIC_Command(hic, SET_CURRENT_FILE, cmddata(0))

' h. Delete the temporary file of the data cryptography file (0x0057)
errcode = DIC_Command(hic, REMOVE_FILE, cmddata(0))

' i. Lower the super password verified status to common user status, as it is unnecessary
'    for super user to create the clear text file
errcode = DIC_Close(hic)
errcode = DIC_Open(hic, 0&)

' j. Create the clear text file of the executable file
For i = 0 To exesize - 1
    cmddata(i) = c_exefile(i)
Next i
errcode = DIC_Command(hic, PLAINTEXT_FILE, cmddata(0))

' k. Create the clear text file of the data file
For i = 0 To datasize - 1
    cmddata(i) = c_datafile(i)
Next i
errcode = DIC_Command(hic, PLAINTEXT_FILE, cmddata(0))

' l. Run the executable file (0x0023) to get the content of the data
'    file (0x0057) and return
errcode = DIC_Set(buffer(0), 0, 2, &H57, 0&)              ' Input the file ID
errcode = DIC_Set(cmddata(0), RUN_DATA, 2, &H6088, buffer(0))  ' Set the run data
errcode = DIC_Command(hic, RUN, cmddata(0))
readsize = DIC_Get(cmddata(0), RUN_DATA, BY_ARRAY, buffer(0))    ' Fetch data after execution
For i = 0 To readsize - 1
    line = line + Chr(buffer(i))
    If (((i + 1) Mod 10) = 0) Then
        List1.List(curline) = line
        curline = curline + 1
        line = ""
    End If
Next i

' m. Close dongle
errcode = DIC_Close(hic)

End Sub

