简体   繁体   中英

Sending encrypted message from php to a C++ App and decrypting then with CryptoPP

i am trying to send an encrypted message with AES-256-CRT that need to be encoded with a base16 hex cause CryptoPP HexDecoder does it in that way, but i can't decrypt the encrypted text in my C++ application how i can solve this?

PS: I am using libcurl to make the requests from c++.

Here is the code's:

PHP File:

<?php

function EncryptThis($ClearTextData)
{
    $ENCRYPTION_KEY = '7D9BB722DA2DC8674E08C3D44AAE976F';
    $ENCRYPTION_IV = '37C6D22FADE22B2D924598BEE2455EFC';

    return openssl_encrypt($ClearTextData, "AES-256-CTR", $ENCRYPTION_KEY, OPENSSL_RAW_DATA, $ENCRYPTION_IV);
}

$code = 'test';
$encryptedBytes = EncryptThis($code);
echo($encryptedBytes);

PHP echo Output: `]$Q

This is fixed right now, and the answer is the above code. (At least for my problem). I have been trying with a CBC mode but it doens't worked so i tried CRT.

I changed the file reading method and added a decryption function just to check that encryption is working properly.

Please keep in mind that this method of reading is not good for large file encryption (it reads the complete file into memory) - in this situation you may need another solution.

This is a very basic example that does not do any vanity checks ...

<?php
$ENCRYPTION_KEY = 'ThisIsNotMine';
$ENCRYPTION_ALGORITHM = 'AES-256-CBC';

function EncryptThis($ClearTextData)
{
    // This function encrypts the data passed into it and returns the cipher data with the IV embedded within it.
    // The initialization vector (IV) is appended to the cipher data with
    // the use of two colons serve to delimited between the two.
    global $ENCRYPTION_KEY;
    global $ENCRYPTION_ALGORITHM;
    $EncryptionKey = base64_decode($ENCRYPTION_KEY);
    $InitializationVector = openssl_random_pseudo_bytes(openssl_cipher_iv_length($ENCRYPTION_ALGORITHM));
    $EncryptedText = openssl_encrypt($ClearTextData, $ENCRYPTION_ALGORITHM, $EncryptionKey, 0, $InitializationVector);
    return base64_encode($EncryptedText . '::' . $InitializationVector);
}

function DecryptThis($CryptedTextData)
{
    // This function decrypts the data passed into it and returns the clear data
    // The initialization vector (IV) is appended to the cipher data with
    // the use of two colons serve to delimited between the two.
    global $ENCRYPTION_KEY;
    global $ENCRYPTION_ALGORITHM;
    $EncryptionKey = base64_decode($ENCRYPTION_KEY);
    list($EncryptedTextData, $InitializationVector) = explode('::', base64_decode($CryptedTextData), 2);
    $DecryptedText = openssl_decrypt($EncryptedTextData, $ENCRYPTION_ALGORITHM, $EncryptionKey, 0, $InitializationVector);
    return ($DecryptedText);
}

$path = 'file.txt';//file to send can be .exe, .dll, .txt whatever i am just trying with it.
if (file_exists($path)) {
    $code = file_get_contents($path); //Get the code to be encypted.
    $encryptedBytes = EncryptThis($code);
    echo 'encryptedBytes:' . ($encryptedBytes) . '<br>';//send the encrypted bytes to my client.
    // decryption for a simple check
    $decryptedData = DecryptThis($encryptedBytes);
    echo 'decryptedData:' . $decryptedData . '<br>';
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM