-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaes-encrypt.c
54 lines (52 loc) · 1.18 KB
/
aes-encrypt.c
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
#include <stdio.h>
#include "aes.h"
#define KEYBITS 256
int main(int argc, char **argv)
{
unsigned long rk[RKLENGTH(KEYBITS)];
unsigned char key[KEYLENGTH(KEYBITS)];
int i;
int nrounds;
char *password;
FILE *output;
if (argc < 3)
{
fputs("Missing argument\n", stderr);
return 1;
}
password = argv[1];
for (i = 0; i < sizeof(key); i++)
key[i] = *password != 0 ? *password++ : 0;
output = fopen(argv[2], "wb");
if (output == NULL)
{
fputs("File write error", stderr);
return 1;
}
nrounds = rijndaelSetupEncrypt(rk, key, 256);
while (!feof(stdin))
{
unsigned char plaintext[16];
unsigned char ciphertext[16];
int j;
for (j = 0; j < sizeof(plaintext); j++)
{
int c = getchar();
if (c == EOF)
break;
plaintext[j] = c;
}
if (j == 0)
break;
for (; j < sizeof(plaintext); j++)
plaintext[j] = ' ';
rijndaelEncrypt(rk, nrounds, plaintext, ciphertext);
if (fwrite(ciphertext, sizeof(ciphertext), 1, output) != 1)
{
fclose(output);
fputs("File write error", stderr);
return 1;
}
}
fclose(output);
}