Asper Header  1.0.14
The header injector extension
Loading...
Searching...
No Matches
autoKey.ts
Go to the documentation of this file.
1
12import { BaseCipher } from "../base/baseCipher";
13
21export class AutokeyCipher extends BaseCipher {
25 readonly CipherName = "Autokey";
26
33 super();
34 }
35
44 encode(plainText: string = "", key: string = ""): string {
45 const cleanPT = this.sanitize(plainText);
46 const cleanKey = this.sanitize(key);
47 let fullKey = (cleanKey + cleanPT).slice(0, cleanPT.length);
48
49 let out = "";
50 for (let i = 0; i < cleanPT.length; i++) {
51 const p = BaseCipher.ALPHABET.indexOf(cleanPT[i]);
52 const k = BaseCipher.ALPHABET.indexOf(fullKey[i]);
53 out += BaseCipher.ALPHABET[this.mod(p + k, 26)];
54 }
55 return out;
56 }
57
66 decode(cipherText: string = "", key: string = ""): string {
67 const cleanCT = this.sanitize(cipherText);
68 let fullKey = this.sanitize(key).split("");
69
70 let out = "";
71 for (const ch of cleanCT) {
72 const c = BaseCipher.ALPHABET.indexOf(ch);
73 const k = BaseCipher.ALPHABET.indexOf(fullKey[0]);
74 const p = BaseCipher.ALPHABET[this.mod(c - k, 26)];
75
76 out += p;
77 fullKey.push(p); // append plaintext to key
78 fullKey.shift(); // maintain rolling window
79 }
80 return out;
81 }
82}
Implementation of the Autokey polyalphabetic substitution cipher.
Definition autoKey.ts:21
readonly CipherName
Identifier name for this cipher.
Definition autoKey.ts:25
constructor()
Constructor for Autokey cipher.
Definition autoKey.ts:32
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
export const Record< string,(...args:any[])=> string