Asper Header  1.0.14
The header injector extension
Loading...
Searching...
No Matches
xor.ts
Go to the documentation of this file.
1
13import { BaseCipher } from "../base/baseCipher";
14
22export class XORCipher extends BaseCipher {
26 readonly CipherName = "XOR";
27
35 super();
36 }
37
47 encode(plaintext: string, key: string): string {
48 if (!key) {
49 throw new Error("Key required");
50 }
51
52 const keyBuffer = Buffer.from(key);
53 const plainBuffer = Buffer.from(plaintext);
54 const output = Buffer.alloc(plainBuffer.length);
55
56 for (let i = 0; i < plainBuffer.length; i++) {
57 output[i] = plainBuffer[i] ^ keyBuffer[i % keyBuffer.length];
58 }
59
60 return output.toString("hex");
61 }
62
73 decode(ciphertext: string, key: string): string {
74 if (!key) {
75 throw new Error("Key required");
76 }
77
78 const keyBuffer = Buffer.from(key);
79 const cipherBuffer = Buffer.from(ciphertext, "hex");
80 const output = Buffer.alloc(cipherBuffer.length);
81
82 for (let i = 0; i < cipherBuffer.length; i++) {
83 output[i] = cipherBuffer[i] ^ keyBuffer[i % keyBuffer.length];
84 }
85
86 return output.toString();
87 }
88}
Abstract base class providing common functionality for cipher implementations.
Definition baseCipher.ts:18
Implementation of XOR (Exclusive OR) encryption/decryption.
Definition xor.ts:22
constructor()
Constructor for XOR cipher.
Definition xor.ts:34
readonly CipherName
Identifier name for this cipher.
Definition xor.ts:26
export const Record< string,(...args:any[])=> string