Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

RSA Encrypt and OpenSSL Decrypt

See more OpenSSL Examples

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

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oRsa
LOCAL oPrivKey
LOCAL oPubKey
LOCAL cPlainText
LOCAL nBUsePrivateKey
LOCAL cEncryptedStr
LOCAL oBd

nSuccess := 0

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

oRsa := CreateObject("Chilkat.Rsa")

oPrivKey := CreateObject("Chilkat.PrivateKey")
nSuccess := oRsa:GenKey(2048, oPrivKey)
nSuccess := oPrivKey:SavePkcs8PemFile("qa_output/privKey.pem")

oPubKey := CreateObject("Chilkat.PublicKey")
oPrivKey:ToPublicKey(oPubKey)

oRsa:EncodingMode := "base64"
cPlainText := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890"
nBUsePrivateKey := 0
oRsa:UsePublicKey(oPubKey)
cEncryptedStr := oRsa:EncryptStringENC(cPlainText, nBUsePrivateKey)

oBd := CreateObject("Chilkat.BinData")
oBd:AppendEncoded(cEncryptedStr, "base64")
nSuccess := oBd:WriteFile("qa_output/enc.dat")

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

? "OK"

oRsa:destroy()
oPrivKey:destroy()
oPubKey:destroy()
oBd:destroy()