47 encode(plaintext:
string, key:
string):
string {
49 throw new Error(
"Key required");
52 const keyBuffer = Buffer.from(key);
53 const plainBuffer = Buffer.from(plaintext);
54 const output = Buffer.alloc(plainBuffer.length);
56 for (let i = 0; i < plainBuffer.length; i++) {
57 output[i] = plainBuffer[i] ^ keyBuffer[i % keyBuffer.length];
60 return output.toString(
"hex");
73 decode(ciphertext:
string, key:
string):
string {
75 throw new Error(
"Key required");
78 const keyBuffer = Buffer.from(key);
79 const cipherBuffer = Buffer.from(ciphertext,
"hex");
80 const output = Buffer.alloc(cipherBuffer.length);
82 for (let i = 0; i < cipherBuffer.length; i++) {
83 output[i] = cipherBuffer[i] ^ keyBuffer[i % keyBuffer.length];
86 return output.toString();