-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon_cesar.c
More file actions
39 lines (32 loc) · 1.13 KB
/
common_cesar.c
File metadata and controls
39 lines (32 loc) · 1.13 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
// Copyright [2020]<Jonathan David Rosenblatt>
#include "common_cesar.h"
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#define ENCODE 1
#define DECODE -1
int cesar_cipher_init(void* cipher, unsigned char* key) {
if (cipher == NULL) return -1;
cipher = (cesar_cipher_t*)cipher;
((cesar_cipher_t*)cipher)->shift = (unsigned int)strtol((char*)key, NULL, 10);
return 0;
}
int cesar_shift_bytes(cesar_cipher_t* cesar, unsigned char msg[],
unsigned int msgLen, int incremento) {
int shift = cesar->shift * incremento;
if (msg == NULL) return -1;
for (int i = 0; i < msgLen; i++) {
unsigned int caracter = msg[i];
msg[i] = (caracter + shift) % 256;
}
return 0;
}
int cesar_encode(void* cipher, unsigned char msg[], unsigned int msgLen) {
if (cipher == NULL || msg == NULL) return -1;
return cesar_shift_bytes((cesar_cipher_t*)cipher, msg, msgLen, ENCODE);
}
int cesar_decode(void* cipher, unsigned char msg[], unsigned int msgLen) {
if (cipher == NULL || msg == NULL) return -1;
return cesar_shift_bytes((cesar_cipher_t*)cipher, msg, msgLen, DECODE);
}
void cesar_destroy(void* cipher) {}