(Tcl) RSA Encrypt and OpenSSL Decrypt
Demonstrates how to use Chilkat to RSA encrypt, and then use OpenSSL to decrypt.
load ./chilkat.dll
# This requires the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.
set rsa [new_CkRsa]
set success [CkRsa_GenerateKey $rsa 2048]
# privKey is a CkPrivateKey
set privKey [CkRsa_ExportPrivateKeyObj $rsa]
set success [CkPrivateKey_SavePkcs8PemFile $privKey "qa_output/privKey.pem"]
delete_CkPrivateKey $privKey
CkRsa_put_EncodingMode $rsa "base64"
set plainText "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890"
set bUsePrivateKey 0
set encryptedStr [CkRsa_encryptStringENC $rsa $plainText $bUsePrivateKey]
set bd [new_CkBinData]
CkBinData_AppendEncoded $bd $encryptedStr "base64"
set success [CkBinData_WriteFile $bd "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
puts "OK"
delete_CkRsa $rsa
delete_CkBinData $bd
|