A custom function that can encrypt and decrypt texts using the Advanced Encryption Standard (AES) algorithm and supports three different key lengths. Note that this function uses Cipher Block Chaining (CBC) mode. This function can be modified to support other block cipher modes but requires advanced knowledge of JavaScript and algorithms.
Supported Key Lengths
- AES-128-CBC (128 bits or 16 bytes)
- AES-192-CBC (192 bits or 24 bytes)
- AES-256-CBC (256 bits or 32 bytes)
Usage
$aes[text;bit;mode;secretKey?;iv?]
Parameters
text
- The string to be encrypted or decrypted.
bit
- The bit size to be used in the process. This can be either 128, 192, or 256.
mode
- The mode of the process. Mode can be either 'encrypt' or 'decrypt'.
secretKey
- Secret key. The value must be either base64 or hexadecimal string. (Optional)
iv
- The initialization vector. The value must be a hexadecimal string. (Optional)
The parameters secretKey
and iv
are optional in encryption mode, but I highly recommend specifying values for these since these parameters are required when trying to decrypt an encrypted text. The values of these parameters need to match with the corresponding parameters during the encryption process.
Source Code
/**
* @param {String} text - The string to be encrypted or decrypted.
* @param {Number} bit - The bit size to be used in the process, must be either 128, 192, or 256.
* @param {String} mode - The mode of the process, must be either 'encrypt' or 'decrypt'.
* @param {String} secretKey - Secret key, this is optional.
* @param {String} iv - the initialization vector, value must be hexadecimal string. This is optional.
*/
const crypto = require("crypto");
module.exports = {
name: "$aes",
type: "djs",
code: async function (d) {
let data = d.util.aoiFunc(d);
let [text, bit, mode, secretKey = "", iv = ""] = data.inside.splits;
if (!text || !bit || !mode) {
d.aoiError.consoleError("Error", "Missing 'text', 'bit', or 'mode' parameters");
return d.aoiError.fnError(d, "custom", {}, "Usage. Missing Text, Bit, or Mode Parameters");
}
bit = Number(bit);
if (![128, 192, 256].includes(bit)) {
d.aoiError.consoleError("Error", `Invalid bit provided. Bit must be 128, 192, or 256 but provided bit was ${bit}.`);
return d.aoiError.fnError(d, "custom", {}, "Bit Provided In Parameters");
}
if (!['encrypt', 'decrypt'].includes(mode)) {
d.aoiError.consoleError("Error", "Invalid mode provided. Mode must be encrypt or decrypt.");
return d.aoiError.fnError(d, "custom", {}, "Mode Provided In Parameters");
} else if (mode === "decrypt" && (!secretKey || !iv)) {
d.aoiError.consoleError("Error", "Mode is decryption but no secret key or IV is provided.");
return d.aoiError.fnError(d, "custom", {}, "Usage. Missing Key Or Iv In Decryption Mode");
}
let key;
if (!secretKey) {
key = crypto.randomBytes(bit / 8);
} else {
key = Buffer.from(secretKey, secretKey.includes('hex') ? 'hex' : 'base64');
}
const cipher = mode === 'encrypt' ? 'createCipheriv' : 'createDecipheriv';
const algo = `aes-${bit}-cbc`;
let ivBuffer;
if (iv) {
ivBuffer = Buffer.from(iv, 'hex'); // Parse IV if specified
} else {
ivBuffer = crypto.randomBytes(16); // Use the same IV for both encryption and decryption
}
const cryptoObject = crypto[cipher](algo, key, ivBuffer);
let output;
if (mode === 'encrypt') {
try {
output = cryptoObject.update(text, 'utf-8', 'hex');
output += cryptoObject.final('hex');
} catch (err) {
console.error("Error", err);
return d.aoiError.fnError(d, "custom", {}, err);
}
} else {
try {
output = cryptoObject.update(text, 'hex', 'utf-8');
output += cryptoObject.final('utf-8');
} catch (err) {
console.error("Error", err);
return d.aoiError.fnError(d, "custom", {}, err);
}
}
data.result = JSON.stringify({ result: output, secretKey: key, iv: ivBuffer.toString('hex') });
return {
code: d.util.setCode(data)
}
}
}
Expected Output
{"result": "output_in_hex", "secretKey": "the_secret_key", "iv": "initialization_vector_in_hex"}
Got issues or questions? Reply to this discussion.