3
我用mbedtls庫編寫了以下簡單的加密解密程序。加密工作正常(檢查http://aes.online-domain-tools.com/)。但是,當解密回來我得到不正確的結果(輸出2和輸入是不同的)。我濫用lib嗎?解密如何在mbedtls中工作?
int main()
{
mbedtls_aes_context aes;
mbedtls_aes_context aes2;
unsigned char key[16] = "itzkbgulrcsjmnv";
key[15] = 'x';
unsigned char iv[16] = {0xb2, 0x4b, 0xf2, 0xf7, 0x7a, 0xc5, 0xec, 0x0c, 0x5e, 0x1f, 0x4d, 0xc1, 0xae, 0x46, 0x5e, 0x75};
const unsigned char *input = (const unsigned char*) "Some string to b";
unsigned char output[128] = {0};
unsigned char output2[128] = {0};
mbedtls_aes_setkey_enc(&aes, key, 16*8);
mbedtls_aes_crypt_cbc(&aes, MBEDTLS_AES_ENCRYPT, strlen((const char*)input), iv, input, output);
mbedtls_aes_setkey_dec(&aes2, key, 16*8);
mbedtls_aes_crypt_cbc(&aes2, MBEDTLS_AES_DECRYPT, strlen((const char*)output), iv, output, output2);
}