3

I get this error while trying to decrypt in c++ what I encrypt in php, but it happens only sometimes:

B8:48:00:00:error:Provider routines:unpadblock:bad decrypt:providers\implementations\ciphers\ciphercommon_block.c:104:

I use php server to encrypt:

$iv = generateRandomString(16); // 16char random -> send as first 16chars in message
$key = getKey(); // 32char

$data_en = openssl_encrypt($data, "AES-256-CBC", $key, 0, $iv);

echo $iv . $data_en;

This is my c++ client code:

int decrypt(unsigned char* ciphertext, int ciphertext_len, unsigned char* key,
    unsigned char* iv, unsigned char* plaintext)
{
    EVP_CIPHER_CTX* ctx;

    int len;

    int plaintext_len;

    /* Create and initialise the context */
    if (!(ctx = EVP_CIPHER_CTX_new()))
        handleErrors();

    /*
     * Initialise the decryption operation. IMPORTANT - ensure you use a key
     * and IV size appropriate for your cipher
     * In this example we are using 256 bit AES (i.e. a 256 bit key). The
     * IV size for *most* modes is the same as the block size. For AES this
     * is 128 bits
     */
    if (1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
        handleErrors();

    /*
     * Provide the message to be decrypted, and obtain the plaintext output.
     * EVP_DecryptUpdate can be called multiple times if necessary.
     */
    if (1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
        handleErrors();
    plaintext_len = len;

    /*
     * Finalise the decryption. Further plaintext bytes may be written at
     * this stage.
     */
    if (1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len))
        handleErrors();
    plaintext_len += len;

    /* Clean up */
    EVP_CIPHER_CTX_free(ctx);

    return plaintext_len;
}

The most weird thing about this it gives the error once in a time.

sepofep298
  • 31
  • 1

0 Answers0