NetSim Source Code Help
Loading...
Searching...
No Matches
aes_run.c
Go to the documentation of this file.
1/*
2 * This program demonstrates how to use AES to encrypt/decrypt a
3 * block (128 bits) of data. To encrypt/decrypt data that are
4 * larger than one block, you need to implement one of the modes of
5 * AES, such as AES-ECB and AES-CBC.
6 */
7
8#include "aes.h"
9
10#include <string.h>
11#include <stdlib.h>
12#include <stdio.h>
13
14static unsigned char AES_test[16] =
15{ 0xF5, 0xBF, 0x8B, 0x37, 0x13, 0x6F, 0x2E, 0x1F,
16 0x6B, 0xEC, 0x6F, 0x57, 0x20, 0x21, 0xE3, 0xBA
17};
18
19static unsigned char secret_key[32] =
20{
21 0x4E, 0x46, 0xF8, 0xC5, 0x09, 0x2B, 0x29, 0xE2,
22 0x9A, 0x97, 0x1A, 0x0C, 0xD1, 0xF6, 0x10, 0xFB,
23 0x1F, 0x67, 0x63, 0xDF, 0x80, 0x7A, 0x7E, 0x70,
24 0x96, 0x0D, 0x4C, 0xD3, 0x11, 0x8E, 0x60, 0x1A
25};
26
27
28int aes256( char* str,int* len )
29{
30 int n;
31 int l=*len;
32 aes_context ctx;
33 unsigned char buf[16];
34 unsigned char key[32];
35
36 for (n = 0; n < *len; n+=16,str+=16,l-=16)
37 {
38 /* Set the plain-text */
39 memcpy( buf, str, min(16,l));
40
41 /* Set the key */
42 memcpy( key, secret_key, 16);
43 aes_set_key( &ctx, key, 128 + n * 64);
44
45 aes_encrypt( &ctx, buf, buf );
46 memcpy(str,buf,16);
47 }
48
49 return( 0 );
50}
51
52
#define min(a, b)
Definition: Linux.h:106
int aes_set_key(aes_context *ctx, uint8 *key, int nbits)
Definition: aes.c:437
void aes_encrypt(aes_context *ctx, uint8 input[16], uint8 output[16])
Definition: aes.c:588
static unsigned char secret_key[32]
Definition: aes_run.c:19
static unsigned char AES_test[16]
Definition: aes_run.c:14
int aes256(char *str, int *len)
Definition: aes_run.c:28