Sample code for 30+ languages & platforms
DataFlex

RSA Encrypt and OpenSSL Decrypt

See more OpenSSL Examples

Demonstrates how to use Chilkat to RSA encrypt, and then use OpenSSL to decrypt.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoRsa
    Variant vPrivKey
    Handle hoPrivKey
    Variant vPubKey
    Handle hoPubKey
    String sPlainText
    Boolean iBUsePrivateKey
    String sEncryptedStr
    Handle hoBd

    Move False To iSuccess

    // This requires 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

    Get Create (RefClass(cComChilkatPrivateKey)) To hoPrivKey
    If (Not(IsComObjectCreated(hoPrivKey))) Begin
        Send CreateComObject of hoPrivKey
    End
    Get pvComObject of hoPrivKey to vPrivKey
    Get ComGenKey Of hoRsa 2048 vPrivKey To iSuccess
    Get ComSavePkcs8PemFile Of hoPrivKey "qa_output/privKey.pem" To iSuccess

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

    Set ComEncodingMode Of hoRsa To "base64"
    Move "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890" To sPlainText
    Move False To iBUsePrivateKey
    Get pvComObject of hoPubKey to vPubKey
    Get ComUsePublicKey Of hoRsa vPubKey To iSuccess
    Get ComEncryptStringENC Of hoRsa sPlainText iBUsePrivateKey To sEncryptedStr

    Get Create (RefClass(cComChilkatBinData)) To hoBd
    If (Not(IsComObjectCreated(hoBd))) Begin
        Send CreateComObject of hoBd
    End
    Get ComAppendEncoded Of hoBd sEncryptedStr "base64" To iSuccess
    Get ComWriteFile Of hoBd "qa_output/enc.dat" To iSuccess

    // The OpenSSL command to decrypt is:
    // openssl pkeyutl -in enc.dat -inkey privKey.pem -keyform PEM -pkeyopt rsa_padding_mode:pkcs1 -decrypt

    Showln "OK"


End_Procedure