我正在研究一个项目,该项目涉及加密json字符串(长4000+个字符),然后通过CURL将其发布到PHP后解密。我已经完成了openssl库的工作和编译,它能够将类似的json数据保存为二进制文件,进行读取,解密和使用。只要我只尝试加密字符串并将其发布,openssl就会截断数据。数据包含特殊的chars(utf8mb4),但是由于可以将其写入文件中,所以我对为什么拒绝加密整个字符串感到困惑。我用32字节(256位)密钥和16字节(128位)iv尝试了以下方法:
AES-256-CBC AES-256-CFB AES-256-ECB AES-256-GCM
短字符串工作正常(即<175个字符)。但是更长的字符串(例如JSON有效负载)无法正常工作。即使openssl自己的示例代码也无法加密有效负载。
主要功能(用于测试):https://pastebin.com/ZrpSd88W
int main()
{
OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
/* Set up the key and iv. Do I need to say to not hard code these in a real application? :-) */
/* A 256 bit key */
static const unsigned char key[] = "01234567890123456789012345678901";
/* A 128 bit IV */
static const unsigned char iv[] = "0123456789012345";
/* Message to be encrypted */
unsigned char plaintext[] = "The quick brown fox jumps over the lazy dog";
/* Some additional data to be authenticated */
static const unsigned char aad[] = "Some AAD data";
int decryptedtext_len = 0, ciphertext_len = 0;
printf("");
printf("");
printf("");
printf("");
printf("");
printf("");
printf("");
curl_global_init(CURL_GLOBAL_ALL);
std::string data = "{ \"jobData\": {\"isMultiplayer\": false,\"late\": false,\"sourceCity\": \"Düsseldorf\",\"sourceCompany\": \"Stokes\",\"destinationCity\": \"Duisburg\",\"destinationCompany\": \"LkwLog GmbH\",\"cargo\": \"Wheat\",\"truckMake\": \"Volvo\",\"truckModel\": \"FH16 Classic\",\"game\": \"Euro Truck Simulator 2\",\"sourceCityID\": \"dusseldorf\",\"sourceCompanyID\": \"stokes\",\"destinationCityID\": \"duisburg\",\"destinationCompanyID\": \"lkwlog\",\"cargoID\": \"wheat\",\"truckMakeID\": \"volvo\",\"truckModelID\": \"vehicle.volvo.fh16\",\"gameID\": \"ets2\",\"gameVersion\": \"1.13\",\"pluginVersion\": \"0.15.365.0\",\"income\": 799,\"trailerMass\": 17140,\"distanceDriven\": 7.56641,\"fuelBurned\": 4.75964,\"fuelPurchased\": 0,\"startOdometer\": 64493,\"endOdometer\": 64500.6,\"collisionCount\": 3,\"finishTrailerDamage\": 0.0201135,\"startTrailerDamage\": 0,\"deliveryX\": -13184.1,\"deliveryY\": 58.2873,\"deliveryZ\": -6147.25,\"pickupX\": -13147.2,\"pickupY\": 48.0304,\"pickupZ\": -4555.74,\"trailerDeliveryX\": -13184.2,\"trailerDeliveryY\": 58.2863,\"trailerDeliveryZ\": -6152.03,\"trailerPickupX\": -13147.2,\"trailerPickupY\": 48.0285,\"trailerPickupZ\": -4560.52,\"startEngineDamage\": 0,\"startTransmissionDamage\": 0,\"startCabinDamage\": 0,\"startChassisDamage\": 0,\"startWheelDamage\": 0,\"finishEngineDamage\": 0.0138086,\"finishTransmissionDamage\": 0.00829076,\"finishCabinDamage\": 0.0220714,\"finishChassisDamage\": 0.0275892,\"finishWheelDamage\": 0.00427838,\"totalEngineDamage\": 0.0138086,\"totalTransmissionDamage\": 0.00829076,\"totalCabinDamage\": 0.0220714,\"totalChassisDamage\": 0.0275892,\"totalWheelDamage\": 0.00427838,\"totalTrailerDamage\": 0.0201135,\"osEnvironment\": \"Windows\",\"architecture\": \"x64\",\"steamID\": \"xxx\",\"steamUsername\": \"xxx\",\"navigationDistanceRemaining\": 160.393,\"teleported\": true}}";
unsigned char a[KEY_SIZE+1];
unsigned char b[IV_SIZE + 1];
for (int i = 0; i < KEY_SIZE; i++)
a[i] = 'a';
for (int i = 0; i < IV_SIZE; i++)
b[i] = 'b';
a[KEY_SIZE] = '\0';
b[IV_SIZE] = '\0';
int predicted_len = strlen(data.c_str()) + (BLOCK_SIZE - (strlen(data.c_str()) % BLOCK_SIZE));
predicted_len = strlen(data.c_str());
unsigned char* encrypted = new unsigned char[predicted_len+1];
std::string hex2 = NewMessage::StrToHex(a);
std::string hex3 = NewMessage::StrToHex(b);
//int encrypted_len = NewMessage::encryptcbc((unsigned char*)data.c_str(), strlen(data.c_str()), a, b, encrypted);
int encrypted_len = NewMessage::encrypt((unsigned char*)data.c_str()+'\0', strlen(data.c_str()), a,b, encrypted);
encrypted[predicted_len] = '\0';
std::string hex = NewMessage::StrToHex(encrypted);
std::cout << std::endl << std::endl << std::endl << std::endl;
std::cout << hex << std::endl;
std::cout << std::endl << std::endl << std::endl << std::endl;
std::cout << hex2;
std::cout << std::endl << std::endl << std::endl << std::endl;
std::cout << hex3;
std::cout << std::endl << std::endl << std::endl << std::endl;
data.clear();
//delete[] encrypted;
/*----------------------------------------------------------------------*/
data = "";
curl_global_cleanup();
/* Remove error strings */
ERR_free_strings();
return 0;
}
加密功能:
int encrypt(unsigned char* plaintext, int plaintext_len, unsigned char* key, unsigned char* iv, unsigned char* ciphertext)
{
EVP_CIPHER_CTX* ctx = NULL;
int len = 0, ciphertext_len = 0;
/* Create and initialise the context */
if (!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
/* Initialise the encryption operation. */
if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_ctr(), NULL, key, iv))
handleErrors();
if (plaintext)
{
if (1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
handleErrors();
ciphertext_len = len;
}
if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors();
ciphertext_len += len;
EVP_CIPHER_CTX_free(ctx);
return ciphertext_len;
}
即使ciphertext_len返回正确的值,为什么data
从未被完全加密,我也迷失了。如果有人能指出我正确的方向,那就太好了。
答案 0 :(得分:0)
我使它成功工作,另一位c ++开发人员帮助我解决了所用的StrToHex方法的问题。有问题的方法(固定)发布在下面:
std::string StrToHex(const std::string& in)
{
std::string csHexString;
for (unsigned int i = 0; i < in.size(); i++)
{
char tmp[10];
int len = sprintf(tmp, "%02X", (in[i] & 0xFF));
csHexString += std::string(tmp, len);
}
return csHexString;
}