Asper Header  1.0.14
The header injector extension
Loading...
Searching...
No Matches
bifid.ts
Go to the documentation of this file.
1
13import { BaseCipher } from "../base/baseCipher";
14import { PolybiusCipher } from "./polybius";
15
23export class BifidCipher extends BaseCipher {
27 readonly CipherName = "Bifid";
28
35 super();
36 }
37
45 encode(plainText: string = ""): string {
46 const poly = new PolybiusCipher();
47 const sanitizedText = this.sanitize(plainText);
48
49 // Convert each character to Polybius coordinates without spaces
50 const pairs: string[] = [];
51 for (const ch of sanitizedText) {
52 pairs.push(poly.encode(ch).replace(/\s/g, ""));
53 }
54
55 // Flatten into single digits array
56 const coords: number[] = pairs.join("").split("").map(Number);
57
58 // Split and rearrange coordinates
59 const half = Math.ceil(coords.length / 2);
60 const rearranged = coords.slice(0, half).concat(coords.slice(half));
61
62 // Reconstruct letters from paired coordinates
63 const output: string[] = [];
64 for (let i = 0; i < rearranged.length; i += 2) {
65 const code = String(rearranged[i]) + String(rearranged[i + 1]);
66 output.push(poly.decode(code));
67 }
68
69 return output.join("");
70 }
71
79 decode(cipherText: string = ""): string {
80 const poly = new PolybiusCipher();
81 const sanitizedText = this.sanitize(cipherText);
82
83 // Convert each character to Polybius coordinates without spaces
84 const pairs: string[] = [];
85 for (const ch of sanitizedText) {
86 pairs.push(poly.encode(ch).replace(/\s/g, ""));
87 }
88
89 // Flatten into single digits array
90 const coords: number[] = pairs.join("").split("").map(Number);
91
92 // Split coordinates into two halves
93 const half = Math.floor(coords.length / 2);
94 const firstHalf = coords.slice(0, half);
95 const secondHalf = coords.slice(half);
96
97 // Reconstruct letters from paired coordinates
98 const output: string[] = [];
99 for (let i = 0; i < firstHalf.length; i++) {
100 const code = String(firstHalf[i]) + String(secondHalf[i]);
101 output.push(poly.decode(code));
102 }
103
104 return output.join("");
105 }
106}
Abstract base class providing common functionality for cipher implementations.
Definition baseCipher.ts:18
Implementation of the Bifid fractionating transposition cipher.
Definition bifid.ts:23
readonly CipherName
Identifier name for this cipher.
Definition bifid.ts:27
constructor()
Constructor for Bifid cipher.
Definition bifid.ts:34
Implementation of the Polybius Square substitution cipher.
Definition polybius.ts:21
export const Record< string,(...args:any[])=> string