A lightweight library that leverages AES encryption to securely encrypt and decrypt JavaScript objectsโsupporting arrays, records, buffers, and more.
npm install aes-object
import { decryptObject, encryptObject } from 'aes-object';
const secretKey = 'mySecretKey';
const data = { message: Buffer.from('Hello, world!') };
const encrypted = encryptObject({ input: data, secretKey });
console.log('Encrypted:', encrypted); // S7WhaSK63RuL3r+AQ5cG5AMSG9r4356DX1wUXZXgTUgeh19GlTl1wbsW1+jZUBqJl0pBKDMBBxAKDrTOqBbD0U5clObURb2OyRCKsf1LtqM=
const decrypted = decryptObject({ input: encrypted, secretKey });
console.log('Decrypted:', decrypted); // { message: <Buffer 48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21> }
console.log('Message:', decrypted.message.toString()); // Hello, world!;
For all configuration options, please see the API docs.
/**
* Encrypts an object using AES encryption.
*
* @template T - The type of the input object.
* @param {AesEncryptObjectParams<T>} params - The encryption parameters.
* @param {T} params.input - The object to be encrypted.
* @param {string} params.secretKey - The secret key for encryption.
* @param {string} [params.iv] - The initialization vector (IV) for encryption. If not provided, a random IV will be generated.
* @returns {string} The encrypted data as a string in Base64 format.
* @throws {TypeError} If the input is not an object.
*/
function encryptObject<T extends ObjectLike>(params: AesEncryptObjectParams<T>): string;
/**
* Decrypts an AES-encrypted string back into an object.
*
* @template T - The expected type of the decrypted object.
* @param {AesDecryptObjectParams} params - The decryption parameters.
* @param {string} params.input - The encrypted string in Base64 format.
* @param {string} params.secretKey - The secret key used for decryption.
* @param {string} [params.iv] - The initialization vector (IV) used for decryption. If not provided, it uses the IV from the encrypted data.
* @returns {T | null} The decrypted object if successful, or null if decryption fails.
*/
function decryptObject<T extends ObjectLike>(params: AesDecryptObjectParams): T | null;
Want to contribute? Awesome! To show your support is to star the project, or to raise issues on GitHub
Thanks again for your support, it is much appreciated! ๐
MIT ยฉ Shahrad Elahi and contributors.