Asper Header  1.0.14
The header injector extension
Loading...
Searching...
No Matches
bacon.ts
Go to the documentation of this file.
1
12import { BaseCipher } from "../base/baseCipher";
13
21export class BaconCipher extends BaseCipher {
25 readonly CipherName = "Bacon";
26
33 super();
34 }
35
42 private encodeChar(ch: string): string {
43 const index = BaseCipher.ALPHABET.indexOf(ch.toUpperCase());
44 if (index === -1) {
45 return ch;
46 }
47
48 const binary = index.toString(2).padStart(5, "0");
49 return binary.replace(/0/g, "A").replace(/1/g, "B");
50 }
51
58 private decodeToken(token: string): string {
59 if (!/^[AB]{5}$/.test(token)) {
60 return token;
61 }
62
63 const binary = token.replace(/A/g, "0").replace(/B/g, "1");
64 const index = parseInt(binary, 2);
65 return BaseCipher.ALPHABET[index] ?? "?";
66 }
67
75 encode(plainText: string = ""): string {
76 const tokens: string[] = [];
77
78 for (const ch of plainText) {
79 tokens.push(this.encodeChar(ch));
80 }
81
82 return tokens.join(" ");
83 }
84
92 decode(cipherText: string = ""): string {
93 const tokens: string[] = cipherText.split(/\s+/);
94 const output: string[] = [];
95
96 for (const token of tokens) {
97 output.push(this.decodeToken(token));
98 }
99
100 return output.join("");
101 }
102}
Implementation of Bacon's binary steganographic cipher.
Definition bacon.ts:21
readonly CipherName
Identifier name for this cipher.
Definition bacon.ts:25
constructor()
Constructor for Bacon cipher.
Definition bacon.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