1
我遇到了一个问题,它在C++中加密我的字符串,然后在PHP中解密。在C++方面,我认为一切都很顺利。以下是我的C++代码。发出C++中的加密字符串并在PHP中解密
unsigned char inbuffer[1024];
unsigned char outbuffer[1024];
unsigned char oneKey[] = "abc";
AES_KEY key;
AES_set_encrypt_key(oneKey, 128, &key);
string straa("hello world\n");
memcpy((char*)inbuffer, straa.c_str(), 13);
AES_encrypt(inbuffer, encryptedbuffer, &key);
LPCSTR pszSource = (LPCSTR)encryptedbuffer;
DWORD nDestinationSize;
if (CryptBinaryToString(reinterpret_cast<const BYTE*> (pszSource), strlen(pszSource), CRYPT_STRING_BASE64, nullptr, &nDestinationSize))
{
LPTSTR pszDestination = static_cast<LPTSTR> (HeapAlloc(GetProcessHeap(), HEAP_NO_SERIALIZE, nDestinationSize * sizeof(TCHAR)));
if (pszDestination)
{
if (CryptBinaryToString(reinterpret_cast<const BYTE*> (pszSource), strlen(pszSource), CRYPT_STRING_BASE64, pszDestination, &nDestinationSize))
{
printf("OUT: %s", pszDestination); // Base64 encoded output of encrypted string
}
}
}
这是我的PHP代码:
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('AES-256-CBC'));
$out = openssl_decrypt(base64_decode("G6g0f/K7EzAw8fxn9BTFzw=="), 'AES-256-CBC', "abc", OPENSSL_RAW_DATA, $iv);
echo $out;
没有从PHP代码输出。
的C++代码将输出以下内容:
OUT: G6g0f/K7EzAw8fxn9BTFzw==
我在这里没有帮助,因为我没有C++经验,但我很感兴趣(如果你有时间解释),你在使用C++和PHP的组合吗? –
当然我有时间。我正在创建一个登录客户端,它将客户端传输到AES中的服务器数据,以防止人们查看网络流量。 –
使用HTTPS保护客户端到服务器的通信。 – zaph