1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int dummy;
}
aes_encrypt_ctx;
#define BLOCK_LEN 64
static void fillrand(unsigned char*p, size_t size)
{
size_t i;
for (i = 0; i < size; i++)
{
p[i] = rand() % 256;
}
}
static int encfile(FILE *fin, FILE *fout, aes_encrypt_ctx ctx[1])
{
unsigned char dbuf[3 * BLOCK_LEN], b;
unsigned long i, len;
// set a random IV
fillrand(dbuf, BLOCK_LEN);
// read the first file block
len = (unsigned long) fread((char*)dbuf + BLOCK_LEN, 1, BLOCK_LEN, fin);
if (len < BLOCK_LEN)
{ // if the file length is less than one block
printf("len<16 len= %ld\n", len);
// xor the file bytes with the IV bytes
for (i = 0; i < len; ++i)
{
dbuf[i + BLOCK_LEN] ^= dbuf[i];
}
i = 16;
b = dbuf[i];
printf("dbuf[%ld]= '%c'\n", i, b);
}
return 0;
}
int main(void)
{
FILE *fp_in = fopen ("data.bin", "rb");
FILE *fp_out = fopen ("out.bin", "wb");
aes_encrypt_ctx ctx;
encfile(fp_in, fp_out, &ctx);
return 0;
} |
Partager