2017-02-13 49 views
1

我想加密和解密C++庫和PHP服務器之間使用OPENSSL庫在它們中的通信。我想使用Blowfish CBC算法,但似乎C++代碼和PHP代碼的結果不同。 C++代碼就是從這裏採取:OPENSSL Blowfish CBC加密不同於PHP到C++

這是PHP代碼:

<?php 
function strtohex($x) 
{ 
    $s=''; 
    foreach (str_split($x) as $c) $s.=sprintf("%02X",ord($c)); 
    return($s); 
} 


$encryptedMessage = openssl_encrypt("input", "BF-CBC", "123456", 0, "initvect"); 


echo $encryptedMessage; 
echo "\n"; 
echo strtohex($encryptedMessage); 

的PHP輸出是這樣的:

x9jDa2WMwvQ= 
78396A446132574D7776513D 

這是C++代碼:

bool do_encrypt(const char *in, unsigned char *out, int *outlen, unsigned char *key, unsigned char *iv) 
{ 
    int buflen, tmplen; 

    EVP_CIPHER_CTX ctx; 
    EVP_CIPHER_CTX_init(&ctx); 
    EVP_EncryptInit_ex(&ctx, EVP_bf_cbc(), nullptr, key, iv); 

    if (!EVP_EncryptUpdate(&ctx, out, &buflen, (unsigned char*)in,  strlen(in))) 
    { 
     return false; 
    } 

    if (!EVP_EncryptFinal_ex(&ctx, out + buflen, &tmplen)) 
    { 
     return false; 
    } 

    buflen += tmplen; 
    *outlen = buflen; 
    EVP_CIPHER_CTX_cleanup(&ctx); 

    return true; 
} 

unsigned char output[2048] = { 0 }; 
int outLen; 

auto result = do_encrypt("input", output, &outLen, (unsigned char*)"123456", (unsigned char*)"initvect"); 

BIGNUM *outputStr = BN_new(); 
BN_bin2bn(output, outLen, outputStr); 

cout << base64_encode(output, outLen) << "\n"; 
cout << BN_bn2hex(outputStr) << "\n"; 

C++的輸出是這樣的:

EfRhhWqGmSQ= 
11F461856A869924 

有人可以幫助我瞭解我做錯了什麼嗎?任何幫助將非常感激。

謝謝!

編輯1: 我設法解決後JWW的答案C++代碼,效果不錯。我錯過了EVP_CIPHER_CTX_set_key_length但是,我無法讓PHP代碼返回相同的結果,最終我們決定轉向AES,現在它完美地工作。謝謝!

+0

你可能想對OpenSSL的維基快速看一下[EVP對稱加密和解密(http://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption)。事實上,您應該使用經過身份驗證的加密,因爲它提供了*機密性和真實性。請參閱OpenSSL wiki上的[EVP Authenticated Encryption and Decryption](http://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption)。 – jww

+0

感謝您的建議。我會研究它。但是現在,將PHP端點更改爲使用不同的加密並不容易。 – saw66

+0

河豚不再被認爲是安全的,即使它的發明者現在使用AES。 – zaph

回答

0

對於你的OpenSSL代碼,我相信你需要調用EVP_CIPHER_CTX_set_key_length來告訴庫的關鍵字只有6個字節。

讓我把Crypto ++扔到下面的舞臺上。 OpenSSL和Crypto ++將在您添加缺少的EVP_CIPHER_CTX_set_key_length OpenSSL調用後收斂在正確答案上。正確的答案是32CEBA7E046431EB(十六進制)。

我不知道這是怎麼回事用PHP:

x9jDa2WMwvQ= 
78396A446132574D7776513D 

考慮x是ASCII 0x78,9是ASCII 0x39,我猜你的十六進制編碼的Base64編碼串。


$ cat test.cxx 
#include "blowfish.h" 
#include "modes.h" 
#include "channels.h" 
#include "filters.h" 
#include "base64.h" 
#include "hex.h" 
using namespace CryptoPP; 

#include <iostream> 
#include <string> 
using namespace std; 

int main(int argc, char* argv[]) 
{ 
    const byte key[] = "123456"; // 6 
    const byte iv[] = "initvect"; // 8 
    CBC_Mode<Blowfish>::Encryption encryptor; 
    encryptor.SetKeyWithIV(key, 6, iv, 8); 

    string r1, r2; 
    ChannelSwitch chsw; 

    Base64Encoder e1(new StringSink(r1)); 
    HexEncoder e2(new StringSink(r2)); 
    chsw.AddDefaultRoute(e1); 
    chsw.AddDefaultRoute(e2); 

    string msg = "input"; 
    StringSource ss(msg, true, 
    new StreamTransformationFilter(encryptor, 
     new Redirector(chsw))); 

    cout << r1 << endl; 
    cout << r2 << endl; 

    return 0; 
} 

測試程序結果:

$ ./test.exe 
Ms66fgRkMes= 
32CEBA7E046431EB 

這裏的東西OpenSSL的部分。通知EVP_EncryptInit_ex被調用兩次。首先調用EVP_EncryptInit_ex來設置分組密碼EVP_bf_cbc。密鑰長度設置爲EVP_CIPHER_CTX_set_key_length。然後第二個,調用EVP_EncryptInit_ex來設置密鑰和iv。

#include <openssl/evp.h> 

#include <iostream> 
#include <iomanip> 
#include <stdexcept> 
using namespace std; 

typedef unsigned char byte; 

int main() 
{ 
    EVP_CIPHER_CTX ctx; 
    EVP_CIPHER_CTX_init(&ctx); 

    int rc; 
    const byte key[] = "123456"; // 6 
    const byte iv[] = "initvect"; // 8 

    rc = EVP_EncryptInit_ex(&ctx, EVP_bf_cbc(), NULL, 0, 0); 
    if (rc != 1) 
    throw runtime_error("EVP_EncryptInit_ex failed"); 

    rc = EVP_CIPHER_CTX_set_key_length(&ctx, 6); 
    if (rc != 1) 
    throw runtime_error("EVP_CIPHER_CTX_set_key_length failed"); 

    rc = EVP_EncryptInit_ex(&ctx, NULL, NULL, key, iv); 
    if (rc != 1) 
    throw runtime_error("EVP_EncryptInit_ex failed"); 

    const byte msg[] = "input"; 
    byte buf[32]; 
    int len1 = sizeof(buf), len2 = sizeof(buf); 

    rc = EVP_EncryptUpdate(&ctx, buf, &len1, msg, 5); 
    if (rc != 1) 
    throw runtime_error("EVP_EncryptUpdate failed"); 

    rc = EVP_EncryptFinal_ex(&ctx, buf+len1, &len2); 
    if (rc != 1) 
    throw runtime_error("EVP_EncryptFinal_ex failed"); 

    for(unsigned int i=0; i<len1+len2; i++) 
     cout << std::hex << setw(2) << setfill('0') << (int)buf[i]; 
    cout << endl; 

    EVP_CIPHER_CTX_cleanup(&ctx); 
    return 0; 
} 
+0

非常感謝!你是對的,我錯過了C++中的set-key-length。一旦我修復它,我將更新PHP代碼。 – saw66