-
Notifications
You must be signed in to change notification settings - Fork 638
Expand file tree
/
Copy pathcipher.js
More file actions
89 lines (89 loc) Β· 4.02 KB
/
Copy pathcipher.js
File metadata and controls
89 lines (89 loc) Β· 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { getBin } from '../lib/compile.js';
import { exec } from 'child_process';
import { promisify } from 'util';
const execAsync = promisify(exec);
export default {
command: 'cipher',
aliases: ['encrypt', 'decrypt', 'encode', 'crypt'],
category: 'utility',
description: 'Encrypt or decrypt text using Caesar, Vigenere, or XOR cipher',
usage: '.cipher <type> <encode|decode> <key> <text>',
async handler(sock, message, args, context) {
const { chatId, channelInfo } = context;
if (args.length < 4) {
return await sock.sendMessage(chatId, {
text: `π *Text Cipher*\n\n` +
`*Usage:* \`.cipher <type> <encode|decode> <key> <text>\`\n\n` +
`*Cipher types:*\n\n` +
`*caesar* β shift letters by a number (key = number)\n` +
`β’ \`.cipher caesar encode 13 Hello World\`\n` +
`β’ \`.cipher caesar decode 13 Uryyb Jbeyq\`\n\n` +
`*vigenere* β polyalphabetic cipher (key = word)\n` +
`β’ \`.cipher vigenere encode SECRET Hello World\`\n` +
`β’ \`.cipher vigenere decode SECRET Zincs Pgvnu\`\n\n` +
`*xor* β XOR byte cipher, output is hex (key = any text)\n` +
`β’ \`.cipher xor encode mykey Hello\`\n` +
`β’ \`.cipher xor decode mykey 25090a0e06\``,
...channelInfo
}, { quoted: message });
}
const cipherType = args[0].toLowerCase();
const mode = args[1].toLowerCase();
const key = args[2];
const text = args.slice(3).join(' ').trim();
if (!['caesar', 'vigenere', 'xor'].includes(cipherType)) {
return await sock.sendMessage(chatId, {
text: `β Unknown cipher: *${cipherType}*\nUse: \`caesar\`, \`vigenere\`, or \`xor\``,
...channelInfo
}, { quoted: message });
}
if (!['encode', 'decode', 'encrypt', 'decrypt'].includes(mode)) {
return await sock.sendMessage(chatId, {
text: `β Unknown mode: *${mode}*\nUse: \`encode\` or \`decode\``,
...channelInfo
}, { quoted: message });
}
if (!text) {
return await sock.sendMessage(chatId, {
text: `β No text provided.`,
...channelInfo
}, { quoted: message });
}
if (cipherType === 'caesar' && isNaN(parseInt(key, 10))) {
return await sock.sendMessage(chatId, {
text: `β Caesar cipher key must be a number (e.g. 13)`,
...channelInfo
}, { quoted: message });
}
try {
const bin = getBin('cipher');
const safeText = text.replace(/"/g, '\\"');
const safeKey = key.replace(/"/g, '\\"');
const { stdout, stderr } = await execAsync(`"${bin}" ${cipherType} ${mode} "${safeKey}" "${safeText}"`, { timeout: 10000 });
if (stderr && !stdout) {
return await sock.sendMessage(chatId, {
text: `β ${stderr.trim()}`,
...channelInfo
}, { quoted: message });
}
const result = stdout.trim();
const cipherNames = {
caesar: 'Caesar', vigenere: 'Vigenère', xor: 'XOR'
};
const modeLabel = (mode === 'encode' || mode === 'encrypt') ? 'π Encrypted' : 'π Decrypted';
await sock.sendMessage(chatId, {
text: `π *${cipherNames[cipherType]} Cipher*\n\n` +
`π₯ *Input:* \`${text}\`\n` +
`π *Key:* \`${key}\`\n` +
`${modeLabel}: \`${result}\``,
...channelInfo
}, { quoted: message });
}
catch (error) {
await sock.sendMessage(chatId, {
text: `β Failed: ${error.message}`,
...channelInfo
}, { quoted: message });
}
}
};