C# .NET NET.pkcs11.dll在objToken.TokenInfo中引发异常system.formatexception
我使用以下代码访问令牌中的Certificate,模块获取关于令牌的信息C# .NET NET.pkcs11.dll在objToken.TokenInfo中引发异常system.formatexception,c#,C#,我使用以下代码访问令牌中的Certificate,模块获取关于令牌的信息 Module module = Module.GetInstance(@"C:\WINDOWS\system32\eTPKCS11.dll"); module.Initialize(); Slot[] slots = module.GetSlotList(true); if (slots.Length== 0) {
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)
Module Module=Module.GetInstance(@“C:\WINDOWS\system32\eTPKCS11.dll”);
初始化()模块;
Slot[]slots=module.GetSlotList(true);
if(slots.Length==0)
{
MessageBox.Show(“没有可用插槽”);
返回null;
}
令牌=null;
对于(int i=0;i
位于Net.Sf.Pkcs11.Wrapper.Pkcs11Module.FindObjectsInit(UInt32会话,CK_属性[]pTemplate)
位于Net.Sf.Pkcs11.Session.FindObjectsInit(P11Attribute[]attrs)
在c:\Users\vaishali.pathare\Desktop\Token\decryptor\u NewChanges\decryptor\u tool\u source\u 2048\CSP Registrator decryptor Utility 2048\decryptor\CSPDec.cs中的ECDecryptor.CSPDec.Decrypt(字节[]消息,字节[]pad,字节[]模数):第100行
P11Object[]keyObjects=session.findObject(10) 下面使用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;
}
Cryptoki Cryptoki=newcryptoki(“eTPKCS11.dll”);
cryptoki.Initialize();
SlotList slots=cryptoki.slots;
如果(slots.Count==0)
{
返回null;
}
令牌=null;
对于(int i=0;i
这一行是错的,你可以这样叫:
替换此行:
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);