Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Alladin eToken导入RSA密钥失败_C#_Rsa_Pkcs#11_E Token - Fatal编程技术网

C# Alladin eToken导入RSA密钥失败

C# Alladin eToken导入RSA密钥失败,c#,rsa,pkcs#11,e-token,C#,Rsa,Pkcs#11,E Token,我正在使用SafeNet的(Alladin)eToken与PKCS11接口连接到C#。 我需要将不使用eToken创建的RSA密钥导入eToken RSA密钥的创建通过以下方式完成: RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); RSAParameters publicKey = RSA.ExportParameters(false); RSAParameters privateKey = RSA.ExportPar

我正在使用SafeNet的(Alladin)eToken与PKCS11接口连接到C#。 我需要将不使用eToken创建的RSA密钥导入eToken

RSA密钥的创建通过以下方式完成:

RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSAParameters publicKey = RSA.ExportParameters(false);
RSAParameters privateKey = RSA.ExportParameters(true);
eTokenHelper.WritePrivateKeyToToken(session, privateKey, "private");
上述WritePrivateKeyToToToToToken的实现是:

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());
    }
(异常出现在最后一行:Create()

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

谢谢,
罗宁我有点问题。顺便说一下,您在代码中设置了两次PKCS11.CKA_LOCAL。这是不正确的。不要设置属性PKCS11.CKA_LOCAL-它是自动设置的。如果设置为PKCS11.SENSTIVE,则无法设置CKA_可提取、CKA_从不可提取和CKA_始终敏感

此代码应适用于:

   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());
List attList=新列表{};
Add(新的PKCS11.Attribute(PKCS11.CKA_类,PKCS11.CKO_私钥));
Add(新的PKCS11.Attribute(PKCS11.CKA_KEY_TYPE,PKCS11.CKK_RSA));
Add(新的PKCS11.Attribute(PKCS11.CKA_PRIVATE,true));
//Add(新的PKCS11.Attribute(PKCS11.CKA_SUBJECT,cert.SubjectName.RawData));
Add(新的PKCS11.Attribute(PKCS11.CKA_ID,0xa1));
Add(新的PKCS11.Attribute(PKCS11.CKA_LABEL,LABEL));
Add(新的PKCS11.Attribute(PKCS11.CKA_标记,true));
Add(新的PKCS11.Attribute(PKCS11.CKA_modules,publicParams.module));
Add(新的PKCS11.Attribute(PKCS11.CKA_PUBLIC_index,publicParams.index));
Add(新的PKCS11.Attribute(PKCS11.CKA_PRIVATE_index,publicParams.D));
//Add(新的ObjectAttribute(PKCS11.CKH_CLOCK,true));
attList.Add(新的PKCS11.Attribute(PKCS11.CKA_MODIFIABLE,true));
Add(新的PKCS11.Attribute(PKCS11.CKA_-SENSITIVE,true));
Add(新的PKCS11.Attribute(PKCS11.CKA_派生,false));
Add(新的PKCS11.Attribute(PKCS11.CKA_DECRYPT,true));
Add(新的PKCS11.Attribute(PKCS11.CKA_符号,true));
Add(新的PKCS11.Attribute(PKCS11.CKA_SIGN_RECOVER,false));
Add(新的PKCS11.Attribute(PKCS11.CKA_UNWRAP,false));
创建(会话,attList.ToArray());

Jiri,谢谢你的回答。。。从经过的时间的长距离来看,你能找出为什么我得到
public const int CKR\u TEMPLATE\u complete=0x000000D0使用您的代码?任何帮助都将不胜感激!
   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());