| 9 | description: 'Encrypt or decrypt text using Caesar, Vigenere, or XOR cipher', |
| 10 | usage: '.cipher <type> <encode|decode> <key> <text>', |
| 11 | async handler(sock, message, args, context) { |
| 12 | const { chatId, channelInfo } = context; |
| 13 | if (args.length < 4) { |
| 14 | return await sock.sendMessage(chatId, { |
| 15 | text: `🔐 *Text Cipher*\n\n` + |
| 16 | `*Usage:* \`.cipher <type> <encode|decode> <key> <text>\`\n\n` + |
| 17 | `*Cipher types:*\n\n` + |
| 18 | `*caesar* — shift letters by a number (key = number)\n` + |
| 19 | `• \`.cipher caesar encode 13 Hello World\`\n` + |
| 20 | `• \`.cipher caesar decode 13 Uryyb Jbeyq\`\n\n` + |
| 21 | `*vigenere* — polyalphabetic cipher (key = word)\n` + |
| 22 | `• \`.cipher vigenere encode SECRET Hello World\`\n` + |
| 23 | `• \`.cipher vigenere decode SECRET Zincs Pgvnu\`\n\n` + |
| 24 | `*xor* — XOR byte cipher, output is hex (key = any text)\n` + |
| 25 | `• \`.cipher xor encode mykey Hello\`\n` + |
| 26 | `• \`.cipher xor decode mykey 25090a0e06\``, |
| 27 | ...channelInfo |
| 28 | }, { quoted: message }); |
| 29 | } |
| 30 | const cipherType = args[0].toLowerCase(); |
| 31 | const mode = args[1].toLowerCase(); |
| 32 | const key = args[2]; |
| 33 | const text = args.slice(3).join(' ').trim(); |
| 34 | if (!['caesar', 'vigenere', 'xor'].includes(cipherType)) { |
| 35 | return await sock.sendMessage(chatId, { |
| 36 | text: `❌ Unknown cipher: *${cipherType}*\nUse: \`caesar\`, \`vigenere\`, or \`xor\``, |
| 37 | ...channelInfo |
| 38 | }, { quoted: message }); |
| 39 | } |
| 40 | if (!['encode', 'decode', 'encrypt', 'decrypt'].includes(mode)) { |
| 41 | return await sock.sendMessage(chatId, { |
| 42 | text: `❌ Unknown mode: *${mode}*\nUse: \`encode\` or \`decode\``, |
| 43 | ...channelInfo |
| 44 | }, { quoted: message }); |
| 45 | } |
| 46 | if (!text) { |
| 47 | return await sock.sendMessage(chatId, { |
| 48 | text: `❌ No text provided.`, |
| 49 | ...channelInfo |
| 50 | }, { quoted: message }); |
| 51 | } |
| 52 | if (cipherType === 'caesar' && isNaN(parseInt(key, 10))) { |
| 53 | return await sock.sendMessage(chatId, { |
| 54 | text: `❌ Caesar cipher key must be a number (e.g. 13)`, |
| 55 | ...channelInfo |
| 56 | }, { quoted: message }); |
| 57 | } |
| 58 | try { |
| 59 | const bin = getBin('cipher'); |
| 60 | const safeText = text.replace(/"/g, '\\"'); |
| 61 | const safeKey = key.replace(/"/g, '\\"'); |
| 62 | const { stdout, stderr } = await execAsync(`"${bin}" ${cipherType} ${mode} "${safeKey}" "${safeText}"`, { timeout: 10000 }); |
| 63 | if (stderr && !stdout) { |
| 64 | return await sock.sendMessage(chatId, { |
| 65 | text: `❌ ${stderr.trim()}`, |
| 66 | ...channelInfo |
| 67 | }, { quoted: message }); |
| 68 | } |