#include <openssl/aes.h>
int main(void) {
#define AES_SIZE_BIT 256
unsigned char keys[AES_SIZE_BIT / 8 + 1];
unsigned char buff[1024];
unsigned char encodes[1024];
int len = 0;
memset(buff, 0, sizeof(buff));
memset(keys, 0, sizeof(keys));
memset(encodes,0,sizeof(encodes));
strcpy((char*) keys, "password");
strcpy((char*) buff, "Hello world!");
if (strlen((char *) buff) % AES_BLOCK_SIZE == 0) { /*//aes加密数据输入/输出长度必须是16的倍数*/
len = strlen((char *) buff) + 1;
} else {
len = ((strlen((char *) buff) + 1) / AES_BLOCK_SIZE + 1)
* AES_BLOCK_SIZE;
}
log_d("len=%d", len);
{
uint8_t iv[] = { 0xA, 1, 0xB, 5, 4, 0xF, 7, 9, 0x17, 3, 1, 6, 8, 0xC,
0xD, 91 };
AES_KEY aes;
AES_set_encrypt_key(keys, AES_SIZE_BIT, &aes);
AES_cbc_encrypt(buff, encodes, len, &aes, iv, AES_ENCRYPT);
log_hex(encodes, 100, "encode:");
}
log_d("Key:%s", keys);
{
uint8_t iv[] = { 0xA, 1, 0xB, 5, 4, 0xF, 7, 9, 0x17, 3, 1, 6, 8, 0xC,
0xD, 91 };
log_hex(iv, 16, "iv:");
unsigned char decods[1024];
memset(decods, 0, sizeof(decods));
AES_KEY aes;
AES_set_decrypt_key(keys, AES_SIZE_BIT, &aes); //AES_SIZE_BIT: 128/256
AES_cbc_encrypt(encodes, decods, len, &aes, iv, AES_DECRYPT); //len: %16==0, iv-len=AES_BLOCK_SIZE=16
log_d("%s", decods);
}
return EXIT_SUCCESS;
}
int main(void) {
#define AES_SIZE_BIT 256
unsigned char keys[AES_SIZE_BIT / 8 + 1];
unsigned char buff[1024];
unsigned char encodes[1024];
int len = 0;
memset(buff, 0, sizeof(buff));
memset(keys, 0, sizeof(keys));
memset(encodes,0,sizeof(encodes));
strcpy((char*) keys, "password");
strcpy((char*) buff, "Hello world!");
if (strlen((char *) buff) % AES_BLOCK_SIZE == 0) { /*//aes加密数据输入/输出长度必须是16的倍数*/
len = strlen((char *) buff) + 1;
} else {
len = ((strlen((char *) buff) + 1) / AES_BLOCK_SIZE + 1)
* AES_BLOCK_SIZE;
}
log_d("len=%d", len);
{
uint8_t iv[] = { 0xA, 1, 0xB, 5, 4, 0xF, 7, 9, 0x17, 3, 1, 6, 8, 0xC,
0xD, 91 };
AES_KEY aes;
AES_set_encrypt_key(keys, AES_SIZE_BIT, &aes);
AES_cbc_encrypt(buff, encodes, len, &aes, iv, AES_ENCRYPT);
log_hex(encodes, 100, "encode:");
}
log_d("Key:%s", keys);
{
uint8_t iv[] = { 0xA, 1, 0xB, 5, 4, 0xF, 7, 9, 0x17, 3, 1, 6, 8, 0xC,
0xD, 91 };
log_hex(iv, 16, "iv:");
unsigned char decods[1024];
memset(decods, 0, sizeof(decods));
AES_KEY aes;
AES_set_decrypt_key(keys, AES_SIZE_BIT, &aes); //AES_SIZE_BIT: 128/256
AES_cbc_encrypt(encodes, decods, len, &aes, iv, AES_DECRYPT); //len: %16==0, iv-len=AES_BLOCK_SIZE=16
log_d("%s", decods);
}
return EXIT_SUCCESS;
}