Simple implementation of Node.js des encryption _node.js

Source: Internet
Author: User
Tags base64 decrypt md5 openssl openssl library asymmetric encryption

Common encryption algorithms are basically divided into these categories, 1: Linear hashing algorithm, 2: Symmetric encryption algorithm, 3, asymmetric encryption Algorithm (remember)

Linear hashing algorithm (Signature algorithm): Md5,sha1,hmac

For example, MD5: Message-digest algorithm 5 (Information-Digest algorithm 5), which is used to ensure the complete consistency of information transmission.

Characteristics:

1, compressibility: Arbitrary length of data, calculated MD5 value of the length are fixed.
2, easy to calculate: From the original data calculated MD5 value is very easy.
3, resistance to modification: Any changes to the original data, even if only modify 1 bytes, the resulting MD5 values are very different.
4, strong anti-collision: Known the original data and its MD5 value, it is very difficult to find a data with the same MD5 value (that is, forged data).

The role of MD5 is to allow bulk information to be "compressed" into a confidential format (that is, to transform a byte string of any length into a string of hexadecimal digits) before signing the private key with the digital signature software.

Symmetric encryption algorithm: Aes,des,3des

For example, AES: (Advanced encryption Standard) in cryptography, also known as Rijndael encryption, is the United States federal government used a block encryption standard. This standard, which replaces the original DES, has been analyzed and widely used worldwide.

Asymmetric Encryption algorithm: RSA,DSA,ECC

For example, the Rsa:rsa public key cryptosystem. The so-called public key cryptosystem is the use of different encryption keys and decryption keys, is a "derived from the known encryption key decryption key is computationally infeasible" cryptosystem.
In public key cryptography, the encryption key (that is, public key) PK is public information, and the decryption key (that is, secret key) SK needs to be kept confidential. Encryption algorithm E and decryption algorithm d are also public. Although the decryption key SK is determined by public key PK, but can not calculate the SK according to PK.

The crypto module in Nodejs

Node uses the OpenSSL library to implement its encryption technology because OpenSSL is already a widely used cryptographic algorithm. It includes a similar MD5 or SHA-1 algorithm that you can use in your application.

The following code uses the Crypto Module DES algorithm implementation method

/***
 * @author Chenjianxiang
 * @date 2016-07-07

/var crypto = require (' crypto ');
var key = ' 12345670 ';
Exports.des = {

  algorithm:{ECB: ' DES-ECB ', CBC: ' DES-CBC '},
  encrypt:function (plaintext,iv) {
    var key = new Buffer (key);
    var IV = new Buffer (iv iv:0);
    var cipher = Crypto.createcipheriv (THIS.ALGORITHM.ECB, key, iv);
    Cipher.setautopadding (True)//default true
    var ciph = cipher.update (plaintext, ' UTF8 ', ' base64 ');
    Ciph + = cipher.final (' base64 ');
    Return Ciph
  },
  decrypt:function (encrypt_text,iv) {
    var key = new Buffer (key);
    var IV = new Buffer (iv iv:0);
    var decipher = Crypto.createdecipheriv (THIS.ALGORITHM.ECB, key, iv);
    Decipher.setautopadding (true);
    var txt = decipher.update (encrypt_text, ' base64 ', ' UTF8 ');
    TXT + + decipher.final (' UTF8 ');
    return txt;
  }

;

Using DES encryption and decryption methods

Encrypt
var cryptutil = require ("./utils/crypt");
var str = "/upload/image/201602120012.jpg";
var encrypt_text = CryptUtil.des.encrypt (str,0);
var decrypt_text = CryptUtil.des.decrypt (encrypt_text,0);
Console.log (encrypt_text);
Console.log (Decrypt_text);

Output results:

i+qwosxqvbq18kvmx3ainomhbs3nt+v64s

/upload/image/201602120012.jpg

Above this article Node.js des encryption simple realization is small arranges to share to everybody's content, hoped can give everybody a reference, also hoped that everybody supports the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.