Javascript mã hóa dữ liệu

Đôi khi bạn muốn mã hóa thông tin trước khi lưu vào cơ sở dữ liệu để thông tin được an toàn hơn. Ví dụ khi bạn thiết lập mật khẩu cho User thì bạn phải mã hóa chuỗi mật khẩu đó rồi mới lưu vào, từ đó khi đăng nhập CSDL ta sẽ so sánh chuỗi đã mã hóa thay vì so sánh mật khẩu chưa mã hóa

Javascript mã hóa dữ liệu

Bài viết này đã được đăng tại freetuts. net , không được sao chép dưới mọi hình thức.

Trong NodeJS có một module hỗ trợ rất tốt chức năng này đó là module crypto-js, vì vậy trong bài viết này chúng ta sẽ tìm hiểu cách sử dụng module này

1. Cài đặt module Crypto-js trong NodeJS

Để cài đặt Crypto lên Server, bạn sử dụng npm và chạy đoạn mã sau

Bài viết này đã được đăng tại [free tuts. mạng lưới]

npm install crypto-js

Sau khi cài đặt xong là bạn có thể sử dụng ngay

Để cài đặt tại Client, bạn sử dụng Bower và chạy đoạn mã sau

bower install crypto-js

Riêng đôi với trường hợp sử dụng Bower thì chúng ta sẽ tìm hiểu sau

2. Cách sử dụng mô-đun Crypto để mã hóa chuỗi

Trong Crypto có rất nhiều mô-đun riêng và mỗi mô-đun là một loại mã hóa khác nhau. Trong phần này mình sẽ demo một số module để các bạn hiểu nguyên tắc sử dụng. Chúng ta sẽ phân chia thành cách sử dụng ở backend và frontend, nhưng trong bài này chúng ta chỉ học cách sử dụng ở backend thôi nhé

Bước 1. Tạo đối tượng mới CryptoJS

var crypto = require('crypto-js');

Bước 2. Mã hóa (Encrypt)

var crypto = require('crypto-js');

// Mã hóa
var message = crypto.AES.encrypt('Nội dung cần mã hóa', 'itsasecret123').toString();

// Xem chuỗi đã mã hóa
console.log(message);

Sau khi chạy lên, bạn sẽ thấy giao diện như sau

Javascript mã hóa dữ liệu

Bước 3. Giải mã (Decrypt)

var crypto = require('crypto-js');

// Mã hóa
var message = crypto.AES.encrypt('Noi dung can ma hoa', 'ma bi mat').toString();

// Xem chuỗi đã mã hóa
console.log(message);

// Lấy danh sách byte đã mã hóa
var bytes = crypto.AES.decrypt(message, 'ma bi mat');

// Chuyển sang chuỗi gốc
var message_decode = bytes.toString(crypto.enc.Utf8);

console.log(message_decode);

Run up interface will like after

Javascript mã hóa dữ liệu

3. Danh sách các loại mã hóa của Crypto

Trong phần 2 mình chỉ dùng mỗi module AES chứ thực ra nó còn mấy chục module mã khóa khác nhau nữa. Và sau đây là danh sách các module mời các bạn tham khảo nhé

  • var crypto = require('crypto-js');
    
    // Mã hóa
    var message = crypto.AES.encrypt('Noi dung can ma hoa', 'ma bi mat').toString();
    
    // Xem chuỗi đã mã hóa
    console.log(message);
    
    // Lấy danh sách byte đã mã hóa
    var bytes = crypto.AES.decrypt(message, 'ma bi mat');
    
    // Chuyển sang chuỗi gốc
    var message_decode = bytes.toString(crypto.enc.Utf8);
    
    console.log(message_decode);
    4
  • var crypto = require('crypto-js');
    
    // Mã hóa
    var message = crypto.AES.encrypt('Noi dung can ma hoa', 'ma bi mat').toString();
    
    // Xem chuỗi đã mã hóa
    console.log(message);
    
    // Lấy danh sách byte đã mã hóa
    var bytes = crypto.AES.decrypt(message, 'ma bi mat');
    
    // Chuyển sang chuỗi gốc
    var message_decode = bytes.toString(crypto.enc.Utf8);
    
    console.log(message_decode);
    5
  • var crypto = require('crypto-js');
    
    // Mã hóa
    var message = crypto.AES.encrypt('Noi dung can ma hoa', 'ma bi mat').toString();
    
    // Xem chuỗi đã mã hóa
    console.log(message);
    
    // Lấy danh sách byte đã mã hóa
    var bytes = crypto.AES.decrypt(message, 'ma bi mat');
    
    // Chuyển sang chuỗi gốc
    var message_decode = bytes.toString(crypto.enc.Utf8);
    
    console.log(message_decode);
    6
  • var crypto = require('crypto-js');
    
    // Mã hóa
    var message = crypto.AES.encrypt('Noi dung can ma hoa', 'ma bi mat').toString();
    
    // Xem chuỗi đã mã hóa
    console.log(message);
    
    // Lấy danh sách byte đã mã hóa
    var bytes = crypto.AES.decrypt(message, 'ma bi mat');
    
    // Chuyển sang chuỗi gốc
    var message_decode = bytes.toString(crypto.enc.Utf8);
    
    console.log(message_decode);
    7
  • var crypto = require('crypto-js');
    
    // Mã hóa
    var message = crypto.AES.encrypt('Noi dung can ma hoa', 'ma bi mat').toString();
    
    // Xem chuỗi đã mã hóa
    console.log(message);
    
    // Lấy danh sách byte đã mã hóa
    var bytes = crypto.AES.decrypt(message, 'ma bi mat');
    
    // Chuyển sang chuỗi gốc
    var message_decode = bytes.toString(crypto.enc.Utf8);
    
    console.log(message_decode);
    8
  • bower install crypto-js
    0
  • bower install crypto-js
    1
  • bower install crypto-js
    2
  • bower install crypto-js
    3
  • bower install crypto-js
    4
  • bower install crypto-js
    5
  • bower install crypto-js
    6
  • bower install crypto-js
    7
  • bower install crypto-js
    8
  • bower install crypto-js
    9
  • var crypto = require('crypto-js');
    0
  • var crypto = require('crypto-js');
    1
  • var crypto = require('crypto-js');
    2
  • var crypto = require('crypto-js');
    3
  • var crypto = require('crypto-js');
    4
  • var crypto = require('crypto-js');
    5
  • var crypto = require('crypto-js');
    6
  • var crypto = require('crypto-js');
    7
  • var crypto = require('crypto-js');
    8
  • var crypto = require('crypto-js');
    9
  • var crypto = require('crypto-js');
    
    // Mã hóa
    var message = crypto.AES.encrypt('Nội dung cần mã hóa', 'itsasecret123').toString();
    
    // Xem chuỗi đã mã hóa
    console.log(message);
    0
  • var crypto = require('crypto-js');
    
    // Mã hóa
    var message = crypto.AES.encrypt('Nội dung cần mã hóa', 'itsasecret123').toString();
    
    // Xem chuỗi đã mã hóa
    console.log(message);
    1
  • var crypto = require('crypto-js');
    
    // Mã hóa
    var message = crypto.AES.encrypt('Nội dung cần mã hóa', 'itsasecret123').toString();
    
    // Xem chuỗi đã mã hóa
    console.log(message);
    2
  • var crypto = require('crypto-js');
    
    // Mã hóa
    var message = crypto.AES.encrypt('Nội dung cần mã hóa', 'itsasecret123').toString();
    
    // Xem chuỗi đã mã hóa
    console.log(message);
    3
  • var crypto = require('crypto-js');
    
    // Mã hóa
    var message = crypto.AES.encrypt('Nội dung cần mã hóa', 'itsasecret123').toString();
    
    // Xem chuỗi đã mã hóa
    console.log(message);
    4
  • var crypto = require('crypto-js');
    
    // Mã hóa
    var message = crypto.AES.encrypt('Nội dung cần mã hóa', 'itsasecret123').toString();
    
    // Xem chuỗi đã mã hóa
    console.log(message);
    5
  • var crypto = require('crypto-js');
    
    // Mã hóa
    var message = crypto.AES.encrypt('Nội dung cần mã hóa', 'itsasecret123').toString();
    
    // Xem chuỗi đã mã hóa
    console.log(message);
    6
  • var crypto = require('crypto-js');
    
    // Mã hóa
    var message = crypto.AES.encrypt('Nội dung cần mã hóa', 'itsasecret123').toString();
    
    // Xem chuỗi đã mã hóa
    console.log(message);
    7
  • var crypto = require('crypto-js');
    
    // Mã hóa
    var message = crypto.AES.encrypt('Nội dung cần mã hóa', 'itsasecret123').toString();
    
    // Xem chuỗi đã mã hóa
    console.log(message);
    8
  • var crypto = require('crypto-js');
    
    // Mã hóa
    var message = crypto.AES.encrypt('Nội dung cần mã hóa', 'itsasecret123').toString();
    
    // Xem chuỗi đã mã hóa
    console.log(message);
    9
  • var crypto = require('crypto-js');
    
    // Mã hóa
    var message = crypto.AES.encrypt('Noi dung can ma hoa', 'ma bi mat').toString();
    
    // Xem chuỗi đã mã hóa
    console.log(message);
    
    // Lấy danh sách byte đã mã hóa
    var bytes = crypto.AES.decrypt(message, 'ma bi mat');
    
    // Chuyển sang chuỗi gốc
    var message_decode = bytes.toString(crypto.enc.Utf8);
    
    console.log(message_decode);
    0
  • var crypto = require('crypto-js');
    
    // Mã hóa
    var message = crypto.AES.encrypt('Noi dung can ma hoa', 'ma bi mat').toString();
    
    // Xem chuỗi đã mã hóa
    console.log(message);
    
    // Lấy danh sách byte đã mã hóa
    var bytes = crypto.AES.decrypt(message, 'ma bi mat');
    
    // Chuyển sang chuỗi gốc
    var message_decode = bytes.toString(crypto.enc.Utf8);
    
    console.log(message_decode);
    1
  • var crypto = require('crypto-js');
    
    // Mã hóa
    var message = crypto.AES.encrypt('Noi dung can ma hoa', 'ma bi mat').toString();
    
    // Xem chuỗi đã mã hóa
    console.log(message);
    
    // Lấy danh sách byte đã mã hóa
    var bytes = crypto.AES.decrypt(message, 'ma bi mat');
    
    // Chuyển sang chuỗi gốc
    var message_decode = bytes.toString(crypto.enc.Utf8);
    
    console.log(message_decode);
    2
  • var crypto = require('crypto-js');
    
    // Mã hóa
    var message = crypto.AES.encrypt('Noi dung can ma hoa', 'ma bi mat').toString();
    
    // Xem chuỗi đã mã hóa
    console.log(message);
    
    // Lấy danh sách byte đã mã hóa
    var bytes = crypto.AES.decrypt(message, 'ma bi mat');
    
    // Chuyển sang chuỗi gốc
    var message_decode = bytes.toString(crypto.enc.Utf8);
    
    console.log(message_decode);
    3
  • var crypto = require('crypto-js');
    
    // Mã hóa
    var message = crypto.AES.encrypt('Noi dung can ma hoa', 'ma bi mat').toString();
    
    // Xem chuỗi đã mã hóa
    console.log(message);
    
    // Lấy danh sách byte đã mã hóa
    var bytes = crypto.AES.decrypt(message, 'ma bi mat');
    
    // Chuyển sang chuỗi gốc
    var message_decode = bytes.toString(crypto.enc.Utf8);
    
    console.log(message_decode);
    4
  • var crypto = require('crypto-js');
    
    // Mã hóa
    var message = crypto.AES.encrypt('Noi dung can ma hoa', 'ma bi mat').toString();
    
    // Xem chuỗi đã mã hóa
    console.log(message);
    
    // Lấy danh sách byte đã mã hóa
    var bytes = crypto.AES.decrypt(message, 'ma bi mat');
    
    // Chuyển sang chuỗi gốc
    var message_decode = bytes.toString(crypto.enc.Utf8);
    
    console.log(message_decode);
    5
  • var crypto = require('crypto-js');
    
    // Mã hóa
    var message = crypto.AES.encrypt('Noi dung can ma hoa', 'ma bi mat').toString();
    
    // Xem chuỗi đã mã hóa
    console.log(message);
    
    // Lấy danh sách byte đã mã hóa
    var bytes = crypto.AES.decrypt(message, 'ma bi mat');
    
    // Chuyển sang chuỗi gốc
    var message_decode = bytes.toString(crypto.enc.Utf8);
    
    console.log(message_decode);
    6
  • var crypto = require('crypto-js');
    
    // Mã hóa
    var message = crypto.AES.encrypt('Noi dung can ma hoa', 'ma bi mat').toString();
    
    // Xem chuỗi đã mã hóa
    console.log(message);
    
    // Lấy danh sách byte đã mã hóa
    var bytes = crypto.AES.decrypt(message, 'ma bi mat');
    
    // Chuyển sang chuỗi gốc
    var message_decode = bytes.toString(crypto.enc.Utf8);
    
    console.log(message_decode);
    7
  • var crypto = require('crypto-js');
    
    // Mã hóa
    var message = crypto.AES.encrypt('Noi dung can ma hoa', 'ma bi mat').toString();
    
    // Xem chuỗi đã mã hóa
    console.log(message);
    
    // Lấy danh sách byte đã mã hóa
    var bytes = crypto.AES.decrypt(message, 'ma bi mat');
    
    // Chuyển sang chuỗi gốc
    var message_decode = bytes.toString(crypto.enc.Utf8);
    
    console.log(message_decode);
    8

Trong quá trình lập trình, chúng ta sẽ xem xét các mô-đun này, vì tạm thời chúng ta sẽ không tìm hiểu

4. Lời kết

Như vậy trong NodeJS có cung cấp cho chúng tôi một mô-đun mã hóa chuỗi rất là tuyệt vời phải không các bạn, hy vọng tới bài thứ 12 này các bạn sẽ yêu thích NodeJS nhiều hơn nữa. Bài này mình sẽ dừng ở đây, bài tiếp theo chúng ta cùng tìm hiểu về cách quản trị lỗi trong NodeJS