Click here to Skip to main content
15,867,845 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
iam try sign invoice by this code

Dim data As Byte() = Encoding.UTF8.GetBytes(serializedJson)
' Dim factories As Pkcs11InteropFactories = New Pkcs11InteropFactories()
Dim factories As New Pkcs11InteropFactories()


Dim DllLibPath As String = Application.StartupPath & "\eps2003csp11.dll"
Using pkcs11Library As IPkcs11Library = factories.Pkcs11LibraryFactory.LoadPkcs11Library(factories, DllLibPath, AppType.SingleThreaded)
    Dim slot As ISlot = pkcs11Library.GetSlotList(SlotsType.WithTokenPresent).FirstOrDefault()
    'opensc_pkcs11.dll
    '"F:\Work\VBNET project\EInvoice\EgyptInvoiceVB1\EgyptInvoiceVB\bin\Debug\eps2003csp11.dll"

    If slot Is Nothing Then
        Return "No slots found"
    End If

    Dim tokenInfo As ITokenInfo = slot.GetTokenInfo()
    Dim slotInfo As ISlotInfo = slot.GetSlotInfo()
    Using session = slot.OpenSession(SessionType.ReadWrite)
        session.Login(CKU.CKU_USER, Encoding.UTF8.GetBytes(TokenPin))
        Dim certificateSearchAttributes = New List(Of IObjectAttribute)() From {
    session.Factories.ObjectAttributeFactory.Create(CKA.CKA_CLASS, CKO.CKO_CERTIFICATE),
    session.Factories.ObjectAttributeFactory.Create(CKA.CKA_TOKEN, True),
    session.Factories.ObjectAttributeFactory.Create(CKA.CKA_CERTIFICATE_TYPE, CKC.CKC_X_509)
}
        Dim certificate As IObjectHandle = session.FindAllObjects(certificateSearchAttributes).FirstOrDefault()

        If certificate Is Nothing Then
            Return "Certificate not found"
        End If

        Dim store As X509Store = New X509Store(StoreName.My, StoreLocation.CurrentUser)
        store.Open(OpenFlags.MaxAllowed)
        Dim foundCerts = store.Certificates.Find(X509FindType.FindByIssuerName, TokenCertificate, False)
        'قرائة بالسريال
        Dim coundcartbySN = store.Certificates.Find(X509FindType.FindBySerialNumber, TokenCertificate, True)

        If foundCerts.Count = 0 Then Return "no device detected"
        Dim certForSigning = foundCerts(0)
        store.Close()
        Dim content As ContentInfo = New ContentInfo(New Oid("1.2.840.113549.1.7.5"), data)
        Dim cms As SignedCms = New SignedCms(content, True)
        Dim bouncyCertificate As EssCertIDv2 = New EssCertIDv2(New Org.BouncyCastle.Asn1.X509.AlgorithmIdentifier(New DerObjectIdentifier("1.2.840.113549.1.9.16.2.47")), Me.HashBytes(certForSigning.RawData))
        Dim signerCertificateV2 As SigningCertificateV2 = New SigningCertificateV2(New EssCertIDv2() {bouncyCertificate})
        Dim signer As CmsSigner = New CmsSigner(certForSigning)
        signer.DigestAlgorithm = New Oid("2.16.840.1.101.3.4.2.1")
        signer.SignedAttributes.Add(New Pkcs9SigningTime(DateTime.UtcNow))
        signer.SignedAttributes.Add(New AsnEncodedData(New Oid("1.2.840.113549.1.9.16.2.47"), signerCertificateV2.GetEncoded()))
        cms.ComputeSignature(signer)
        Dim output = cms.Encode()
        Return Convert.ToBase64String(output)
    End Using
End Using


bu i have erroe
{"Method C_GetTokenInfo returned CKR_GENERAL_ERROR"}

any one can help me

What I have tried:

 iam try sign invoice by this code 

<pre>
            Dim data As Byte() = Encoding.UTF8.GetBytes(serializedJson)
            ' Dim factories As Pkcs11InteropFactories = New Pkcs11InteropFactories()
            Dim factories As New Pkcs11InteropFactories()


            Dim DllLibPath As String = Application.StartupPath & "\eps2003csp11.dll"
            Using pkcs11Library As IPkcs11Library = factories.Pkcs11LibraryFactory.LoadPkcs11Library(factories, DllLibPath, AppType.SingleThreaded)
                Dim slot As ISlot = pkcs11Library.GetSlotList(SlotsType.WithTokenPresent).FirstOrDefault()
                'opensc_pkcs11.dll
                '"F:\Work\VBNET project\EInvoice\EgyptInvoiceVB1\EgyptInvoiceVB\bin\Debug\eps2003csp11.dll"

                If slot Is Nothing Then
                    Return "No slots found"
                End If

                Dim tokenInfo As ITokenInfo = slot.GetTokenInfo()
                Dim slotInfo As ISlotInfo = slot.GetSlotInfo()
                Using session = slot.OpenSession(SessionType.ReadWrite)
                    session.Login(CKU.CKU_USER, Encoding.UTF8.GetBytes(TokenPin))
                    Dim certificateSearchAttributes = New List(Of IObjectAttribute)() From {
                session.Factories.ObjectAttributeFactory.Create(CKA.CKA_CLASS, CKO.CKO_CERTIFICATE),
                session.Factories.ObjectAttributeFactory.Create(CKA.CKA_TOKEN, True),
                session.Factories.ObjectAttributeFactory.Create(CKA.CKA_CERTIFICATE_TYPE, CKC.CKC_X_509)
            }
                    Dim certificate As IObjectHandle = session.FindAllObjects(certificateSearchAttributes).FirstOrDefault()

                    If certificate Is Nothing Then
                        Return "Certificate not found"
                    End If

                    Dim store As X509Store = New X509Store(StoreName.My, StoreLocation.CurrentUser)
                    store.Open(OpenFlags.MaxAllowed)
                    Dim foundCerts = store.Certificates.Find(X509FindType.FindByIssuerName, TokenCertificate, False)
                    'قرائة بالسريال 
                    Dim coundcartbySN = store.Certificates.Find(X509FindType.FindBySerialNumber, TokenCertificate, True)

                    If foundCerts.Count = 0 Then Return "no device detected"
                    Dim certForSigning = foundCerts(0)
                    store.Close()
                    Dim content As ContentInfo = New ContentInfo(New Oid("1.2.840.113549.1.7.5"), data)
                    Dim cms As SignedCms = New SignedCms(content, True)
                    Dim bouncyCertificate As EssCertIDv2 = New EssCertIDv2(New Org.BouncyCastle.Asn1.X509.AlgorithmIdentifier(New DerObjectIdentifier("1.2.840.113549.1.9.16.2.47")), Me.HashBytes(certForSigning.RawData))
                    Dim signerCertificateV2 As SigningCertificateV2 = New SigningCertificateV2(New EssCertIDv2() {bouncyCertificate})
                    Dim signer As CmsSigner = New CmsSigner(certForSigning)
                    signer.DigestAlgorithm = New Oid("2.16.840.1.101.3.4.2.1")
                    signer.SignedAttributes.Add(New Pkcs9SigningTime(DateTime.UtcNow))
                    signer.SignedAttributes.Add(New AsnEncodedData(New Oid("1.2.840.113549.1.9.16.2.47"), signerCertificateV2.GetEncoded()))
                    cms.ComputeSignature(signer)
                    Dim output = cms.Encode()
                    Return Convert.ToBase64String(output)
                End Using
            End Using


bu i have erroe
{"Method C_GetTokenInfo returned CKR_GENERAL_ERROR"}

any one can help me
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900