Sample code for 30+ languages & platforms
PureBasic

RSA Signature with Certificate's Private Key from PFX

See more RSA Examples

Demonstrates how to use a certificate's private key from a PFX file to create an RSA signature.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkCertStore.pb"
IncludeFile "CkCert.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkRsa.pb"
IncludeFile "CkPrivateKey.pb"

Procedure ChilkatExample()

    success.i = 0

    ; This example requires the Chilkat API to have been previously unlocked.
    ; See Global Unlock Sample for sample code.

    ; Create an instance of a certificate store object, load a PFX file,
    ; locate the certificate we need, and use it for signing.
    ; (a PFX file may contain more than one certificate.)
    certStore.i = CkCertStore::ckCreate()
    If certStore.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; The 1st argument is the filename, the 2nd arg is the 
    ; PFX file's password:
    success = CkCertStore::ckLoadPfxFile(certStore,"chilkat.pfx","test")
    If success = 0
        Debug CkCertStore::ckLastErrorText(certStore)
        CkCertStore::ckDispose(certStore)
        ProcedureReturn
    EndIf

    ; Find the certificate by the subject common name:
    jsonCN.i = CkJsonObject::ckCreate()
    If jsonCN.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(jsonCN,"CN","cert common name")

    cert.i = CkCert::ckCreate()
    If cert.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkCertStore::ckFindCert(certStore,jsonCN,cert)
    If success = 0
        Debug CkCertStore::ckLastErrorText(certStore)
        CkCertStore::ckDispose(certStore)
        CkJsonObject::ckDispose(jsonCN)
        CkCert::ckDispose(cert)
        ProcedureReturn
    EndIf

    privKey.i = CkPrivateKey::ckCreate()
    If privKey.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkCert::ckGetPrivateKey(cert,privKey)
    If success = 0
        Debug CkCert::ckLastErrorText(cert)
        CkCertStore::ckDispose(certStore)
        CkJsonObject::ckDispose(jsonCN)
        CkCert::ckDispose(cert)
        CkPrivateKey::ckDispose(privKey)
        ProcedureReturn
    EndIf

    rsa.i = CkRsa::ckCreate()
    If rsa.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkRsa::ckUsePrivateKey(rsa,privKey)
    If success = 0
        Debug CkRsa::ckLastErrorText(rsa)
        CkCertStore::ckDispose(certStore)
        CkJsonObject::ckDispose(jsonCN)
        CkCert::ckDispose(cert)
        CkPrivateKey::ckDispose(privKey)
        CkRsa::ckDispose(rsa)
        ProcedureReturn
    EndIf

    ; Encode the signature as a hex string
    CkRsa::setCkEncodingMode(rsa, "hex")

    strData.s = "This is the string to be signed."

    ; Sign the string using the sha-1 hash algorithm.
    ; Other valid choices are "sha-256", "md2" and "md5".
    hexSig.s = CkRsa::ckSignStringENC(rsa,strData,"sha-1")

    Debug hexSig

    Debug "Success!"


    CkCertStore::ckDispose(certStore)
    CkJsonObject::ckDispose(jsonCN)
    CkCert::ckDispose(cert)
    CkPrivateKey::ckDispose(privKey)
    CkRsa::ckDispose(rsa)


    ProcedureReturn
EndProcedure