top of page
Programming
Writer's pictureGourav Dhar

Encryption in Typescript



In this blog, I have used the crypto library to

  • Generate random numbers

  • Generate Hash

  • Encrypt and decrypt data


I have coded for encrypting data using the 'aes-256-cbc' algorithm. In real life, you must encrypt sensitive information like credit card information, secret keys, etc. before storing it in the database. This will prevent the misuse of this sensitive information if the data gets leaked. You will also need a way to decrypt this information when required. In this blog/video, I have written a basic code in typescript to encrypt and decrypt information.


In this video, I have coded using the crypto library in Node.js to encrypt some random text. I dive into crypto in JavaScript and in TypeScript to generate cryptographically safe random numbers, create strong hashes, and securely encrypt sensitive data such as credit card numbers and CVVs. You'll learn how to store this encrypted data in a database and decrypt it when needed.


Discover the use of crypto in a Node.js project, including how to use Node crypto to encrypt and decrypt tokens. Whether you're working on securing emails, sensitive documents, or any kind of private information, this tutorial will guide you through the steps of encryption and decryption using AES and other algorithms.


By the end of this, you’ll know how to encrypt emails, protect your data from hacking, and securely handle sensitive information in your Node.js applications. Plus, I cover practical examples like how to encrypt and decrypt text in JavaScript, and Typescript, and explore topics like initialization vectors, cipher modes, and more.



Generate random numbers


import * as crypto from 'crypto';

// async
crypto.randomBytes(16, (err, buf) => {
 console.log(buf.toString('hex'));
});

// sync
let iv1 = crypto.randomBytes(16); // initialization vector
console.log(iv1.toString('hex'));

Generate hash


let hash = crypto.createHash('md5-sha1').update('code with gd').digest('hex');
console.log(hash);
console.log(crypto.getHashes());



Encrypt data


let iv = crypto.randomBytes(16);
let credit_card = '901445912340985';
let cvv = '725';

// console.log(crypto.getCiphers());

let secret_key = 'n8XIkjtyxtJk80mwstUTEHLPf4gOfLQI'; // store in //configs

let cipher = crypto.createCipheriv('aes-256-cbc', secret_key, iv);
let encrypted_credit_card = cipher.update(credit_card, 'utf-8', 'hex');
encrypted_credit_card += cipher.final('hex');

let cipherCvv = crypto.createCipheriv('aes-256-cbc', secret_key, iv);
let encrypted_cvv = cipherCvv.update(cvv, 'utf-8', 'hex');
encrypted_cvv += cipherCvv.final('hex');

console.log('Encrypted Credit Card Number: ', encrypted_credit_card);
console.log('Encrypted Credit Card CVV: ', encrypted_cvv);

console.log('--------------');


Decrypt Data

// Decipher
let decipher = crypto.createDecipheriv('aes-256-cbc', secret_key, iv);
let decrypted_credit_card = decipher.update(
 encrypted_credit_card,
 'hex',
 'utf-8'
);

decrypted_credit_card += decipher.final('utf8');
let decipherCvv = crypto.createDecipheriv('aes-256-cbc', secret_key, iv);
let decrypted_cvv = decipherCvv.update(encrypted_cvv, 'hex', 'utf-8');

decrypted_cvv += decipherCvv.final('utf8');
console.log('Deciphered Credit Card Number: ', decrypted_credit_card);

console.log('Deciphered Credit Card CVV: ', decrypted_cvv);



5 comments

5件のコメント


uhuk
10月13日
いいね!




chat
10月05日

Ücretsiz Rastgele Görüntülü Sohbet Kameralı Sohbet Gabile Sohbet Canlı Sohbet Cinsel Sohbet imkanı.


Ücretsiz Rastgele Görüntülü Chat Kameralı Chat Gabile Chat Canlı Chat Cinsel Chat imkanı.


https://www.gevezeyeri.com/cinselsohbet.html  Ücretsiz Rastgele Cinsel Sohbet imkanı.


https://www.gevezeyeri.com/gabilesohbet.html Ücretsiz Rastgele Gabile Sohbet imkanı.


https://www.gevezeyeri.com/cinsel-chat Ücretsiz Rastgele Cinsel Chat imkanı.


https://www.gevezeyeri.com/gabile-chat Ücretsiz Rastgele Gabile Chat imkanı.


Android uyumlu dokunmatik ekran akıllı cep telefonları, tablet, ipad, iphone gibi Mobil cihazlarla tek bir tıkla Mobil Sohbet ve Mobil Chat’a katılabilırsıniz.


https://www.gevezeyeri.com/ Ücretsiz Rastgele Sohbet Sohbet Odaları Chat imkanı.


https://www.gevezeyeri.com/chat.html Ücretsiz Rastgele Chat Chat Odaları Chat Sitesi Chat Siteleri imkanı.


https://www.gevezeyeri.com/sohbet.html Ücretsiz Rastgele Sohbet Sitesi Sohbet Siteleri Sohbet odasi imkanı.


https://www.gevezeyeri.com/mobil-sohbet.html Ücretsiz Rastgele Mobil Sohbet Mobil Sohbet odaları imkanı.


https://www.gevezeyeri.com/sohbetodalari.html Ücretsiz Rastgele Sohbet Odaları imkanı.


https://www.gevezeyeri.com/mobil-chat.html Ücretsiz Rastgele Mobil Chat Mobil Chat Odaları imkanı.


https://www.pinterest.co.kr/chatodalari0357/


https://www.pinterest.co.uk/mobilsohbetodalari/


https://mx.pinterest.com/sevyelisohbet/


https://www.pinterest.de/sohbetsohbet0719/


https://www.pinterest.fr/chatsohbet0342/


https://www.pinterest.cl/ucretsizsohbet/


https://at.pinterest.com/istanbulsohbet/


https://taplink.cc/sohbetodalari


https://os.mbed.com/users/mobilsohbet/


https://eternagame.org/players/408511


いいね!
download (7)_edited.png
Subscribe to my Youtube Channel @codewithgd

Related Articles

Videos you might like

Let's Get
Social

  • alt.text.label.Twitter
  • alt.text.label.LinkedIn
  • 25231
Subscribe to our NewsLetter

Join our mailing list to get a notification whenever a new blog is published. Don't worry we will not spam you.

bottom of page