Skip to content

Instantly share code, notes, and snippets.

@vklachkov
Created May 20, 2021 06:13
Show Gist options
  • Save vklachkov/38773bdb167aa42d043b3e6eedf8a126 to your computer and use it in GitHub Desktop.
Save vklachkov/38773bdb167aa42d043b3e6eedf8a126 to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <cstring>
#include "aes.hpp"
#define PRINTF_BUFHEX(buf, n) \
for (int i = 0; i < n; i++) \
{ \
printf("0x%02X ", buf[i]); \
} \
printf("\n");
const uint8_t key[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
const uint8_t iv[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
const uint8_t data_size = 16;
int main(int argc, char** argv)
{
AES_ctx aes_ctx;
uint8_t data[data_size][data_size] = { 0 };
// Заполняем данными
for (uint8_t i = 0; i < data_size; i++)
{
memset(&(data[i]), 0, data_size);
snprintf((char*)&(data[i]), data_size, "%03d:%03d:%03d", i, i + 5, i + 15);
}
printf("Original data:\n");
for (uint8_t i = 0; i < data_size; i++)
{
printf("%s\n", (char*)&(data[i]));
}
printf("\n");
// Инициализируем AES
printf("Initialize AES\n");
printf("Key: ");
PRINTF_BUFHEX(key, sizeof(key));
printf("IV: ");
PRINTF_BUFHEX(iv, sizeof(iv));
AES_init_ctx_iv(&aes_ctx, (uint8_t*)&key, (uint8_t*)&iv);
printf("\n");
// Шифруем данные запись за записью
for (uint8_t i = 0; i < data_size; i++)
{
AES_CBC_encrypt_buffer(&aes_ctx, (uint8_t*)&(data[i]), data_size);
}
printf("Crypted data:\n");
for (uint8_t x = 0; x < data_size; x++)
{
PRINTF_BUFHEX(data[x], data_size);
}
printf("\n");
// Переинициализируем AES
printf("Reinitialize AES\n");
printf("Key: ");
PRINTF_BUFHEX(key, sizeof(key));
printf("IV: ");
PRINTF_BUFHEX(iv, sizeof(iv));
AES_init_ctx_iv(&aes_ctx, (uint8_t*)&key, (uint8_t*)&iv);
printf("\n");
// Дешифруем данные запись за записью
for (uint8_t i = 0; i < data_size; i++)
{
AES_CBC_decrypt_buffer(&aes_ctx, (uint8_t*)&(data[i]), data_size);
}
printf("Enrypted data:\n");
for (uint8_t x = 0; x < data_size; x++)
{
printf("%s\n", (char*)&(data[x]));
}
printf("\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment