-
Notifications
You must be signed in to change notification settings - Fork 1
/
bfish.c
110 lines (88 loc) · 2.5 KB
/
bfish.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/****************************************************************************
FILE : bfish.c
LAST REVISION : 2008-09-15
SUBJECT : File encryption program using Blowfish.
PROGRAMMER : (C) Copyright 2008 by Peter C. Chapin
This program allows the user to encrypt or decryption a file using the
Blowfish algorithm.
To Do:
+ Consider using a better way to convert pass phrases into keys.
Please send comments or bug reports to
Peter C. Chapin
Computer Information Systems
Vermont Technical College
Randolph Center, VT 05061
****************************************************************************/
// Standard
#include <stdio.h>
#include <string.h>
// Unix specific
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
// OpenSSL
#include <openssl/blowfish.h>
#define BUFFER_SIZE 4096
extern int optind;
char buffer[BUFFER_SIZE];
int count;
// ============
// Main Program
// ============
int main(int argc, char **argv)
{
int option;
int do_encrypt = 0;
int do_decrypt = 0;
int direction;
int in; // Input file handle.
int out; // Output file handle.
unsigned char raw_key[16];
BF_KEY key;
unsigned char IV[8];
int IV_index;
while ((option = getopt(argc, argv, "ed")) != -1) {
switch (option) {
case 'e': do_encrypt = 1; direction = BF_ENCRYPT; break;
case 'd': do_decrypt = 1; direction = BF_DECRYPT; break;
}
}
if (argc - optind != 3) {
fprintf(stderr,
"Usage: %s -e|-d infile outfile \"pass phrase\"\n", argv[0]);
return 1;
}
if ((do_encrypt == 1 && do_decrypt == 1) ||
(do_encrypt == 0 && do_decrypt == 0)) {
fprintf(stderr, "Exactly one of -e or -d must be specified.\n");
return 1;
}
// Prepare the key.
strncpy(raw_key, argv[optind + 2], 16);
BF_set_key(&key, 16, raw_key);
// Prepare the IV.
memset(IV, 0, 8);
IV_index = 0;
// Open the files.
if ((in = open(argv[optind + 0], O_RDONLY)) == -1) {
perror("Error opening input file");
return 1;
}
if ((out = open(argv[optind + 1], O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1) {
perror("Error opening output file");
close(in);
return 1;
}
while ((count = read(in, buffer, BUFFER_SIZE)) > 0) {
BF_cfb64_encrypt(buffer, buffer, count, &key, IV, &IV_index, direction);
if (write(out, buffer, count) != count) {
perror("Error writing output");
break;
}
}
close(in);
close(out);
return 0;
}