BCryptDecrypt produces garbled output after decrypting first 33 blocks

  aes, bcrypt, c++, encryption

I’m trying to decrypt decrypt data in c++ that was encrypted with .NET’s Aes, CBC, no padding.

I’m using Microsoft’s Code as a go-by.

The first 33 blocks (528 bytes) decrypt perfectly. Then the 34th block is garbled, and TempInitVector is set to 0xcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd. The loop continues without error, and the IV remains unchanged after each loop.

for (int i = 0; i < PlainTextLength; i += 16)
{
    Status = BCryptDecrypt(
        KeyHandle,
        &CipherText[i],
        16,
        NULL,
        TempInitVector,
        TempInitVectorLength,
        &PlainText[i],
        16,
        &ResultLength,
        NULL);

    if (!NT_SUCCESS(Status))
    {
        ReportError(Status);
        goto cleanup;
    }
}

Initially I tried decrypting the entire buffer in one call to BCryptDecrypt, and it failed at the same point after the 33rd block.

I’m truly at a loss. Any ideas how to fix this?

Source: Windows Questions C++

LEAVE A COMMENT