Common encryption algorithms are basically divided into these categories: 1: linear hash algorithm, 2: symmetric encryption algorithm, 3. Asymmetric encryption algorithm (record)
Linear hash algorithm (signature algorithm): MD5, SHA1, HMAC
For example, MD5: Message-Digest Algorithm 5 (Information-Digest Algorithm 5), is used to ensure complete and consistent information transmission.
Features:
1. Compressibility: The calculated MD5 value length is fixed for data of any length.
2. Easy to calculate: It is easy to calculate the MD5 value from the original data.
3. Modification resistance: Make any changes to the original data, even if only 1 byte is modified, the resulting MD5 values are very different.
4. Strong anti-collision: It is known that the original data and its MD5 value are very difficult to find a data with the same MD5 value (i.e., forged data).
The function of MD5 is to allow large-capacity information to be "compressed" into a confidential format before signing a private key with digital signature software (that is, converting a byte string of any length into a hexadecimal string of a certain length)
Symmetric encryption algorithm: AES, DES, 3DES
For example, AES: (Advanced Encryption Standard) is also known as Rijndael encryption method in cryptography, which is a block encryption standard adopted by the US federal government. This standard is used to replace the original DES, and has been analyzed by multiple parties and is widely used worldwide.
Asymmetric encryption algorithm: RSA, DSA, ECC
For example, RSA:RSA public key cryptography system. The so-called public key cryptography system is to use different encryption keys and decryption keys. It is a cryptography system that "deriving decryption keys from known encryption keys is computationally unfeasible."
In the public key cryptography system, the encryption key (i.e., public key) PK is public information, while the decryption key (i.e., secret key) SK needs to be kept confidential. The encryption algorithm E and the decryption algorithm D are also public. Although the decryption key SK is determined by the public key PK, SK cannot be calculated based on the PK.
Crypto module in NodeJS
node uses the OpenSSL library to implement 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 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 cipher = cipher.update(plaintext, 'utf8', 'base64'); cipher += cipher.final('base64'); return cipher; }, 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 method
//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 result:
I+qwOsXQvBq18KVmX3ainoMHbs3nT+v64s
/upload/image/201602120012.jpg
The simple implementation of Node.js DES encryption above is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.