-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencrypt.c
60 lines (46 loc) · 1002 Bytes
/
encrypt.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
#include "include/fjk.h"
/*
* input: 12345678
*
* column: |1|2|3|4|5|6|7|8|
* +-+-+-+-+-+-+-+-+
* tail: |8|7| | | | | | |
* body 1: |8|7| | |1| | |2|
* body 2: |8|7| |3|1| |4|2|
* body 3: |8|7|5|3|1|6|4|2|
*
* output: 87531642
*/
char* fjk_encrypt(const char *decoded, size_t size)
{
char *result;
size_t current, enc_offset, dec_offset;
struct tail t;
struct chunk c;
result = malloc(size);
t.size = size % CHUNK_SZ;
t.idx = t.size - 1;
current = 0;
c.count = size / CHUNK_SZ;
c.idx = 0;
c.offset = CHUNK_SZ - 1;
// Write tail
while (t.idx >= 0) {
enc_offset = t.idx;
dec_offset = size - t.idx - 1;
*(result + enc_offset) = *(decoded + dec_offset);
t.idx -= 1;
}
// Write data
while (current + t.size < size) {
enc_offset = t.size + c.offset + c.idx * CHUNK_SZ;
dec_offset = current;
*(result + enc_offset) = *(decoded + dec_offset);
current += 1;
if (++c.idx >= c.count) {
c.idx = 0;
c.offset -= 1;
}
}
return result;
}