2014-11-03 55 views
1

我使用下面的代码访问令牌的认证文件,模块得到的信息有关的令牌,.NET Net.pkcs11.dll抛出异常system.formatexception在objToken.TokenInfo

 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) 

在网。 Sf.Pkcs11.Wrapper.Pkcs11Module.FindObjectsInit(UInt32的hSession,CK_ATTRIBUTE [] pTemplate) 在Net.Sf.Pkcs11.Session.FindObjectsInit(P11Attribute [] ATTRS) 在ECDecryptor.CSPDec.Decrypt(字节[]消息时,字节[ ] pad,Byte [] modulus)in c:\ Users \ vaishali.pathare \ Desktop \ Token \ decryptor_NewChanges \ decryptor_tool_source_2048 \ CSP Registrar Decryptor Utility 2048 \ Decryptor \ CSPDec.cs:line 100 P11Object [] keyObject s = session.FindObjects(10);

回答

0

下面的代码使用的Cryptoki工程获得256位RSA密钥 同我使用Net.pkcs11.dll

试图

公共字节[]解密(字节[]消息,字节[]垫,字节[]模量) {

 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);