Sample code for 30+ languages & platforms
DataFlex

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Variant vCert
Store    Handle hoCertStore
    Variant vJsonCN
    Handle hoJsonCN
    Variant vCert
    Handle hoCert
    Variant vPrivKey
    Handle hoPrivKey
    Handle hoRsa
    String sStrData
    String sHexSig
    String sTemp1

    Move False To iSuccess

    // 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.)
    Get Create (RefClass(cComChilkatCertStore)) To hoCertStore
    If (Not(IsComObjectCreated(hoCertStore))) Begin
        Send CreateComObject of hoCertStore
    End

    // The 1st argument is the filename, the 2nd arg is the 
    // PFX file's password:
    Get ComLoadPfxFile Of hoCertStore "chilkat.pfx" "test" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCertStore To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Find the certificate by the subject common name:
    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonCN
    If (Not(IsComObjectCreated(hoJsonCN))) Begin
        Send CreateComObject of hoJsonCN
    End
    Get ComUpdateString Of hoJsonCN "CN" "cert common name" To iSuccess

    Get Create (RefClass(cComChilkatCert)) To hoCert
    If (Not(IsComObjectCreated(hoCert))) Begin
        Send CreateComObject of hoCert
    End
    Get pvComObject of hoJsonCN to vJsonCN
    Get pvComObject of hoCert to vCert
    Get ComFindCert Of hoCertStore vJsonCN vCert To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCertStore To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatPrivateKey)) To hoPrivKey
    If (Not(IsComObjectCreated(hoPrivKey))) Begin
        Send CreateComObject of hoPrivKey
    End
    Get pvComObject of hoPrivKey to vPrivKey
    Get ComGetPrivateKey Of hoCert vPrivKey To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCert To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatRsa)) To hoRsa
    If (Not(IsComObjectCreated(hoRsa))) Begin
        Send CreateComObject of hoRsa
    End

    Get pvComObject of hoPrivKey to vPrivKey
    Get ComUsePrivateKey Of hoRsa vPrivKey To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoRsa To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Encode the signature as a hex string
    Set ComEncodingMode Of hoRsa To "hex"

    Move "This is the string to be signed." To sStrData

    // Sign the string using the sha-1 hash algorithm.
    // Other valid choices are "sha-256", "md2" and "md5".
    Get ComSignStringENC Of hoRsa sStrData "sha-1" To sHexSig

    Showln sHexSig

    Showln "Success!"


End_Procedure