-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
64 lines (49 loc) · 1.48 KB
/
main.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
55
56
57
58
59
60
61
62
63
64
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "blowfish.h"
/* MUST NOT BE ALTERED */
#define KEYSIZE 56
#define DATASIZE 1024
/* change these to change the ciphertext and the secret key */
#define PLAINTEXT "testing!"
#define KEY "the key is you"
int main()
{
int i, Osize, Psize, Pbyte;
int KOsize, KPsize, KPbyte;
uint8_t *encrypted,
*decrypted,
key[KEYSIZE],
data[DATASIZE];
/* no string NULL termination bugs now :) */
memset(data, 0, DATASIZE);
memset(key, 0, KEYSIZE);
strncpy(data, PLAINTEXT, sizeof(data));
strncpy(key, KEY, sizeof(key));
Osize = strlen(data); KOsize = strlen(key);
Psize = ceil(Osize / 8.0) * 8; KPsize = ceil(KOsize / 8.0) * 8;
Pbyte = Psize - Osize; KPbyte = KPsize - KOsize;
/* padding bytes added to the data and key */
memset(data + Osize, Pbyte, sizeof *data * Pbyte);
memset(key + KOsize, KPbyte, sizeof *key * KPbyte);
blowfish_init(key, KPsize);
encrypted = blowfish_encrypt(data, Psize);
printf("encrypted data: ");
i = 0;
while (i < Psize) {
printf("%.2X%.2X%.2X%.2X ", encrypted[i], encrypted[i + 1],
encrypted[i + 2], encrypted[i + 3]);
printf("%.2X%.2X%.2X%.2X ", encrypted[i + 4], encrypted[i + 5],
encrypted[i + 6], encrypted[i + 7]);
i += 8;
}
printf("\n");
decrypted = blowfish_decrypt(encrypted, Psize);
/* unpadding */
memset(data, 0, Psize);
memmove(data, decrypted, Osize);
printf("decrypted data: ");
printf("%s\n", data);
return 0;
}