gpt4 book ai didi

c# - 导入 RSA key 时 Aladdin eToken 失败

转载 作者:行者123 更新时间:2023-11-30 16:15:31 25 4
gpt4 key购买 nike

我正在使用带有 PKCS11 接口(interface)的 SafeNet (Alladin) eToken 到 C#。我需要将在没有 eToken 的情况下创建的 RSA key 导入 eToken。

RSA key 的创建是通过以下方式完成的:

RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSAParameters publicKey = RSA.ExportParameters(false);
RSAParameters privateKey = RSA.ExportParameters(true);
eTokenHelper.WritePrivateKeyToToken(session, privateKey, "private");

上面WritePrivateKeyToToken的实现是:

public static void WritePrivateKeyToToken(PKCS11.Session session, System.Security.Cryptography.RSAParameters publicParams, string label)
{

List<PKCS11.Attribute> attList = new List<PKCS11.Attribute>{};


attList.Add(new PKCS11.Attribute(PKCS11.CKA_CLASS, PKCS11.CKO_PRIVATE_KEY));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_KEY_TYPE, PKCS11.CKK_RSA));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_PRIVATE, true));
//attList.Add(new PKCS11.Attribute(PKCS11.CKA_SUBJECT, cert.SubjectName.RawData));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_ID, 0xa1));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_LABEL, label));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_TOKEN, true));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_MODULUS, publicParams.Modulus));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_PUBLIC_EXPONENT, publicParams.Exponent));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_PRIVATE_EXPONENT, publicParams.D));
// attList.Add(new ObjectAttribute(PKCS11.CKH_CLOCK, true));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_MODIFIABLE, true));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_LOCAL, true));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_EXTRACTABLE, false));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_NEVER_EXTRACTABLE, true));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_SENSITIVE, true));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_ALWAYS_SENSITIVE, true));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_DERIVE, false));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_LOCAL, false));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_DECRYPT, true));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_SIGN, true));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_SIGN_RECOVER, false));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_UNWRAP, false));

PKCS11.Object.Create(session, attList.ToArray());
}

当我运行这段代码时,我得到了代码异常

public const int CKR_TEMPLATE_INCONSISTENT = 0x000000D1;

(异常出现在最后一行:Create())。

如果能帮助我理解我做错了什么,我将不胜感激。

谢谢,罗嫩

最佳答案

我有一些问题。顺便说一下,您在代码中两次设置了属性 PKCS11.CKA_LOCAL。这是不正确的。不要设置属性 PKCS11.CKA_LOCAL - 它是自动设置的。如果设置了 PKCS11.SENSITIVE,则无法设置 CKA_EXTRACTABLE、CKA_NEVER_EXTRACTABLE 和 CKA_ALWAYS_SENSITIVE。

这段代码应该可以工作:

   List<PKCS11.Attribute> attList = new List<PKCS11.Attribute>{};

attList.Add(new PKCS11.Attribute(PKCS11.CKA_CLASS, PKCS11.CKO_PRIVATE_KEY));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_KEY_TYPE, PKCS11.CKK_RSA));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_PRIVATE, true));
//attList.Add(new PKCS11.Attribute(PKCS11.CKA_SUBJECT, cert.SubjectName.RawData));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_ID, 0xa1));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_LABEL, label));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_TOKEN, true));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_MODULUS, publicParams.Modulus));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_PUBLIC_EXPONENT, publicParams.Exponent));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_PRIVATE_EXPONENT, publicParams.D));
// attList.Add(new ObjectAttribute(PKCS11.CKH_CLOCK, true));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_MODIFIABLE, true));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_SENSITIVE, true));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_DERIVE, false));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_DECRYPT, true));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_SIGN, true));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_SIGN_RECOVER, false));
attList.Add(new PKCS11.Attribute(PKCS11.CKA_UNWRAP, false));

PKCS11.Object.Create(session, attList.ToArray());

关于c# - 导入 RSA key 时 Aladdin eToken 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19537012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com