Skip to content

Instantly share code, notes, and snippets.

@hongphuc5497
Forked from vlucas/encryption.ts
Created August 16, 2022 08:36

Revisions

  1. @vlucas vlucas revised this gist Jun 14, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion encryption.js
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    const crypto = require('crypto');

    const ENCRYPTION_KEY = process.env.ENCRYPTION_KEY; // Must be 256 bytes (32 characters)
    const ENCRYPTION_KEY = process.env.ENCRYPTION_KEY; // Must be 256 bits (32 characters)
    const IV_LENGTH = 16; // For AES, this is always 16

    function encrypt(text) {
  2. @vlucas vlucas revised this gist Jun 14, 2019. 1 changed file with 12 additions and 12 deletions.
    24 changes: 12 additions & 12 deletions encryption.js
    Original file line number Diff line number Diff line change
    @@ -6,25 +6,25 @@ const ENCRYPTION_KEY = process.env.ENCRYPTION_KEY; // Must be 256 bytes (32 char
    const IV_LENGTH = 16; // For AES, this is always 16

    function encrypt(text) {
    let iv = crypto.randomBytes(IV_LENGTH);
    let cipher = crypto.createCipheriv('aes-256-cbc', new Buffer(ENCRYPTION_KEY), iv);
    let encrypted = cipher.update(text);
    let iv = crypto.randomBytes(IV_LENGTH);
    let cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(ENCRYPTION_KEY), iv);
    let encrypted = cipher.update(text);

    encrypted = Buffer.concat([encrypted, cipher.final()]);
    encrypted = Buffer.concat([encrypted, cipher.final()]);

    return iv.toString('hex') + ':' + encrypted.toString('hex');
    return iv.toString('hex') + ':' + encrypted.toString('hex');
    }

    function decrypt(text) {
    let textParts = text.split(':');
    let iv = new Buffer(textParts.shift(), 'hex');
    let encryptedText = new Buffer(textParts.join(':'), 'hex');
    let decipher = crypto.createDecipheriv('aes-256-cbc', new Buffer(ENCRYPTION_KEY), iv);
    let decrypted = decipher.update(encryptedText);
    let textParts = text.split(':');
    let iv = Buffer.from(textParts.shift(), 'hex');
    let encryptedText = Buffer.from(textParts.join(':'), 'hex');
    let decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(ENCRYPTION_KEY), iv);
    let decrypted = decipher.update(encryptedText);

    decrypted = Buffer.concat([decrypted, decipher.final()]);
    decrypted = Buffer.concat([decrypted, decipher.final()]);

    return decrypted.toString();
    return decrypted.toString();
    }

    module.exports = { decrypt, encrypt };
  3. @vlucas vlucas created this gist Jan 26, 2017.
    30 changes: 30 additions & 0 deletions encryption.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    'use strict';

    const crypto = require('crypto');

    const ENCRYPTION_KEY = process.env.ENCRYPTION_KEY; // Must be 256 bytes (32 characters)
    const IV_LENGTH = 16; // For AES, this is always 16

    function encrypt(text) {
    let iv = crypto.randomBytes(IV_LENGTH);
    let cipher = crypto.createCipheriv('aes-256-cbc', new Buffer(ENCRYPTION_KEY), iv);
    let encrypted = cipher.update(text);

    encrypted = Buffer.concat([encrypted, cipher.final()]);

    return iv.toString('hex') + ':' + encrypted.toString('hex');
    }

    function decrypt(text) {
    let textParts = text.split(':');
    let iv = new Buffer(textParts.shift(), 'hex');
    let encryptedText = new Buffer(textParts.join(':'), 'hex');
    let decipher = crypto.createDecipheriv('aes-256-cbc', new Buffer(ENCRYPTION_KEY), iv);
    let decrypted = decipher.update(encryptedText);

    decrypted = Buffer.concat([decrypted, decipher.final()]);

    return decrypted.toString();
    }

    module.exports = { decrypt, encrypt };