如何在使用HSM时生成密钥对

时间:2017-07-10 15:54:36

标签: pkcs#11 hsm

我正在使用HSM生成密钥对,当我运行代码时它给了我一个错误"错误n.5"。因为我正在使用Ncryptoki,所以我在线搜索答案,它说"它是由底层PKCS#11模块引发的,通常意味着令牌处于某种无效状态,例如未格式化或已完成或其他"我成功登录并且令牌已存在。

我不知道如何格式化或完成令牌或objectAttribute。

代码如下:

Console.WriteLine("Logged in:" + session.IsLoggedIn.ToString())

    ' Searchs for an RSA private key object
    ' Sets the template with its attributes
    template = New CryptokiCollection()
    template.Add(New ObjectAttribute(ObjectAttribute.CKA_CLASS, CryptokiObject.CKO_PRIVATE_KEY))
    template.Add(New ObjectAttribute(ObjectAttribute.CKA_KEY_TYPE, Key.CKK_RSA))
    template.Add(New ObjectAttribute(ObjectAttribute.CKA_LABEL, "new Key 0"))

    ' Launchs the search specifying the template just created
    objects = session.Objects.Find(template, 10)

    For Each obj In objects
        Console.WriteLine(CType(obj, PrivateKey).Label)
    Next

    For i = 0 To objects.Count - 1
        Console.WriteLine(CType(objects(i), PrivateKey).Label)
    Next

    Dim privateKey As RSAPrivateKey
    Dim publicKey As RSAPublicKey

    ' If the private keys is found computes the digital signature 
    If (objects.Count = 0) Then
        Dim templatePub As CryptokiCollection
        Dim templatePri As CryptokiCollection

        templatePub = New CryptokiCollection()
        templatePub.Add(New ObjectAttribute(ObjectAttribute.CKA_CLASS, CryptokiObject.CKO_PUBLIC_KEY))
        templatePub.Add(New ObjectAttribute(ObjectAttribute.CKA_TOKEN, True))
        templatePub.Add(New ObjectAttribute(ObjectAttribute.CKA_PRIVATE, False))
        templatePub.Add(New ObjectAttribute(ObjectAttribute.CKA_LABEL, "test"))
        templatePub.Add(New ObjectAttribute(ObjectAttribute.CKA_ID, "1"))
        templatePub.Add(New ObjectAttribute(ObjectAttribute.CKA_MODULUS_BITS, 1024))
        templatePub.Add(New ObjectAttribute(ObjectAttribute.CKA_PUBLIC_EXPONENT, &H10001))

        templatePub.Add(New ObjectAttribute(ObjectAttribute.CKA_LOCAL, True))
        templatePub.Add(New ObjectAttribute(ObjectAttribute.CKA_WRAP, True))
        templatePub.Add(New ObjectAttribute(ObjectAttribute.CKA_MODULUS, True))

        templatePri = New CryptokiCollection()
        templatePri.Add(New ObjectAttribute(ObjectAttribute.CKA_CLASS, CryptokiObject.CKO_PRIVATE_KEY))
        templatePri.Add(New ObjectAttribute(ObjectAttribute.CKA_TOKEN, True))
        templatePri.Add(New ObjectAttribute(ObjectAttribute.CKA_PRIVATE, True))
        templatePri.Add(New ObjectAttribute(ObjectAttribute.CKA_LABEL, "test"))
        templatePri.Add(New ObjectAttribute(ObjectAttribute.CKA_ID, "1"))




        'gets the first object
        Dim keys As Key()


        Console.WriteLine(templatePub)
        Console.WriteLine(templatePri)
        Console.WriteLine(token)
        Console.WriteLine(Mechanism.RSA_PKCS_KEY_PAIR_GEN)

        keys = session.GenerateKeyPair(Mechanism.RSA_PKCS_KEY_PAIR_GEN, templatePub, templatePri)
        privateKey = CType(keys(1), RSAPrivateKey)
        publicKey = CType(keys(0), RSAPublicKey)
    Else

        privateKey = CType(objects(objects.Count - 1), RSAPrivateKey)
        Console.WriteLine(privateKey.Label)

        template = New CryptokiCollection()
        template.Add(New ObjectAttribute(ObjectAttribute.CKA_CLASS, CryptokiObject.CKO_PUBLIC_KEY))
        template.Add(New ObjectAttribute(ObjectAttribute.CKA_LABEL, "Ugo's new Key 0"))
        template.Add(New ObjectAttribute(ObjectAttribute.CKA_ID, "1"))

        ' Launchs the search specifying the template just created  
        objects = session.Objects.Find(template, 1)

        publicKey = CType(objects(0), RSAPublicKey)
        Dim text As Byte()
        Dim signature As Byte()

        text = Encoding.ASCII.GetBytes("Hello World")

发生错误并停止此行:          " keys = session.GenerateKeyPair(Mechanism.RSA_PKCS_KEY_PAIR_GEN,templatePub,templatePri)" 感谢并感谢任何帮助和评论。

0 个答案:

没有答案