-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_cipher.c
62 lines (59 loc) · 1.64 KB
/
common_cipher.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
#include <stdio.h>
#include <string.h>
#include "common_cipher.h"
#define CESAR 0
#define VIGENERE 1
#define RC4 2
//-------------------------------------------------------------------------
int cipher_init(cipher_t *self, char* method, char* key) {
char cesar[] = "cesar";
char vigenere[] = "vigenere";
char rc4[]= "rc4";
if (method[0] == cesar[0]) {
self->method = CESAR;
cesar_init(&(self->cipher_cesar), key);
} else if (method[0] == vigenere[0]) {
self->method = VIGENERE;
vigenere_init(&(self->cipher_vigenere), key);
} else if (method[0] == rc4[0]) {
self->method = RC4;
rc4_init(&(self->cipher_rc4), key);
} else {
return -1;
}
return 0;
}
int cipher_uninit(cipher_t *self) {
if (self->method == CESAR) {
cesar_uninit(&(self->cipher_cesar));
}
if (self->method == VIGENERE) {
vigenere_uninit(&(self->cipher_vigenere));
}
if (self->method == RC4) {
rc4_uninit(&(self->cipher_rc4));
}
return 0;
}
void cipher_encode(cipher_t *self, char* buffer, size_t buffer_size) {
if (self->method == CESAR) {
cesar_encode(&(self->cipher_cesar), buffer, buffer_size, 1);
}
if (self->method == VIGENERE) {
vigenere_encode(&(self->cipher_vigenere), buffer, buffer_size, 1);
}
if (self->method == RC4) {
rc4_encode(&(self->cipher_rc4), buffer, buffer_size);
}
}
void cipher_decode(cipher_t *self, char* buffer, int bytes_recieved) {
if (self->method == CESAR) {
cesar_encode(&(self->cipher_cesar), buffer, bytes_recieved, 0);
}
if (self->method == VIGENERE) {
vigenere_encode(&(self->cipher_vigenere), buffer, bytes_recieved, 0);
}
if (self->method == RC4) {
rc4_encode(&(self->cipher_rc4), buffer, bytes_recieved);
}
}