我可以解密通过PHP加密的C ++数据吗?

时间:2012-09-06 03:54:02

标签: php c++ encryption-symmetric

我正在使用mcrypt_encrypt和base64_encode来加密php中的数据。我试过在C ++中解密数据,但无济于事。我有多年使用的C ++ Rijndael逻辑,以及base64_decode逻辑。后者完美地解码由php的base64_encode编码的字符串。我正在使用CBC和PHP和C ++。我已经尝试了不同的块大小等等但无济于事。任何建议都非常感谢。

这是我的测试逻辑:

PHP

$key = "qwertyuiopasdfghjklzxcvbnmqwerty";  
$iv = "12345678901234561234567890123456";  
$text = "this is the text to encrypt";
$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, $iv);
echo base64_encode($crypttext)."<br/>";

C ++

char* base64encode = ".. output from php...";  
unsigned char binaryData[256];  
int binaryNumBytes;  
char result[256];  
base64_decode(reinterpret_cast<unsigned char*>(base64encode), strlen(base64encode),       binaryData, &binaryNumBytes, false);  
Encryption::Rijndael rijndael;  
char* key = "qwertyuiopasdfghjklzxcvbnmqwerty";  
char* iv = "12345678901234561234567890123456";  
rijndael.Init(Encryption::Rijndael::CBC, reinterpret_cast<const char*>(key), 32, 32,    reinterpret_cast<const char*>(iv));  
rijndael.Decrypt(reinterpret_cast<const char*>(binaryData), reinterpret_cast<char*>(result), 32);  
cout << result << endl; 

编辑:如果我使用ECB模式,我可以使用它。在CBC之间存在一些问题。

1 个答案:

答案 0 :(得分:0)

使用ECB模式是可行的方法。

相关问题