PBKDF2 密钥生成使用 pkcs11interop,c#,windows visual studio 2010

PBKDF2 key generation using pkcs11interop, c#, windows visual studio 2010

如何在 pkcs11interop 中为 PBKDF2 密钥生成制作属性模板。

下面是我的试用代码:

byte[] randomSalt = session.GenerateRandom(20);

objectAttributes.Add(new ObjectAttribute(CKA.CKA_CLASS, CKZ.CKZ_SALT_SPECIFIED));    
objectAttributes.Add(new ObjectAttribute(CKA.CKA_VALUE, randomSalt));    
objectAttributes.Add(new ObjectAttribute(CKA.CKA_CLASS, 1000));
objectAttributes.Add(new ObjectAttribute(CKA.CKA_CLASS, 0x00000004));    
objectAttributes.Add(new ObjectAttribute(CKA.CKA_VALUE, new byte[] { }));    
objectAttributes.Add(new ObjectAttribute(CKA.CKA_VALUE, Encoding.UTF8.GetBytes("password")));

Mechanism mechanism = new Mechanism(CKM.CKM_PKCS5_PBKD2); objectHandle objectHandle = session.GenerateKey(mechanism, objectAttributes);

有了这个,我得到了 CKR_MECHANISM_INVALID 异常

第一个问题是您试图通过 ObjectAttribute-s 列表而不是 CkPkcs5Pbkd2Params class 实例向 [=​​11=] 机制提供参数。有关详细信息,请查看 PKCS#11 v2.20 specification.

的第 12.26.9 章

第二个问题是您的非托管 PKCS#11 库很可能根本不支持 CKM_PKCS5_PBKD2 机制,因为通过返回 CKR_MECHANISM_INVALID 错误您的非托管 PKCS#11 库告诉您 "An invalid mechanism was specified to the cryptographic operation"。您可以使用GetMechanismInfo()方法检查是否支持该机制:

if (!slot.GetMechanismList().Contains(CKM.CKM_PKCS5_PBKD2))
    throw new Exception("Unmanaged PKCS#11 library does not support CKM_PKCS5_PBKD2 mechanism");