Welcome Guest, you are in: Login

NCryptoki Wiki

RSS RSS

Navigation





Search the wiki
»

Cryptware
PoweredBy

VB6 Tutorial

RSS
Modified on 2013/01/27 21:28 by Administrator Categorized as VB6
This tutorial shows briefly the following procedures:

  • Setup NCryptoki
  • Create and initialize a Cryptoki object
  • Get the list of available slots
  • Open a session
  • Login
  • Generate an RSA key pai
  • Search for an object
  • Encrypt some text.

Setup NCryptoki in Visual Basic 6

In order to use NCryptoki in a Visual Basic 6 application it must be registered by regasm. Open a DOS shell and type the following lines:

regasm /tlb /codebase /NCryptoki.dll
regasm /tlb /codebase /NCryptokiMngd.dll

Then to use NCryptoki in your project you must add the type library ncryptoki.tlb in the refences of your Visual Basic project

 
Dim ctoki As Cryptoki
Dim slots As SlotList
Dim slt As Slot
Dim tkn As Token
Dim sess As Session
Dim sessInfo As Session
Dim objs As CryptokiObjects
Dim I As Integer

' Creates new Cryptoki object
Set ctoki = New Cryptoki

' attach to pkcs11 module
ctoki.Attach ("SmaOScki.dll")

' initialize
ctoki.Initialize

'  Get available slots
Set slots = ctoki.slots

' Get the first slot
Set slt = slots.GetSlot(1)
    
' Wait for a smart card inserted
Do While Not sltInfo.IsTokenPresent
    MsgBox ("Insert Smart Card")
    ctoki.WaitForSlotEvent (0)
Loop

' Get the token
Set tkn = slt.Token

' Open a session
Set sess = tkn.OpenSession(CKF_RW_SESSION Or CKF_SERIAL_SESSION)

' Login
sess.Login CKU_USER, "64005666"

' Prepares the templates to generate a key pair
Dim attrListPub As CryptokiCollection
Dim attrListPri As CryptokiCollection
Dim mech As Mechanism
Dim att As ObjectAttribute

Set attrListPub = New CryptokiCollection
Set attrListPri = New CryptokiCollection

' creates attributes object
Set att = New ObjectAttribute
att.Set CKA_CLASS, CKO_PUBLIC_KEY
attrListPub.Add att

Set att = New ObjectAttribute
att.Set CKA_TOKEN, True
attrListPub.Add att

Set att = New ObjectAttribute
att.Set CKA_PRIVATE, True
attrListPub.Add att

Set att = New ObjectAttribute
att.Set CKA_ENCRYPT, True
attrListPub.Add att

Set att = New ObjectAttribute
att.Set CKA_LABEL, "My Key"
attrListPub.Add att

' creates attributes object
Set att = New ObjectAttribute
att.Set CKA_CLASS, CKO_PRIVATE_KEY
attrListPri.Add att

Set att = New ObjectAttribute
att.Set CKA_TOKEN, True
attrListPri.Add att

Set att = New ObjectAttribute
att.Set CKA_PRIVATE, True
attrListPri.Add att

Set att = New ObjectAttribute
att.Set CKA_DECRYPT, True
attrListPri.Add att

Set att = New ObjectAttribute
att.Set CKA_LABEL, "My Key"
attrListPri.Add att

' Set mechanism
Set mech = New Mechanism
Dim val(1) As Byte
val(0) = 1
mech.Set CKM_RSA_PKCS_KEY_PAIR_GEN, val(0)

Dim pubKey As PublicKey
Dim priKey As PrivateKey

sess.GenerateKeyPair mech, attrListPub, attrListPri, pubKey, priKey

' Find Object just created
Dim attrList As CryptokiCollection

Set att = New ObjectAttribute
att.Set CKA_CLASS, CKO_PUBLIC_KEY
attrList.Add att

Set att = New ObjectAttribute
att.Set CKA_TOKEN, True
attrList.Add att

Set att = New ObjectAttribute
att.Set CKA_PRIVATE, True
attrList.Add att

Dim objList As CryptokiCollection
Set objList = objs.Find(attrList, 4)

' prepare buffer to encrypt
Dim toEncrypt(10) As Byte
Dim encrypted() As Byte

For I = 0 To 10
    toEncrypt(I) = &H41 ' A
Next

mech.Set CKM_RSA_PKCS, Nothing

sess.EncryptInit mech, objList.Item(0)

encrypted = sess.Encrypt(toEncrypt(0))

ScrewTurn Wiki version 3.0.4.560. Some of the icons created by FamFamFam.