How do I encrypt data in PHP using openssl and decrypt in C++?
EDIT: I got it to work somewhat, but now I have an issue that it outputs more characters in the console for some reason.
php code
openssl_encrypt("testtest", "AES-256-CBC", "01234567890123456789012345678901", 0, "0123456789012345");
c++ code
int main() {
/* A 256 bit key */
unsigned char* key = (unsigned char*)"01234567890123456789012345678901";
/* A 128 bit IV */
unsigned char* iv = (unsigned char*)"0123456789012345";
std::vector<byte> data = base64_decode("2fd9RV8QVVUAR2n5WLjiSA==");
byte* ciphertext = new byte[data.size()];
for (size_t i = 0; i < data.size(); i++)
{
ciphertext[i] = data.at(i);
}
byte decryptedtext[128];
int decryptedtext_len;
decryptedtext_len = decrypt(ciphertext, data.size(), key, iv, decryptedtext);
std::cout << decryptedtext;
return 0;
}
The decrypt function is from the OpenSSL wiki (link).
Base64_decode is from here
I get testtestŘŹ
as output in the console instead of just testtest
.