我使用以下代码访问令牌证书,模块获取有关令牌的信息,
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 Net.Sf.Pkcs11.Session.FindObjectsInit(P11Attribute[] hSession,CK_ATTRIBUTE[] pTemplate) at ECDecryptor.CSPDec.Decrypt(Byte[] message,Byte[] pad,Byte[]模数)在c:\Users\vaishali.pathare\Desktop\Token\decryptor_NewChanges\decryptor_tool_source_2048\CSP注册解密器Utility 2048\Decryptor\CSPDec.cs:line 100 P11 Object[] keyObjects =session.FindObjects(10)中;
发布于 2014-11-03 07:34:23
下面使用Cryptoki的代码用于获取256位RSA密钥,与我使用Net.pkcs11.dll尝试的相同
公共byte[]解密( byte[]消息,byte[] pad,byte[]模){
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;
}发布于 2016-05-27 17:47:13
这句话错了,你可以这样打:
替换这一行:
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);https://stackoverflow.com/questions/26709617
复制相似问题