I'm currently struggeling with the public Key Encryption in C++. I am using openssl but the generated string is always broken. My current solution is the following:
string encryptWithPublicKey(const char* pData, int iLen) { // pData: String to encrypt, iLen: length
char chPublicKey[] = "---Public Key-- ...";
BIO* bio = BIO_new_mem_buf(chPublicKey, -1);
RSA* rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL);
int nLen = RSA_size(rsa);
char* pEncode = (char*)malloc(nLen + 1);
int rc = RSA_public_encrypt(iLen, (unsigned char*)pData, (unsigned char*)pEncode, rsa, RSA_NO_PADDING);
cout << "From: " << pData << " To: '" << pEncode << "'";
RSA_free(rsa);
CRYPTO_cleanup_all_ex_data();
std::string strName = std::string(pEncode);
return strName;
}
The output of the encryption is always in that format: "═════════════════════════════════════════════════════════════════²²²²☻"
No idea why the output has that format ^^
Maybe someone has an idea how to use the encryption in a better.
Thanks Kurt