Asper Header  1.0.14
The header injector extension
Loading...
Searching...
No Matches
adfgvx.ts
Go to the documentation of this file.
1
13import { BaseCipher } from "../base/baseCipher";
14import { ColumnarCipher } from "./columnar";
15
23export class ADFGVXCipher extends BaseCipher {
28 private readonly labels = ['A', 'D', 'F', 'G', 'V', 'X'];
29
34 private readonly square: string[];
35
39 readonly CipherName = "ADFGVX";
40
47 super();
48 // 6x6 square with A-Z and 0-9
49 this.square = (BaseCipher.ALPHABET + '0123456789').split('');
50 }
51
60 encode(plainText: string = '', key: string = ''): string {
61 const clean = plainText.toUpperCase();
62 let pairs = '';
63 for (const ch of clean) {
64 const idx = this.square.indexOf(ch);
65 if (idx === -1) {
66 pairs += ch; continue;
67 }
68 const r = Math.floor(idx / 6), c = idx % 6;
69 pairs += this.labels[r] + this.labels[c];
70 }
71 // then columnar transposition
72 return new ColumnarCipher().encode(pairs, key);
73 }
74
83 decode(ciphertext: string = '', key: string = ''): string {
84 const pairs = new ColumnarCipher().decode(ciphertext, key).match(/../g) || [];
85 let out = '';
86 for (const p of pairs) {
87 const r = this.labels.indexOf(p[0]);
88 const c = this.labels.indexOf(p[1]);
89 if (r === -1 || c === -1) {
90 out += p;
91 } else {
92 out += this.square[r * 6 + c];
93 }
94 }
95 return out;
96 }
97}
Implementation of the ADFGVX fractionating transposition cipher.
Definition adfgvx.ts:23
readonly CipherName
Identifier name for this cipher.
Definition adfgvx.ts:39
constructor()
Constructor for ADFGVX cipher.
Definition adfgvx.ts:46
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 Columnar Transposition cipher.
Definition columnar.ts:21
ColumnarCipher
Definition index.ts:19
export const Record< string,(...args:any[])=> string