Hướng dẫn encode nodejs - mã hóa nodejs

Nội dung bài viết

Video học lập trình mỗi ngày

Decode và encode là một trong những cách thường được sử dụng để tránh các cuộc tấn công tập lệnh trên nhiều trang web (XSS) bằng cách mã hóa các ký tự đặc biệt trong một URL. Bài viết này cung cấp cách triển khai cũng như giải thích rõ ràng hơn. Sử dụng encode chuyển đổi một chuỗi thành một định dạng URL hợp lệ để làm cho dữ liệu được truyền đi đáng tin cậy và an toàn hơn. Và sử dụng

const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
0 ngược lại với quá trình mã hóa. Nó chuyển đổi các chuỗi URL được mã hóa và các tham số truy vấn trở lại định dạng bình thường của chúng.

Trong bài viết này, bạn sẽ học cách encode hoặc

const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
0 String URL và
const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
3 trong ứng dụng Node.js.

Xem thêm: Mẹo viết code javascript và nodejs

URL encode

Trong Node.js xây dựng trên công cụ JavaScript V8 của Chrome hỗ trợ hai phương pháp encode đó là

const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
5 và
const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
6 để mã hoá một URL. Vậy nó dùng như thế nào?

encodeURI javascript

Nếu bạn muốn mã hoá một URL hoàn chỉnh thì có thể sử dụng

const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
5, phương thức này không hỗ trợ các ký tự sau:
const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
8. Ví dụ sau cho thấy điều đó:

const url = 'http://example.com/!Learn Node$/ Example';

// Encode complete URL
const encodedUrl = encodeURI(url);

// Print encoded URL
console.log(encodedUrl);

// http://example.com/!Learn%20Node$/%20Example

encodeURIComponent

Nếu như dùng

const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
5 để mã hoá một URL thì việc sử dụng
const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
6 thì chỉ mã hoá
const querystring = require('querystring');

const baseUrl = 'http://example.com/search?';
const query = 'SELECT * from users WHERE id = 1'

// Encode query string
const encodedQuery = querystring.stringify({ q: query });

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
1 hay còn gọi là tham số chuỗi truy vấn chứ không phải một URL hoàn chỉnh. Xem tiếp ví dụ sau:

const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201

Ngoài phương pháp

const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
6 thì chúng tôi cung câp thêm cho bạn một module tương tự nhưng nó được tích hợp sẵn trong Node.js để mã hoá URL đó là
const querystring = require('querystring');

const baseUrl = 'http://example.com/search?';
const query = 'SELECT * from users WHERE id = 1'

// Encode query string
const encodedQuery = querystring.stringify({ q: query });

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
3.

querystring module

Bạn cũng có thể sử dụng

const querystring = require('querystring');

const baseUrl = 'http://example.com/search?';
const query = 'SELECT * from users WHERE id = 1'

// Encode query string
const encodedQuery = querystring.stringify({ q: query });

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
3 được tích hợp Node.js để mã hóa URL. Module này cung cấp các phương thức tiện ích để phân tích cú pháp và định dạng chuỗi truy vấn URL:

const querystring = require('querystring');

const baseUrl = 'http://example.com/search?';
const query = 'SELECT * from users WHERE id = 1'

// Encode query string
const encodedQuery = querystring.stringify({ q: query });

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201

decode URL

Trên đó là việc hướng dẫn cũng như giải thích về việc mã hoá

const querystring = require('querystring');

const baseUrl = 'http://example.com/search?';
const query = 'SELECT * from users WHERE id = 1'

// Encode query string
const encodedQuery = querystring.stringify({ q: query });

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
5. Đương nhiên, khi mã hoá thì phải có giải mã, tương tự khi encode xong thi phải decode.

Tương tự thôi

const querystring = require('querystring');

const baseUrl = 'http://example.com/search?';
const query = 'SELECT * from users WHERE id = 1'

// Encode query string
const encodedQuery = querystring.stringify({ q: query });

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
6 cũng cung cấp cho hai methods đó là
const querystring = require('querystring');

const baseUrl = 'http://example.com/search?';
const query = 'SELECT * from users WHERE id = 1'

// Encode query string
const encodedQuery = querystring.stringify({ q: query });

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
7 và
const querystring = require('querystring');

const baseUrl = 'http://example.com/search?';
const query = 'SELECT * from users WHERE id = 1'

// Encode query string
const encodedQuery = querystring.stringify({ q: query });

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
8 để thực hiện giải mã URL.

decodeURI javascript

Phương pháp

const querystring = require('querystring');

const baseUrl = 'http://example.com/search?';
const query = 'SELECT * from users WHERE id = 1'

// Encode query string
const encodedQuery = querystring.stringify({ q: query });

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
7 được sử dụng để giải mã một URL đầy đủ:

const encodedUrl = 'http://example.com/!Learn%20Node$/%20Example';

// Decode URL
const url = decodeURI(encodedUrl);

// Print URL
console.log(url);

// http://example.com/!Learn Node$/ Example

decodeURIComponent

Chức năng

const querystring = require('querystring');

const baseUrl = 'http://example.com/search?';
const query = 'SELECT * from users WHERE id = 1'

// Encode query string
const encodedQuery = querystring.stringify({ q: query });

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
8 được sử dụng để các thành phần URL giải mã:

try {
  var a = decodeURIComponent('%E0%A4%A');
} catch(e) {
  console.error(e);
}

// URIError: malformed URI sequence

Lời kết

Bài viết đã quá rõ ràng cho việc sử dụng

const encodedUrl = 'http://example.com/!Learn%20Node$/%20Example';

// Decode URL
const url = decodeURI(encodedUrl);

// Print URL
console.log(url);

// http://example.com/!Learn Node$/ Example
1 rồi, cho nên việc tóm tắt là việc làm dư thừa. Nhưng các bạn cũng nên để ý việc khác nhau giữa
const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
5 và
const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
6, cũng như
const querystring = require('querystring');

const baseUrl = 'http://example.com/search?';
const query = 'SELECT * from users WHERE id = 1'

// Encode query string
const encodedQuery = querystring.stringify({ q: query });

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
7 và
const querystring = require('querystring');

const baseUrl = 'http://example.com/search?';
const query = 'SELECT * from users WHERE id = 1'

// Encode query string
const encodedQuery = querystring.stringify({ q: query });

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
8 trong Node.js. Tránh nhầm lẫn để sử dụng không đúng mục đích của việc làm của mình.

Xem thêm: Mẹo viết code javascript và nodejs

URL encode

Code ví dụ Node.js Crypt3 – encode/decode

Module crypt3

(Lưu ý, module crypt3 không chạy trên windows)

Crypt3 mà module dùng để mã hóa và giải mã với crypt3

Để cài đặt module formidable ta dùng lệnh:

npm install crypt3

Để include module formidable vào project ta dùng method:

var crypt = require('crypt3/sync');

Mã hóa và giải mã với module crypt3

Mã hóa bằng crypt3:

Để mã hóa ta dùng hàm

const encodedUrl = 'http://example.com/!Learn%20Node$/%20Example';

// Decode URL
const url = decodeURI(encodedUrl);

// Print URL
console.log(url);

// http://example.com/!Learn Node$/ Example
6

  • key là chuỗi cần mã hóa
  • salt là 2 ký tự dùng để làm nhiễu

Ví dụ:

var crypt = require('crypt3/sync');
console.log( crypt('1234', 'ab') );

Demo:

Hướng dẫn encode nodejs - mã hóa nodejs

Crypt3 mã hóa một chiều, nên ta thường chỉ decode khi biết salt là gì, và số lượng ký tự mã hóa là bao nhiêu.

Ví dụ mình biết rằng salt là

const encodedUrl = 'http://example.com/!Learn%20Node$/%20Example';

// Decode URL
const url = decodeURI(encodedUrl);

// Print URL
console.log(url);

// http://example.com/!Learn Node$/ Example
7 và data mã hóa là số có 4 chữ số:

var crypt = require('crypt3/sync');

var data = 'lf1i/7N/k5d7Q';

for (var i = 0; i <= 9999; i++){
  if (data == crypt(`${i}`, 'lf')){
    console.log(i);
  }
}

Demo

Hướng dẫn encode nodejs - mã hóa nodejs

Okay, Done!

Download code ví dụ trên tại đây.

References: https://github.com/sendanor/node-crypt3