-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtranspositions.c
More file actions
72 lines (57 loc) · 1.84 KB
/
transpositions.c
File metadata and controls
72 lines (57 loc) · 1.84 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
// Transpositions
#include "polyalphabetic.h"
void transperoffset(int plaintext[], int len, int d, int n) {
if (d == 1 && n == 0) return; // Identity transformation.
int indx, temp[MAX_CIPHER_LENGTH];
// Periodic decimation.
for (int i = 0; i < len; i++) {
temp[i] = plaintext[(d * i) % len];
}
// Rotation (Offset.)
for (int i = 0; i < len; i++) {
indx = (i + n) % len;
if (indx < 0) {
indx += len;
}
plaintext[i] = temp[indx];
}
return ;
}
void matrix_rotate(int text[], int len, int width, int clockwise) {
if (width <= 1 || width >= len) return; // Identity or 1D matrix
int R = (len + width - 1) / width; // Ceiling division for rows
int W = width;
int temp[MAX_CIPHER_LENGTH];
int idx = 0;
if (clockwise) {
// Read columns left-to-right, but from bottom row to top row
for (int c = 0; c < W; c++) {
for (int r = R - 1; r >= 0; r--) {
int old_idx = r * W + c;
// Only read if the cell is valid (handles incomplete final rows)
if (old_idx < len) {
temp[idx++] = text[old_idx];
}
}
}
} else {
// Anti-clockwise: Read columns right-to-left, top to bottom
for (int c = W - 1; c >= 0; c--) {
for (int r = 0; r < R; r++) {
int old_idx = r * W + c;
if (old_idx < len) {
temp[idx++] = text[old_idx];
}
}
}
}
// Copy back to original array
for (int i = 0; i < len; i++) {
text[i] = temp[i];
}
}
void transmatrix(int text[], int len, int w1, int w2, int clockwise) {
// Perform a K3-like double rotation.
matrix_rotate(text, len, w1, clockwise);
matrix_rotate(text, len, w2, clockwise);
}