Asper Header  1.0.14
The header injector extension
Loading...
Searching...
No Matches
nihilist.ts
Go to the documentation of this file.
1
12import { BaseCipher } from "../base/baseCipher";
13import { PolybiusCipher } from "./polybius";
14
22export class NihilistCipher extends BaseCipher {
26 readonly CipherName = "Nihilist";
27
33 super();
34 }
35
44 encode(plainText: string = "", key: string = ""): string {
45 const poly = new PolybiusCipher();
46
47 // Convert key to numbers via Polybius
48 const keyNums = this.sanitize(key)
49 .split("")
50 .map(ch => Number(poly.encode(ch).replace(/\s/g, "")));
51
52 // Convert plaintext to numbers via Polybius
53 const plainNums = this.sanitize(plainText)
54 .split("")
55 .map(ch => Number(poly.encode(ch).replace(/\s/g, "")));
56
57 // Add key numbers to plaintext numbers cyclically
58 const encodedNums = plainNums.map((num, i) => num + keyNums[i % keyNums.length]);
59
60 return encodedNums.join(" ");
61 }
62
71 decode(cipherText: string = "", key: string = ""): string {
72 const poly = new PolybiusCipher();
73
74 // Convert key to numbers via Polybius
75 const keyNums = this.sanitize(key)
76 .split("")
77 .map(ch => Number(poly.encode(ch).replace(/\s/g, "")));
78
79 // Convert ciphertext to numbers
80 const cipherNums = cipherText.split(" ").map(n => Number(n));
81
82 // Subtract key numbers cyclically and decode via Polybius
83 const letters = cipherNums.map((num, i) => {
84 const val = num - keyNums[i % keyNums.length];
85 return poly.decode(String(val));
86 });
87
88 return letters.join("");
89 }
90}
Abstract base class providing common functionality for cipher implementations.
Definition baseCipher.ts:18
Implementation of the Nihilist additive coordinate cipher.
Definition nihilist.ts:22
constructor()
Constructor for Nihilist cipher.
Definition nihilist.ts:32
readonly CipherName
Identifier name for this cipher.
Definition nihilist.ts:26
Implementation of the Polybius Square substitution cipher.
Definition polybius.ts:21
export const Record< string,(...args:any[])=> string