Asper Header  1.0.14
The header injector extension
Loading...
Searching...
No Matches
beaufort.ts
Go to the documentation of this file.
1
12import { BaseCipher } from "../base/baseCipher";
13
21export class BeaufortCipher extends BaseCipher {
25 readonly CipherName = "Beaufort";
26
32 super();
33 }
34
43 encode(plainText: string = "", key: string = ""): string {
44 const cleanKey = this.sanitize(key);
45 const cleanText = this.sanitize(plainText);
46 const output: string[] = [];
47
48 for (let i = 0; i < cleanText.length; i++) {
49 const ch = cleanText[i];
50 const textIndex = BaseCipher.ALPHABET.indexOf(ch);
51 if (textIndex === -1) {
52 output.push(ch);
53 continue;
54 }
55
56 const keyIndex = BaseCipher.ALPHABET.indexOf(cleanKey[i % cleanKey.length]);
57 const cipherIndex = this.mod(keyIndex - textIndex, 26);
58 output.push(BaseCipher.ALPHABET[cipherIndex]);
59 }
60
61 return output.join("");
62 }
63
72 decode(cipherText: string = "", key: string = ""): string {
73 // Beaufort is symmetric; encoding and decoding are identical
74 return this.encode(cipherText, key);
75 }
76}
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 Beaufort reciprocal polyalphabetic cipher.
Definition beaufort.ts:21
constructor()
Constructor for Beaufort cipher.
Definition beaufort.ts:31
readonly CipherName
Identifier name for this cipher.
Definition beaufort.ts:25
export const Record< string,(...args:any[])=> string