Sample code for 30+ languages & platforms
DataFlex

RSA Sign with PKCS8 Encrypted Key

See more RSA Examples

Demonstrates how to load a private key from an encrypted PKCS8 file and create an RSA digital signature (and then verify it).

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Variant vPrivKey
    Handle hoPrivKey
    Handle hoRsa
    String sStrData
    String sHexSig
    Handle hoCert
    Variant vPubKey
    Handle hoPubKey
2    Handle hoRsa2
    String sTemp1
    Boolean bTemp1

    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(cComChilkatPrivateKey)) To hoPrivKey
    If (Not(IsComObjectCreated(hoPrivKey))) Begin
        Send CreateComObject of hoPrivKey
    End

    // Load the private key from an RSA PEM file:
    Get ComLoadAnyFormatFile Of hoPrivKey "raul_privateKey.key" "a0123456789" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPrivKey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatRsa)) To hoRsa
    If (Not(IsComObjectCreated(hoRsa))) Begin
        Send CreateComObject of hoRsa
    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

    // This example will sign a string, and receive the signature
    // in a hex-encoded string.  Therefore, set the encoding mode
    // to "hex":
    Set ComEncodingMode Of hoRsa To "hex"

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

    // Sign the string using the sha256 hash algorithm.
    // Other valid choices are sha1, sha384, sha512 and others.
    Get ComSignStringENC Of hoRsa sStrData "sha256" To sHexSig
    Get ComLastMethodSuccess Of hoRsa To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoRsa To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln sHexSig

    // Now verify with the public key.
    // This example shows how to use the public key from 
    // a digital certificate (.cer file)
    Get Create (RefClass(cComChilkatCert)) To hoCert
    If (Not(IsComObjectCreated(hoCert))) Begin
        Send CreateComObject of hoCert
    End
    Get ComLoadFromFile Of hoCert "raul_publicKey.cer" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCert To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatPublicKey)) To hoPubKey
    If (Not(IsComObjectCreated(hoPubKey))) Begin
        Send CreateComObject of hoPubKey
    End
    Get pvComObject of hoPubKey to vPubKey
    Get ComGetPublicKey Of hoCert vPubKey To iSuccess

    Get Create (RefClass(cComChilkatRsa)) To hoRsa2
    If (Not(IsComObjectCreated(hoRsa2))) Begin
        Send CreateComObject of hoRsa2
    End
    Get pvComObject of hoPubKey to vPubKey
    Get ComUsePublicKey Of hoRsa2 vPubKey To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoRsa2 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Verify the signature against the original data:
    Set ComEncodingMode Of hoRsa2 To "hex"
    Get ComVerifyStringENC Of hoRsa2 sStrData "sha256" sHexSig To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoRsa2 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Signature verified!"

    // Verify with incorrect data:
    Get ComVerifyStringENC Of hoRsa2 "something else" "sha256" sHexSig To iSuccess
    If (iSuccess <> True) Begin
        Showln "Signature not verified! (which was expected in this case)"
    End
    Else Begin
        Showln "Hmmm... that's not right..."
    End



End_Procedure