(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 <CkCert.h>
#include <CkPublicKey.h>
#include <CkRsa.h>
void ChilkatSample(void)
{
CkCert cert;
bool success = cert.LoadFromFile("qa_data/pem/mf_public_rsa.pem");
if (success == false) {
std::cout << cert.lastErrorText() << "\r\n";
return;
}
CkPublicKey *pubKey = cert.ExportPublicKey();
if (cert.get_LastMethodSuccess() != true) {
std::cout << cert.lastErrorText() << "\r\n";
return;
}
CkRsa rsa;
success = rsa.ImportPublicKeyObj(*pubKey);
if (success == false) {
std::cout << rsa.lastErrorText() << "\r\n";
return;
}
delete pubKey;
rsa.put_EncodingMode("base64");
const char *encryptedStr = rsa.encryptStringENC("hello",false);
std::cout << "encrypted string = " << encryptedStr << "\r\n";
}
|