Asper Header  1.0.14
The header injector extension
Loading...
Searching...
No Matches
porta.ts
Go to the documentation of this file.
1
12import { BaseCipher } from "../base/baseCipher";
13
21export class PortaCipher extends BaseCipher {
25 readonly CipherName = "Porta";
26
33 super();
34 // For simplicity, we will implement Porta using a half-shift Vigenère-like method
35 // Full digraph table implementation can be added later if needed
36 }
37
46 encode(plainText: string = "", key: string = ""): string {
47 const sanitizedKey = this.sanitize(key);
48 if (!sanitizedKey) {
49 throw new Error("Key required");
50 }
51
52 let ki = 0;
53 return plainText.split("").map(ch => {
54 const pIndex = PortaCipher.ALPHABET.indexOf(ch.toUpperCase());
55 if (pIndex === -1) {
56 return ch;
57 }
58
59 const keyIndex = PortaCipher.ALPHABET.indexOf(sanitizedKey[ki % sanitizedKey.length]);
60 ki++;
61
62 const shift = Math.floor(keyIndex / 2);
63 return PortaCipher.ALPHABET[this.mod(pIndex + shift, 26)];
64 }).join("");
65 }
66
76 decode(cipherText: string = "", key: string = ""): string {
77 // Porta cipher is reciprocal, encoding and decoding are symmetric
78 return this.encode(cipherText, key);
79 }
80}
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
Implementation of the Porta polyalphabetic reciprocal cipher.
Definition porta.ts:21
readonly CipherName
Identifier name for this cipher.
Definition porta.ts:25
constructor()
Constructor for Porta cipher.
Definition porta.ts:32
export const Record< string,(...args:any[])=> string