Stackbits
Utilities > Encryption Decryption

Encryption Decryption

Easily encrypt and decrypt data with secure, hassle-free functions. Whether you're protecting sensitive information or just hiding your secret taco recipe, we've got you covered!

See it in Action 👇🏻

Follow below steps 👇🏻

Secret Key

Copy a secret key using the button below and store it in your .env file or somewhere safe.

Encryption / Decryption Handlers

Secure your data with ease using the functions below. Simply copy and paste them into your code to encrypt sensitive information and decrypt it when needed. No hassle, just seamless security!

1
import crypto from 'crypto';
2
3
const IV_LENGTH = 16;
4
5
const CRYPTO_SECRET = <YOUR SECRET KEY>; // Replace with your own secret key
6
7
// * Encrypt the payload
8
const encryptData = (payload) => {
9
const iv = crypto.randomBytes(IV_LENGTH);
10
const cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(CRYPTO_SECRET, 'base64'), iv);
11
12
let encrypted = cipher.update(JSON.stringify(payload), 'utf8', 'base64');
13
encrypted += cipher.final('base64');
14
15
return { encryptedData: encrypted, iv: iv.toString('base64') };
16
};
17
18
// * Decrypt the payload
19
const decryptData = (encryptedData, iv) => {
20
const decipher = crypto.createDecipheriv(
21
'aes-256-cbc',
22
Buffer.from(CRYPTO_SECRET, 'base64'),
23
Buffer.from(iv, 'base64')
24
);
25
26
let decrypted = decipher.update(encryptedData, 'base64', 'utf8');
27
decrypted += decipher.final('utf8');
28
29
return JSON.parse(decrypted);
30
};

⭐️ Got a question or feedback?
Feel free to reach out!