我正在尝试开发一个 C++ 程序来加密一些数据,以便稍后使用基于 PHP 的 Web 服务进行解密。但是,我在使用 PHP 中的 openssl_decrypt() 方法使用 Blow-fish CBC 方法使用 PHP 解密数据时遇到问题。
我正在测试此代码 C++ 代码以加密消息并转换为 base64 格式以使用我的 PHP 程序解密。这是 C++ 程序的代码。
#include <secureinfra.hpp>
#include <iostream>
int main()
{
cout << "Unit Test for the crypto system" << endl;
SecureInfrastructure mySec;
/* A 256 bit key and IV */
auto *key = (unsigned char *)"ZdOAq0yn6H1i35TywbLrtqBO4NCjh7Vh";
auto *iv = (unsigned char *)"01234567";
/* Message to be encrypted */
auto *plaintext = (unsigned char *)"The quick brown fox jumps over the lazy dog";
/* Buffer for ciphertext. Ensure the buffer is long enough for the ciphertext which may be longer than the
* plaintext, depending on the algorithm and mode. */
unsigned char ciphertext[128];
/* Buffer for the decrypted text */
unsigned char decryptedtext[128];
int decryptedtext_len, ciphertext_len;
/* Encrypt the plaintext */
ciphertext_len = mySec.EncryptMessage(plaintext, strlen((char *) plaintext), key, iv, ciphertext);
/* Decrypt the ciphertext */
decryptedtext_len = mySec.DecryptMessage(ciphertext, ciphertext_len, key, iv, decryptedtext);
/* Add a NULL terminator. We are expecting printable text */
decryptedtext[decryptedtext_len] = '\0';
return 0;
}
但是,PHP 程序无法解密由我的 C++ 程序生成的 $ciphertext。当我尝试回显 $original_plaintext 时,它什么也没显示。
另外,我注意到使用 SSL 的 PHP 和 C++ 加密是不同的。例如,当我尝试使用 PHP(使用 openssl_encrypt 方法)和 C++ 加密短语“The quick brown fox jumps over the lazy dog”时,使用相同的密钥和 IV,加密的 base64 字符串如下:
在 C++ 中:
ljRvLcfBiDwxRSm1lDXMwDk5S4pRCg9D2F9jxR5C5yy79xoBHQNKjA/FaQ2dNfKvyH3brAV/
在 PHP 中:
hWj7F1fnBXhIWv4sonjhSmJgHWJALFecxxrGe0T1kLrN4TfSMUw/uELj6h5+Laph
我应该如何解决这个问题?我看到一篇类似的帖子描述了我需要在 C++ 程序上设置密钥大小,但我不确定如何正确执行此操作。
慕姐8265434
慕标5832272
侃侃无极
随时随地看视频慕课网APP
相关分类