Nodejs encryption Algorithm (RPM)

Source: Internet
Author: User
Tags cast5 decrypt hmac sha1 sha1 encryption asymmetric encryption

1, hash Algorithm encryption:

To create a Nodejs file Hash.js, enter the following:

1 var crypto = require (' crypto ');  Load Crypto Library 2 Console.log (Crypto.gethashes ()); Printing supported hash algorithms

The results are as follows:

1 [' DSA ', 2   ' Dsa-sha ', 3   ' Dsa-sha1 ', 4   ' Dsa-sha1-old ', 5   ' RSA-MD4 ', 6   ' Rsa-md5 ', 7   ' RSA-MDC2 ' , 8   ' rsa-ripemd160 ', 9   ' Rsa-sha ', ten   ' Rsa-sha1 ', one   ' rsa-sha1-2 ',   ' rsa-sha224 ',   ' rsa-sha256 ', ' rsa-sha384 ', ' rsa-sha512 ', '   dsaencryption ', ' Dsawithsha '   ,   ' DsaWithSHA1 ', '   dss1 ', ' ecdsa-with-sha1 ', ' MD4 ', '   md4withrsaencryption '   ,   ' MD5 ', ' md5withrsaencryption ', ' mdc2 ', ' mdc2withrsa ', ' ripemd ', '   ripemd160 ',   ' Ripemd160withrsa ', '   rmd160 ', '   sha ', '   sha1 ', ' sha1withrsaencryption '   , 34   ' sha224 ', '   sha224withrsaencryption ', ' sha256 ',   notoginseng '   sha256withrsaencryption ',   ' sha384 ', ' sha384withrsaencryption ', ' sha512 ', '   sha512withrsaencryption '   , '   Shawithrsaencryption ', ' ssl2-md5 ', ' ssl3-md5 ', ' ssl3-sha1 ', '   Whirlpool '

Used in Nodejs: (' MD5 ' in CreateHash (' MD5 ') can be replaced by the hash encryption method required for the attack)

1     var content = ' password ';//encrypted plaintext, 2     var md5 = crypto.createhash (' MD5 ');//define encryption Method: MD5 Irreversible, The MD5 here can be replaced by any hash encryption method name, 3     md5.update (content), 4     var d = md5.digest (' hex ');  The encrypted value D5     console.log ("Encrypted result:" +d);
2, HMAC algorithm encryption: (mainly used)

HMAC is a key-related hash operation message authentication code (hash-based message Authentication code), the HMAC operation takes advantage of a hashing algorithm, takes a key and a message as input, generates a message digest as output. HMAC can effectively prevent some similar MD5 such as rainbow table attacks, such as some common passwords directly MD5 into the database, may be reverse-cracked.

Defining an HMAC requires a cryptographic hash function (denoted as h, which can be MD5 or SHA-1) and a key K. We use B to represent the number of bytes in the data block. (The partition data block of the hash function mentioned above is b=64), and L is used to represent the output data bytes of the hash function (l=20 in L=16,sha-1 in MD5). The length of the authentication key can be any positive integer value that is less than or equal to the word size of the data block. If the key length used in the application is larger than B, it is first used with the hash function h and then the L-length string with the H output as the actual key used in the HMAC. In general, the recommended minimum key k length is l bytes.

The following is the HMAC encryption I used in my project (in SHA1 mode): (Since the key will be encrypted by randomly generated 16-bit number, and then encrypt the plaintext, each generation of the new key (token) is different, so the final generated ciphertext will not be the same, this encryption is not reversible,

Even in the MD5 way, it is difficult to attack through the rainbow table)

/********HMAC-SHA1 encryption ***************/    var content = ' password ';//encrypted plaintext;    var token1= ' Miyue ';//cryptographic key;    var BUF = crypto.randombytes (+);    Token1 = buf.tostring (' hex ');//key encryption;        Console.log ("generated token (for encrypted key):" +token1);    var secrectkey=token1;//secret key;    var signture = Crypto.createhmac (' SHA1 ', secrectkey);//define Encryption mode    Signture.update ( content);    var miwen=signture.digest (). toString (' base64 ');//generated ciphertext will be re-encrypted as plaintext again via PBKDF2 algorithm;    console.log ("Encrypted result F:" +miwen) /********** the corresponding result (the result of each generation is different) ******************/generated tokens (for encrypted keys): Results of De7c3dafede518a1ad9c2096ee9b4eff Encryption f:pux7fnomlqvj+bs9o6rnngxfffy=

Generated tokens (keys for encryption): 93FEE046EBF47412C2D54C1E808218D2
The result of the encryption f:/erkucrjkwxzgxnm7wczu8rax5o=

3, symmetric encryption and asymmetric encryption:

    • The principle of symmetric encryption algorithm is easy to understand, the communication side with KEK encryption plaintext, the other party received after the same key to decrypt the plaintext can be obtained.
    • Asymmetric encryption algorithm that uses two pairs of keys that are completely different but perfectly matched: the public key and the private key. When encrypting a file using an asymmetric encryption algorithm, the process of encrypting and decrypting the plaintext is accomplished only by using a matching pair of public and private keys.

3.1: Symmetric encryption:

To create a Nodejs file Cipher.js, enter the following:

var crypto = require (' crypto ');  Load Crypto library Console.log (Crypto.getciphers ()); Cipher Algorithms for printing support
The results are as follows:
  1 [' CAST-CBC ', 2 ' AES-128-CBC ', 3 ' AES-128-CFB ', 4 ' aes-128-cfb1 ', 5 ' aes-128-cfb8 ', 6 ' aes-128-ctr ', 7   ' AES-128-ECB ', 8 ' aes-128-gcm ', 9 ' aes-128-ofb ', ' aes-128-xts ', one ' AES-192-CBC ', ' AES-192-CFB ', 13   ' Aes-192-cfb1 ', ' aes-192-cfb8 ', ' aes-192-ctr ', ' AES-192-ECB ', ' aes-192-gcm ', ' aes-192-ofb ', 19   ' AES-256-CBC ', ' aes-256-cfb ', ' aes-256-cfb1 ', ' aes-256-cfb8 ', ' aes-256-ctr ', ' AES-256-ECB ', 25 ' Aes-256-gcm ', ' aes-256-ofb ', ' aes-256-xts ', ' aes128 ', ' aes192 ', ' aes256 ', ' bf ', ' bf-c '   BC ', ' BF-CFB ', ' BF-ECB ', ' bf-ofb ', ' blowfish ', Notoginseng ' CAMELLIA-128-CBC ', ' CAMELLIA-128-CFB ', 39 ' Camellia-128-cfb1 ', ' camellia-128-cfb8 ', ' CAMELLIA-128-ECB ', ' camellia-128-ofb ', ' CAMELLIA-192-CBC ', CAMELLIA-192-CFB ', ' camellia-192-cfb1 ', ' camellia-192-cfb8 ', ' CAMELLIA-192-ECB ', ' Camellia-192-o ' FB ', camellia-256-'CBC ', ' CAMELLIA-256-CFB ', Wuyi ' camellia-256-cfb1 ', ' camellia-256-cfb8 ', ' CAMELLIA-256-ECB ', ' Camellia '   -256-ofb ', ' camellia128 ', ' camellia192 ', ' camellia256 ', ' cast ', ' CAST-CBC ', ' CAST5-CBC ', 61 ' CAST5-CFB ', ' CAST5-ECB ', ' cast5-ofb ', ' des ', ' des-cbc ', ' des-cfb ', ' des-cfb1 ' des-' Cfb8 ', ' DES-ECB ', ' des-ede ', ' DES-EDE-CBC ', ' DES-EDE-CFB ', the ' des-ede-ofb ', the ' des-ede3 ', 75 '   DES-EDE3-CBC ', Des-ede3-cfb ', ' des-ede3-cfb1 ', ' des-ede3-cfb8 ', ' des-ede3-ofb ', ' des-ofb ', 81   ' Des3 ', ' desx ', ' DESX-CBC ', ' id-aes128-gcm ', ' id-aes192-gcm ', ' id-aes256-gcm ', ' idea ', 88 ' IDEA-CBC ', ' idea-cfb ', ' IDEA-ECB ', ' idea-ofb ', ' RC2 ', ' RC2-40-CBC ', 94 ' RC2-64-CBC ', ' RC2 ' -CBC ', ' rc2-cfb ', ' RC2-ECB ', 98 ' rc2-ofb ', ' RC4 ', ' rc4-40 ', 101 ' rc4-hmac-md5 ', 102 ' seed ', 103 ' SEED-CBC ', 104 ' SEED-CFB ', Seed-ecb ', 106 ' SEED-OFB '] 

Here is the symmetric encryption algorithm I used in my project:

Configure routing in App.js. var jiami=require ("./routes/jiami"), ... app.use ("/jiami", Jiami);
Request the node-side route in the service:
1 $http. Post ("/app/jiami/encrypt", {str:JSON.stringify (user)}). Success (function (Miwen) {2/// Console.log ( "Returned ciphertext:" +miwen); 3 }). Error (function (Error) {4 return Deferred.reject (Error); 5 });
1 var secret= ' pass ';//Key 2  3//Encryption 4 Router.post ("/encrypt", function (req,res) {5     var str=req.body.str;//clear Text 6     var cipher = crypto.createcipher (' aes192 ', secret); 7     var enc = cipher.update (str, ' UTF8 ', ' hex ');// The encoding is converted from utf-8 to hex; 8     Enc + = cipher.final (' hex ');//Encode from hex, 9     res.send (ENC), 10}), 11//Decrypt Router.post ("/decrypt", function ( Req,res) {$     var str=req.body.str;//plaintext     var decipher = crypto.createdecipher (' aes192 ', secret);     DEC = decipher.update (str, ' hex ', ' UTF8 ');//encoding from hex to utf-8;16     Dec + = decipher.final (' UTF8 ');//encoding from UTF-8;17     res.send (DEC); 18});

3.2, Asymmetric encryption: This method is still useless, wait for the back to continue to add.

Nodejs encryption Algorithm (RPM)

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.