Asper Header  1.0.14
The header injector extension
Loading...
Searching...
No Matches
gronsfeld.ts
Go to the documentation of this file.
1
12import { BaseCipher } from "../base/baseCipher";
13
21export class GronsfeldCipher extends BaseCipher {
25 readonly CipherName = "Gronsfeld";
26
32 super();
33 }
34
44 encode(plainText: string = "", key: string = ""): string {
45 const numericKey = key.replace(/[^0-9]/g, '');
46 if (!numericKey) {
47 throw new Error('Numeric key required');
48 }
49
50 let keyIndex = 0;
51 return plainText.split('').map(ch => {
52 const charIndex = GronsfeldCipher.ALPHABET.indexOf(ch.toUpperCase());
53 if (charIndex === -1) {
54 return ch;
55 }
56
57 const shift = Number(numericKey[keyIndex % numericKey.length]);
58 keyIndex++;
59 return GronsfeldCipher.ALPHABET[this.mod(charIndex + shift, 26)];
60 }).join('');
61 }
62
72 decode(cipherText: string = "", key: string = ""): string {
73 const numericKey = key.replace(/[^0-9]/g, '');
74 if (!numericKey) {
75 throw new Error('Numeric key required');
76 }
77
78 let keyIndex = 0;
79 return cipherText.split('').map(ch => {
80 const charIndex = GronsfeldCipher.ALPHABET.indexOf(ch.toUpperCase());
81 if (charIndex === -1) {
82 return ch;
83 }
84
85 const shift = Number(numericKey[keyIndex % numericKey.length]);
86 keyIndex++;
87 return GronsfeldCipher.ALPHABET[this.mod(charIndex - shift, 26)];
88 }).join('');
89 }
90}
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 Gronsfeld polyalphabetic substitution cipher.
Definition gronsfeld.ts:21
readonly CipherName
Identifier name for this cipher.
Definition gronsfeld.ts:25
constructor()
Constructor for Gronsfeld cipher.
Definition gronsfeld.ts:31
export const Record< string,(...args:any[])=> string