.NET Net.pkcs11.dll создает исключение system.formatexception в объекте objToken.TokenInfo

1

Я использую следующий код для доступа к сертификату в токене, модуль получает информацию о токене,

        Module module = Module.GetInstance(@"C:\WINDOWS\system32\eTPKCS11.dll");

        module.Initialize();

        Slot[] slots = module.GetSlotList(true);

        if (slots.Length== 0)
        {
            MessageBox.Show("No slot available");
            return null;
        }

        Token token = null;
        for (int i = 0; i < slots.Length; i++)
        {
            if (slots[i].SlotInfo.IsTokenPresent)
                token = slots[i].Token; // slots[i].token assigns token to Token object
        }

        token.TokenInfo;// throws exception at this line

        Session session = token.OpenSession(true);

        PIN pin = new PIN();
        pin.ShowDialog();

        // Executes the login passing the user PIN
        session.Login(UserType.USER,pin.Pin.ToCharArray());

        // Find RSA Private keys
        session.FindObjectsInit(new P11Attribute[]{new ObjectClassAttribute(CKO.PRIVATE_KEY),new KeyTypeAttribute(CKK.RSA)});  // hence when calling FindObjectInit method it throws ATTRIBUTE_VALUE_INVALID , stackTrace    at Net.Sf.Pkcs11.Wrapper.Pkcs11Module.checkCKR(CKR retVal)

в Net.Sf.Pkcs11.Wrapper.Pkcs11Module.FindObjectsInit(UInt32 hSession, CK_ATTRIBUTE [] pTemplate) в Net.Sf.Pkcs11.Session.FindObjectsInit(P11Attribute [] attrs) в ECDecryptor.CSPDec.Decrypt(Byte [] message, Byte [], модуль байта [] в c:\Users\vaishali.pathare\Desktop\Token\decryptor_NewChanges\decryptor_tool_source_2048\CSP Registrar Decryptor Utility 2048\Decryptor\CSPDec.cs: строка 100 P11Object [] keyObjects = session.FindObjects( 10);

Теги:

2 ответа

0
Лучший ответ

Ниже кода Использование Cryptoki работает для получения 256-битного ключа RSA. То же самое я пытаюсь использовать Net.pkcs11.dll

public byte [] Decrypt (byte [] message, byte [] pad, byte [] module) {

        Cryptoki cryptoki = new Cryptoki("eTPKCS11.dll");

        cryptoki.Initialize();

        SlotList slots = cryptoki.Slots;
        if (slots.Count == 0)
        {

            return null;
        }

        Token token = null;
        for (int i = 0; i < slots.Count; i++)
        {
            if (slots[i].IsTokenPresent)
                token = slots[i].Token;
        }

        // Searchs for an RSA private key object
        // Sets the template with its attributes
        CryptokiCollection template_PrivateKey = new CryptokiCollection();
        template_PrivateKey.Add(new ObjectAttribute(ObjectAttribute.CKA_CLASS, CryptokiObject.CKO_PRIVATE_KEY));
        template_PrivateKey.Add(new ObjectAttribute(ObjectAttribute.CKA_KEY_TYPE, Key.CKK_RSA));

        CryptokiCollection template_PublicKey = new CryptokiCollection();
        template_PublicKey.Add(new ObjectAttribute(ObjectAttribute.CKA_CLASS, CryptokiObject.CKO_PUBLIC_KEY));
        template_PublicKey.Add(new ObjectAttribute(ObjectAttribute.CKA_KEY_TYPE, Key.CKK_RSA));

        // Opens a read/write serial session
        Session session = token.OpenSession(Session.CKF_SERIAL_SESSION | SessionInfo.CKF_RW_SESSION);

        PIN pin = new PIN();
        pin.ShowDialog();

        // Executes the login passing the user PIN
        int nRes = session.Login(Session.CKU_USER,pin.Pin);
        if (nRes != 0)
        {
            MessageBox.Show("Wrong PIN");
            return null;
        }

        // Launchs the search specifying the template just created
        CryptokiCollection obj_PrivKey = session.Objects.Find(template_PrivateKey, 10);
        // Launchs the search specifying the template just created
        CryptokiCollection obj_PubKey = session.Objects.Find(template_PublicKey, 10);
        //CryptokiObjects o1 = session.Objects;

        RSAPrivateKey privateKey = null;
        //RSAPublicKey publicKey;
        //RSAPrivateKey tempKey=null;


        for (int i = 0; i < obj_PrivKey.Count; i++)
        {
            privateKey =(RSAPrivateKey)obj_PrivKey[i];
            if (Utilities.CompareBytes(privateKey.Modulus, modulus))
            {
                break;
            }
        }


        if (privateKey == null)
        {
            MessageBox.Show(" No corresponding Private key found ");
            return null;
        }

        Cryptware.NCryptoki.Mechanism m_encrypt = Mechanism.RSA_X_509;
        byte[] aeskey = null;
        try
        {
            int re = session.DecryptInit(Mechanism.RSA_X_509, privateKey);

            byte[] dec = session.Decrypt(message);
           IAsymmetricBlockCipher cipher = new OaepEncoding(new RsaEngine(),new Sha256Digest(),pad);
                      Org.BouncyCastle.Math.BigInteger mod = new Org.BouncyCastle.Math.BigInteger(1,privateKey.Modulus);
            Org.BouncyCastle.Math.BigInteger exp=new Org.BouncyCastle.Math.BigInteger("1",16);
            RsaKeyParameters p_Temp = new RsaKeyParameters(false, mod, exp);

            cipher.Init(false, p_Temp);

           aeskey = cipher.ProcessBlock(dec, 0,dec.Length);



       }
        catch (Exception ex)
        {

        }
        finally
        {
            session.Logout();
       '
        }
        return aeskey;
    }
0

эта строка неверна, вы можете так:

замените эту строку:

token.TokenInfo;// throws exception at this line

от

// Prints all information relating to the token
TokenInfo tinfo = token.Info;
Console.WriteLine(tinfo.Label);
Console.WriteLine(tinfo.ManufacturerID);
Console.WriteLine(tinfo.Model);
Console.WriteLine(tinfo.SerialNumber);
Console.WriteLine(tinfo.HardwareVersion);

Ещё вопросы

Сообщество Overcoder
Наверх
Меню