0
static string pkcs11LibraryPath = @"C:\Windows\System32\eTPKCS11.dll";    
using (Pkcs11 pkcs11 = new Pkcs11(pkcs11LibraryPath, AppType.SingleThreaded))
{
    // Get list of available slots with token present
    List<Slot> slots = pkcs11.GetSlotList(SlotsType.WithTokenPresent);

    // Find first slot with token present
    Slot slot = slots[0];

    // Open RO session
    using (Session session = slot.OpenSession(SessionType.ReadWrite))
    {
        session.Login(CKU.CKU_USER, "654321");//HSM:123456

        for (int i = 0; i <= slot.GetMechanismList().Count - 1; i++)
        {
            Console.WriteLine(slot.GetMechanismList()[i].ToString());
        }

        // Prepare attribute template that defines search criteria
        List<ObjectAttribute> objectAttributes = new List<ObjectAttribute>();
        //objectAttributes.Add(new ObjectAttribute(CKA.CKA_LABEL, "test"));//HSM:KEY_028_04
        objectAttributes.Add(new ObjectAttribute(CKA.CKA_TOKEN, true));

        // Initialize searching
        session.FindObjectsInit(objectAttributes);

        // Get search results
        List<ObjectHandle> foundObjects = session.FindObjects(2);

        // Terminate searching
        session.FindObjectsFinal();

        ObjectHandle objectHandle = foundObjects[0];

        byte[] iv = Encoding.UTF8.GetBytes("00000000");
        byte[] inputData = Encoding.UTF8.GetBytes("data to encrypt.");


         Mechanism mechanism = new Mechanism(CKM.CKM_DES3_CBC, iv);;//HSM: CKM_DES3_CBC

        byte[] result = session.Encrypt(mechanism, objectHandle, inputData);

        Console.WriteLine(Convert.ToBase64String(result));
    }
}

エラーが発生Net.Pkcs11Interop.Common.Pkcs11Exception: 'Method C_EncryptInit returned CKR_KEY_TYPE_INCONSISTENT'しましたbyte[] result = session.Encrypt(mechanism, objectHandle, inputData);

私はセーフネット 5100 etoken を使用しています。

4

1 に答える 1