-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbvernam.c
More file actions
72 lines (62 loc) · 2.15 KB
/
bvernam.c
File metadata and controls
72 lines (62 loc) · 2.15 KB
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
#include <stdio.h>
#include <stdlib.h>
#include "bvernam.h"
data_files *load_data_files(char *key_file, char *input_file, char *output_file) {
data_files *pointer = malloc(sizeof(data_files));
FILE *key = fopen(key_file, "rb");
FILE *input = fopen(input_file, "rb");
if (key == NULL || input == NULL)
perror("Error ");
FILE *output = fopen(output_file, "wb");
pointer->key_file = key;
pointer->input_file = input;
pointer->output_file = output;
return pointer;
}
source_byte *load_source_byte(data_files *d) {
source_byte *pointer = malloc(sizeof(source_byte));
fseek(d->key_file, 0L, SEEK_END);
long k_size = ftell(d->key_file);
fseek(d->key_file, 0, SEEK_SET);
// empty input file or key file implies that there is nothing to encrypt
if (fread(&pointer->byte, sizeof(unsigned char), 1, d->input_file) != 1 || k_size == 0)
return NULL;
pointer->byte_index = 0;
pointer->block_index = 0;
pointer->block_size = k_size;
pointer->encrypted_byte = 0;
return pointer;
}
source_byte *update_source_byte(data_files *d, source_byte *s) {
// update indexes
s->byte_index++;
s->block_index = s->byte_index / s->block_size;
long key_index = (s->byte_index + s->block_index) % s->block_size;
// update pointer of key file
if (key_index == 0)
fseek(d->key_file, 0, SEEK_SET);
else if (s->byte_index % s->block_size == 0)
fseek(d->key_file, key_index, SEEK_SET);
// reads a byte from the input file
if (fread(&s->byte, sizeof(unsigned char), 1, d->input_file) < 1)
return NULL;
return s;
}
source_byte *encode(data_files *d, source_byte *s) {
unsigned key_byte;
if (fread(&key_byte, sizeof(unsigned char), 1, d->key_file) != 1)
return NULL;
s->encrypted_byte = key_byte ^ s->byte;
return s;
}
int upload_encrypted_byte(data_files *d, source_byte *s) {
return fwrite(&s->encrypted_byte, sizeof(unsigned char), 1, d->output_file) == 1 ?
1 : -1;
}
void free_bvernam(data_files *d, source_byte *s) {
free(s);
fclose(d->key_file);
fclose(d->input_file);
fclose(d->output_file);
free(d);
}