Simple implementation of Node. js DES encryption

  • 2021-07-02 23:02:38
  • OfStack

Common encryption algorithms are basically divided into these categories: 1: linear hash algorithm, 2: symmetric encryption algorithm, 3, asymmetric encryption algorithm (remember)

Linear hash algorithm (signature algorithm): MD5, SHA1, HMAC

For example, MD5: Message-Digest Algorithm 5 (information-summary algorithm 5) is used to ensure the integrity of information transmission.

Features:

1. Compressibility: For data of any length, the calculated length of MD5 value is fixed.
2. Easy to calculate: It is easy to calculate MD5 value from the original data.
3. Modification resistance: Any change to the original data, even if only one byte is modified, the obtained MD5 value is quite different.
4. Strong anti-collision: Given the original data and its MD5 value, it is very difficult to find a data with the same MD5 value (i.e., forged data).

The function of MD5 is to "compress" a large amount of information into a secure format before signing the private key with digital signature software (that is, to transform a byte string of arbitrary length into a 106-digit string of fixed length)

Symmetric encryption algorithms: AES, DES, 3DES

For example, AES: (Advanced Encryption Standard), also known as Rijndael encryption in cryptography, is a block encryption standard adopted by the US federal government. This standard is used to replace the original DES, which has been analyzed by many parties and widely used all over the world.

Asymmetric encryption algorithms: RSA, DSA, ECC

For example, RSA: RSA public key cryptosystem. The so-called public key cryptosystem uses different encryption keys and decryption keys, which is a cryptosystem that "it is computationally infeasible to deduce decryption keys from known encryption keys".
In the public key cryptosystem, the encryption key (i.e. public key) PK is public information, while the decryption key (i.e. secret key) SK needs to be kept secret. The encryption algorithm E and the decryption algorithm D are also disclosed. Although the decryption key SK is determined by the public key PK, SK cannot be calculated from PK.

Crypto Module in NodeJS

node uses OpenSSL library to realize its encryption technology, because OpenSSL is already a widely used encryption algorithm. It includes algorithms like MD5 or SHA-1 that you can use in your application.

The following code uses the implementation method of DES algorithm of Crypto module


/***
 * @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;
  }

};

Encryption and decryption method using DES


// Encryption 
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:

I+qwOsXQvBq18KVmX3ainoMHbs3nT+v64s

/upload/image/201602120012.jpg


Related articles: