Sample code for 30+ languages & platforms
DataFlex

RSA Sign Using Private Key from .pfx/.p12 to Base64 Signature

See more RSA Examples

Demonstrates how to RSA sign something using a private key loaded from a .pfx/.p12. The RSA signature is returned in Base64 encoded format.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoRsa
    Handle hoPfx
    Variant vPrivKey
    Handle hoPrivKey
    String sStrData
    String sBase64Sig
    String sTemp1

    Move False To iSuccess

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

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

    // Load the .pfx/.p12
    Get Create (RefClass(cComChilkatPfx)) To hoPfx
    If (Not(IsComObjectCreated(hoPfx))) Begin
        Send CreateComObject of hoPfx
    End
    Get ComLoadPfxFile Of hoPfx "qa_data/pfx/myKey.p12" "myPassword" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPfx To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Get the default private key.

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

    // Import the private key into the RSA component:
    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

    // Get the signature in base64
    Set ComEncodingMode Of hoRsa To "base64"

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

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

    Showln sBase64Sig

    Showln "Success!"


End_Procedure