Asper Header  1.0.14
The header injector extension
Loading...
Searching...
No Matches
scytale.ts
Go to the documentation of this file.
1
12import { BaseCipher } from "../base/baseCipher";
13
21export class ScytaleCipher extends BaseCipher {
25 readonly CipherName = "Scytale";
26
32 super();
33 }
34
43 encode(plaintext: string, diameter: number): string {
44 const cols = Math.ceil(plaintext.length / diameter);
45 let result = "";
46
47 // Read column by column after filling the grid row by row
48 for (let c = 0; c < cols; c++) {
49 for (let r = 0; r < diameter; r++) {
50 const index = c + r * cols;
51 if (index < plaintext.length) {
52 result += plaintext[index];
53 }
54 }
55 }
56
57 return result;
58 }
59
65 decode(ciphertext: string, diameter: number): string {
66 const cols = Math.ceil(ciphertext.length / diameter);
67 const grid: string[] = Array(diameter * cols).fill("");
68 let pos = 0;
69
70 // Reconstruct grid in column-major order
71 for (let c = 0; c < cols; c++) {
72 for (let r = 0; r < diameter; r++) {
73 const index = c + r * cols;
74 if (pos < ciphertext.length) {
75 grid[index] = ciphertext[pos++];
76 }
77 }
78 }
79
80 // Read off row by row to recover plaintext
81 return grid.join("").trimEnd();
82 }
83}
Abstract base class providing common functionality for cipher implementations.
Definition baseCipher.ts:18
Implementation of the ancient Scytale transposition cipher.
Definition scytale.ts:21
constructor()
Constructor for Scytale cipher.
Definition scytale.ts:31
readonly CipherName
Identifier name for this cipher.
Definition scytale.ts:25
export const Record< string,(...args:any[])=> string