@
dtsdao #19
ECB 模式无所谓 padding,因为他默认输入是 16byte 的整数倍,就是要求自己 padding。在你的代码里其实就相当于 zero padding
```
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <mbedtls/md.h>
#include <mbedtls/cipher.h>
uint8_t key[] = "88a2a91ee2126bfd";
uint8_t text[80] = {'t', 'e', 's', 't', '1', '2', '3', '4'};
uint8_t len = 0;
int main()
{
mbedtls_cipher_context_t ctx;
mbedtls_cipher_init(&ctx);
mbedtls_cipher_setup(&ctx, mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_ECB));
mbedtls_cipher_setkey(&ctx, key, 128, MBEDTLS_ENCRYPT);
mbedtls_cipher_set_padding_mode(&ctx, MBEDTLS_PADDING_ZEROS);
uint8_t buf[80];
size_t olen = sizeof buf;
size_t idx = 0;
mbedtls_cipher_update(&ctx, text, 16, buf + idx, &olen);
idx += olen;
olen = (sizeof buf) - idx;
mbedtls_cipher_finish(&ctx, buf + idx, &olen);
idx += olen;
mbedtls_cipher_free(&ctx);
for (int i = 0; i < idx; ++i) {
printf("%02hhx", buf[i]);
}
printf("\n");
return 0;
}
```
ECB 模式其实也不需要上面的 mbedtls_cipher_finish,但是为了过程完整我还是写出来了