Đô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 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ỗiTrong 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 CryptoJSvar 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 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 3. Danh sách các loại mã hóa của CryptoTrong 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); 4var 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); 5var 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); 7var 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); 8bower install crypto-js 0bower install crypto-js 1bower install crypto-js 2bower install crypto-js 3bower install crypto-js 4bower install crypto-js 5
bower install crypto-js 6bower install crypto-js 7bower install crypto-js 8bower install crypto-js 9var crypto = require('crypto-js'); 0var crypto = require('crypto-js'); 1var crypto = require('crypto-js'); 2var crypto = require('crypto-js'); 3
var crypto = require('crypto-js'); 5var crypto = require('crypto-js'); 6var crypto = require('crypto-js'); 7var crypto = require('crypto-js'); 8var crypto = require('crypto-js'); 9var 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); 1var 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); 3var 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); 4var 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); 5var 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); 6var 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); 8var 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); 9var 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); 0var 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); 1var 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); 3var 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); 4var 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); 5var 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); 6var 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); 7var 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ếtNhư 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 |