Asper Header  1.0.14
The header injector extension
Loading...
Searching...
No Matches
trifid.ts
Go to the documentation of this file.
1
13import { BaseCipher } from "../base/baseCipher";
14import { PolybiusCipher } from "./polybius";
15
23export class TrifidCipher extends BaseCipher {
27 readonly CipherName = "Trifid";
28
33 private period: number;
34
40 constructor(period: number = 5) {
41 super();
42 this.period = period; // Default period = 5 (common historical value)
43 }
44
52 private toCoordinates(plaintext: string): number[][] {
53 const poly = new PolybiusCipher();
54 const sanitized = this.sanitize(plaintext);
55 const coordinates: number[][] = [];
56
57 for (const ch of sanitized) {
58 const encoded = poly.encode(ch).replace(/\s/g, ''); // e.g. '23'
59 const row = Number(encoded[0]);
60 const col = Number(encoded[1]);
61 const depth = 1; // Placeholder depth (Trifid normally uses 3x3x3)
62 coordinates.push([row, col, depth]);
63 }
64
65 return coordinates;
66 }
67
75 private fromCoordinates(coords: number[][]): string {
76 const poly = new PolybiusCipher();
77 const output: string[] = [];
78
79 for (const [row, col] of coords) {
80 const code = `${row}${col}`;
81 output.push(poly.decode(code));
82 }
83
84 return output.join('');
85 }
86
94 encode(plaintext: string): string {
95 const coords = this.toCoordinates(plaintext);
96 const flattened = coords.flat(); // e.g. [r1, c1, d1, r2, c2, d2, ...]
97 const blocks: number[][] = [];
98
99 // Split flattened coordinates into period-sized chunks
100 for (let i = 0; i < flattened.length; i += this.period * 3) {
101 blocks.push(flattened.slice(i, i + this.period * 3));
102 }
103
104 const encodedCoords: number[][] = [];
105
106 // Regroup each block’s coordinates
107 for (const block of blocks) {
108 const third = Math.ceil(block.length / 3);
109
110 for (let i = 0; i < third; i++) {
111 const triplet = [
112 block[i] ?? 1,
113 block[i + third] ?? 1,
114 block[i + 2 * third] ?? 1,
115 ];
116 encodedCoords.push(triplet);
117 }
118 }
119
120 return this.fromCoordinates(encodedCoords);
121 }
122
130 decode(ciphertext: string): string {
131 const coords = this.toCoordinates(ciphertext);
132 const flattened = coords.flat();
133 const blocks: number[][] = [];
134
135 for (let i = 0; i < flattened.length; i += this.period * 3) {
136 blocks.push(flattened.slice(i, i + this.period * 3));
137 }
138
139 const decodedCoords: number[][] = [];
140
141 for (const block of blocks) {
142 const third = Math.ceil(block.length / 3);
143
144 for (let i = 0; i < third; i++) {
145 const triplet = [
146 block[i] ?? 1,
147 block[i + third] ?? 1,
148 block[i + 2 * third] ?? 1,
149 ];
150 decodedCoords.push(triplet);
151 }
152 }
153
154 return this.fromCoordinates(decodedCoords);
155 }
156}
Abstract base class providing common functionality for cipher implementations.
Definition baseCipher.ts:18
Implementation of the Polybius Square substitution cipher.
Definition polybius.ts:21
Implementation of the Trifid fractionating transposition cipher.
Definition trifid.ts:23
readonly CipherName
Identifier name for this cipher.
Definition trifid.ts:27
constructor(period:number=5)
Constructor for Trifid cipher.
Definition trifid.ts:40
PolybiusCipher
Definition index.ts:22
export const Record< string,(...args:any[])=> string