Asper Header  1.0.14
The header injector extension
Loading...
Searching...
No Matches
route.ts
Go to the documentation of this file.
1
12import { BaseCipher } from "../base/baseCipher";
13
21export class RouteCipher extends BaseCipher {
25 readonly CipherName = "Route";
26
32 super();
33 }
34
43 encode(plaintext: string, width: number): string {
44 const w = width;
45 const h = Math.ceil(plaintext.length / w);
46
47 // Fill grid row by row
48 const grid: string[][] = Array.from({ length: h }, () => Array(w).fill(" "));
49 let index = 0;
50 for (let r = 0; r < h; r++) {
51 for (let c = 0; c < w; c++) {
52 grid[r][c] = plaintext[index++] ?? " ";
53 }
54 }
55
56 // Spiral traversal
57 const result: string[] = [];
58 let top = 0, bottom = h - 1;
59 let left = 0, right = w - 1;
60
61 while (top <= bottom && left <= right) {
62 // Move right along the top row
63 for (let c = left; c <= right; c++) {
64 result.push(grid[top][c]);
65 }
66 top++;
67
68 // Move down the rightmost column
69 for (let r = top; r <= bottom; r++) {
70 result.push(grid[r][right]);
71 }
72 right--;
73
74 // Move left along the bottom row
75 if (top <= bottom) {
76 for (let c = right; c >= left; c--) {
77 result.push(grid[bottom][c]);
78 }
79 bottom--;
80 }
81
82 // Move up the leftmost column
83 if (left <= right) {
84 for (let r = bottom; r >= top; r--) {
85 result.push(grid[r][left]);
86 }
87 left++;
88 }
89 }
90
91 return result.join("");
92 }
93
99 decode(ciphertext: string, width: number): string {
100 const w = width;
101 const h = Math.ceil(ciphertext.length / w);
102 const grid: string[][] = Array.from({ length: h }, () => Array(w).fill(" "));
103
104 let top = 0, bottom = h - 1;
105 let left = 0, right = w - 1;
106 let index = 0;
107
108 // Fill the grid in spiral order
109 while (top <= bottom && left <= right) {
110 for (let c = left; c <= right; c++) {
111 grid[top][c] = ciphertext[index++] ?? " ";
112 }
113 top++;
114
115 for (let r = top; r <= bottom; r++) {
116 grid[r][right] = ciphertext[index++] ?? " ";
117 }
118 right--;
119
120 if (top <= bottom) {
121 for (let c = right; c >= left; c--) {
122 grid[bottom][c] = ciphertext[index++] ?? " ";
123 }
124 bottom--;
125 }
126
127 if (left <= right) {
128 for (let r = bottom; r >= top; r--) {
129 grid[r][left] = ciphertext[index++] ?? " ";
130 }
131 left++;
132 }
133 }
134
135 // Read row by row
136 return grid.map(row => row.join("")).join("");
137 }
138}
Abstract base class providing common functionality for cipher implementations.
Definition baseCipher.ts:18
Implementation of the Route transposition cipher.
Definition route.ts:21
readonly CipherName
Identifier name for this cipher.
Definition route.ts:25
constructor()
Constructor for Route cipher.
Definition route.ts:31
export const Record< string,(...args:any[])=> string