(Unicode C++) RSA Import Public Key from Certificate PEM
Uses a certificate's public key for RSA encryption. The public key from the certificate .pem file is used.
#include <CkCertW.h>
#include <CkPublicKeyW.h>
#include <CkRsaW.h>
void ChilkatSample(void)
{
CkCertW cert;
bool success = cert.LoadFromFile(L"qa_data/pem/mf_public_rsa.pem");
if (success == false) {
wprintf(L"%s\n",cert.lastErrorText());
return;
}
CkPublicKeyW *pubKey = cert.ExportPublicKey();
if (cert.get_LastMethodSuccess() != true) {
wprintf(L"%s\n",cert.lastErrorText());
return;
}
CkRsaW rsa;
success = rsa.ImportPublicKeyObj(*pubKey);
if (success == false) {
wprintf(L"%s\n",rsa.lastErrorText());
return;
}
delete pubKey;
rsa.put_EncodingMode(L"base64");
const wchar_t *encryptedStr = rsa.encryptStringENC(L"hello",false);
wprintf(L"encrypted string = %s\n",encryptedStr);
}
|