我使用以下代码访问令牌中的certifcate,Module获取有关令牌的信息,
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 []消息,Byte [] pad,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);
答案 0 :(得分:0)
下面的代码使用Cryptoki可以获取256位RSA密钥 我正在尝试使用Net.pkcs11.dll
public byte [] Decrypt(byte [] message,byte [] pad,byte [] modulus) {
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;
}
答案 1 :(得分: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);