Asper Header  1.0.14
The header injector extension
Loading...
Searching...
No Matches
polybius.ts
Go to the documentation of this file.
1
12import { BaseCipher } from "../base/baseCipher";
13
21export class PolybiusCipher extends BaseCipher {
25 readonly CipherName = "Polybius";
26
30 private grid: Record<string, string> = {};
31
35 private inverse: Record<string, string> = {};
36
43 super();
44
45 // Create 5x5 grid for letters A-Z (merge I/J)
46 const letters = PolybiusCipher.ALPHABET.replace('J', '');
47 let idx = 0;
48
49 for (let r = 1; r <= 5; r++) {
50 for (let c = 1; c <= 5; c++) {
51 const ch = letters[idx++];
52 const key = `${r}${c}`;
53 this.grid[ch] = key;
54 this.inverse[key] = ch;
55 }
56 }
57 }
58
66 encode(plainText: string = ""): string {
67 return plainText
68 .split("")
69 .map(ch => {
70 const up = ch.toUpperCase();
71 if (up === 'J') {
72 return this.grid['I'];
73 }
74 if (/[A-Z]/.test(up)) {
75 return this.grid[up] ?? ch;
76 }
77 return ch;
78 })
79 .join(' ');
80 }
81
89 decode(cipherText: string = ""): string {
90 return cipherText
91 .split(/\s+/)
92 .map(tok => this.inverse[tok] ?? tok)
93 .join('');
94 }
95}
Abstract base class providing common functionality for cipher implementations.
Definition baseCipher.ts:18
static readonly ALPHABET
Standard English alphabet for cipher operations.
Definition baseCipher.ts:44
Implementation of the Polybius Square substitution cipher.
Definition polybius.ts:21
constructor()
Constructor for Polybius Square cipher.
Definition polybius.ts:42
readonly CipherName
Identifier name for this cipher.
Definition polybius.ts:25
export const Record< string,(...args:any[])=> string