Understood. Suddenly someone come in handy:
CPP:
#include <openssl/conf.h> #include <openssl/evp.h> #include <string> #include <iostream> #include "stringf.h" // собственная обёртка для операций с файлами под Windows std::string openssl_encrypt(std::string data, const EVP_CIPHER * mode, unsigned char * key, unsigned char * iv){ int buf_length, out_length; unsigned char *cipher_text = (unsigned char*)malloc(data.length()); EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); if(!ctx){return "ctx is null";} if(!EVP_EncryptInit_ex(ctx, mode, NULL, key, iv)){return "EVP_EncryptInit_ex error";} if(!EVP_EncryptUpdate(ctx, cipher_text, &buf_length, (unsigned char*)data.c_str(), data.length())){return "EVP_EncryptUpdate error";} out_length = buf_length; if(!EVP_EncryptFinal_ex(ctx, cipher_text + buf_length, &buf_length)){return "EVP_EncryptFinal_ex error";} out_length += buf_length; EVP_CIPHER_CTX_free(ctx); std::string out(reinterpret_cast<char*>(cipher_text), out_length); free(cipher_text); return out; } std::string openssl_decrypt(std::string cipher, const EVP_CIPHER * mode, unsigned char * key, unsigned char * iv){ int buf_length, out_length; unsigned char *data_buf = (unsigned char*)malloc(cipher.length()); EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); if(!ctx){return "ctx is null";} if(!EVP_DecryptInit_ex(ctx, mode, NULL, key, iv)){return "EVP_DecryptInit_ex error";} if(!EVP_DecryptUpdate(ctx, data_buf, &buf_length, (unsigned char*)cipher.c_str(), cipher.length())){return "EVP_DecryptUpdate error";} out_length = buf_length; if(!EVP_DecryptFinal_ex(ctx, data_buf + buf_length, &buf_length)){return "EVP_DecryptFinal_ex error";} out_length += buf_length; EVP_CIPHER_CTX_free(ctx); std::string out(reinterpret_cast<char*>(data_buf), out_length); free(data_buf); return out; } int main (int argc, char* argv[]){ if (argc > 2){ unsigned char * key256 = (unsigned char *)argv[1]; // обязательно 32 символа (256 бит) unsigned char * iv128 = (unsigned char *)argv[2]; // обязательно 16 символов (128 бит) std::string data = "Hello World!"; std::string cpp_cipher, php_cipher; stringf::WriteBufferToFile("D:\\aes.cpp", openssl_encrypt(data, EVP_aes_256_ctr(), key256, iv128)); stringf::ReadFileToBuffer("D:\\aes.cpp", cpp_cipher); stringf::ReadFileToBuffer("D:\\aes.php", php_cipher); std::cout << "CPP cipher:\t" << openssl_decrypt(cpp_cipher, EVP_aes_256_ctr(), key256, iv128) << std::endl; std::cout << "PHP cipher:\t" << openssl_decrypt(php_cipher, EVP_aes_256_ctr(), key256, iv128) << std::endl; } else{std::cout << "Not enough arguments!\n";} return 0; }
BAT:
test.exe 11112222333344445555666677778888 1111333355557777 pause
PHP (for two-way validation):
<?php $key256 = '11112222333344445555666677778888'; $iv128 = '1111333355557777'; $data = "Hello World!"; file_put_contents('D:\\aes.php', openssl_encrypt($data,'AES-256-CTR',$key256,OPENSSL_RAW_DATA,$iv128)); $cpp = file_get_contents('D:\\aes.cpp'); echo 'CPP cipher decrypted: '; var_dump(openssl_decrypt($cpp,'AES-256-CTR',$key256,OPENSSL_RAW_DATA,$iv128));