Nodejs's crypto module public key encryption and decryption

Source: Internet
Author: User
Tags decrypt

Nodejs's crypto module public key encryption and decryption

Nodejs has the following 4 classes related to public key cryptography.
1. Cipher: Used for encrypting data;
2. Decipher: Used to decrypt data;
3. Sign: Used to generate signatures;
4. Verify: Used to verify the signature;

When using public key cryptography, public and private keys are used to create encrypted data that only the owner of the private key can read, and to authenticate the signature of the owner of the private key, which is used to decrypt the data and to sign the data.

One: Encrypt data
In the crypto module, the cipher class is used to encrypt data, and we can create cipher objects in the following two ways;

1.1 Createcipher Method that uses the specified algorithm and password to create the Cipher object. This method is used in the following ways:

Crypto.createcipher (params, pasword);

The method uses two parameters, the first parameter specifies the algorithm used to encrypt the data, such as ' ASE-256-CBC ', and the second parameter specifies the password to use when encrypting, which must be a binary-formatted string or a buffer object.

The Createcipher method returns a Cipher object that was created.

1.2 Createcipheriv method , which uses the specified algorithm, password, and initial vector to create the cipher object, using the method as follows:

Crypto.createcipheriv (params, password, iv);

The first parameter of the method is the algorithm used to encrypt the data, such as ' ASE-256-CBC ', and so on, and the second parameter specifies the password to use when encrypting, which must be a binary-formatted string or a buffer object. The third parameter specifies the initial vector to use when encrypting, and the parameter value must also be a binary-formatted string or a buffer object.
The method returns a Cipher object that is also created.

1.3 After you have created the cipher object in two ways, you can specify the data that needs to be encrypted by using the object's Update method, which is used in the following ways:

Cipher.update (data, [input_encoding], [output_encoding])

The first parameter is a required option (the other parameter is optional), which is a buffer object or a string that specifies the data that needs to be encrypted. The second parameter specifies the encoding format used for the encrypted data, specifying that the parameter value is ' Utf-8 ', ' ASCII ' or ' binary '. If you do not use the second argument, then the first argument must be a buffer object.
The third parameter specifies the encoding format to use when outputting encrypted data, and can specify a parameter value of ' hex ', ' binary ' or ' base64 '. If the third argument is not used, the method returns a buffer object that holds the encrypted data.

1.4 You can use the final method of the Cipher object to return the encrypted data. When the method is called, the cached data in any cipher object is encrypted. The final method in which the Cipher object is used
, you can no longer append encrypted data to the cipher object, which is used in the following way:

Cipher.final ([output_encoding]);

The method uses an optional parameter, which is a string that specifies the encoding format of the output encrypted data, specifying the parameter value ' hex ', ' binary ', and ' base64 '. If you use the
This parameter, the final method returns the encrypted data in string format, and if the parameter is not used, the method returns a buffer object.

The specific code used is as follows:

Const CRYPTO = require (' crypto ');/*here is the use of encryption algorithm to ' I am not a fool ' several words encrypted, the password used is 123456*/Const Data= ' I'm not an idiot '; Const password= ' 123456 ';//Creating an encryption algorithmConst ASEENCODE =function(data, password) {//use the specified algorithm and password to create the Cipher object using the following methodConst CIPHER = crypto.createcipher (' aes192 ', password); //use the Update method of this object to specify the data that needs to be encryptedLet crypted = cipher.update (data, ' utf-8 ', ' hex ')); Crypted+ = cipher.final (' hex ')); returncrypted;}; Console.log (aseencode (data, password)); //Output ebdf98c254b9aa5265f6d4a5e73f861d

Second: Decrypt the data

In the crypto module, the Decipher class is used to decrypt encrypted data.
Create a Decipher object that can be created in the following two ways:

The 2.1 Createdecipher method uses the specified algorithm and password to create the Decipher object, which is used in the following ways:

Crypto.createdecipher (params, password);

In this method, the first parameter specifies the algorithm used to decrypt the data, such as ' AES-256-CBC ', which must be consistent with the algorithm used to encrypt the data.
The second parameter specifies the password to use when decrypting, the parameter value is a binary-formatted string or a buffer object, and the password must be consistent with the password used to encrypt the data.

The Createdecipher method returns a Decipher object that was created.

2.2 Createdecipheriv method ; The method creates a decipher object using the specified algorithm, password, and initial vector. The method is used as follows:

Crypto.createdecipheriv (params, password, iv);

In this method, the first parameter specifies the algorithm used to decrypt the data, such as ' AES-256-CBC ', which must be consistent with the algorithm used to encrypt the data. The second parameter specifies the password to use when decrypting, the parameter value must be a binary-formatted string or a buffer object, and the password must be consistent with the password used to encrypt the data.
The third parameter specifies the initial vector to use when decrypting, which must also be a binary-formatted string or a buffer object that must be consistent with the initial vector used to encrypt the data.

2.3 After you have created the Decipher object, you can specify the data that needs to be decrypted by using the object's Update method. The method is used as follows:

Decipher.update (data, [input_encoding], [output_encoding]);

In this method, the first parameter is a buffer object or a string that specifies the data that needs to be decrypted, the second parameter specifies the encoding format used for the decrypted data, the specified parameter value is ' hex ', ' binary ', ' base64 ', and so on, if the second argument is not used , the parameter value must be a buffer object. The third parameter specifies the encoding format to use when outputting the decrypted data, and the optional parameter value is ' Utf-8 ', ' ASCII ' or ' binary ';

2.4 You can use the final method of the Decipher object to return the decrypted raw data, which is used as follows:

Decipher.final ([output_encoding]);

In the final method of the object, an optional parameter is used, the parameter value is a string that specifies the encoding format to use when outputting the decrypted data, specifying the parameter value as ' Utf-8 ', ' ASCII ' or ' binary ';
If this parameter is used, final returns the decrypted data in string format, and if the parameter is not used, the final method returns a buffer object.

The following code decrypts the data:

Const CRYPTO = require (' crypto ');/*here is the use of encryption algorithm to ' I am not a fool ' several words encrypted, the password used is 123456*/Const Data= ' I'm not an idiot '; Const password= ' 123456 ';//Creating an encryption algorithmConst ASEENCODE =function(data, password) {//use the specified algorithm and password to create the Cipher object using the following methodConst CIPHER = crypto.createcipher (' aes192 ', password); //use the Update method of this object to specify the data that needs to be encryptedLet crypted = cipher.update (data, ' utf-8 ', ' hex ')); Crypted+ = cipher.final (' hex ')); returncrypted;}; Console.log (aseencode (data, password)); //Output ebdf98c254b9aa5265f6d4a5e73f861d//creating a decryption algorithmConst ASEDECODE =function(data, password) {/*The method uses the specified algorithm and password to create a Decipher object, and the first algorithm must be consistent with the algorithm used to encrypt the data; The second parameter specifies the password to use when decrypting, the parameter value is a binary-formatted string or a buffer object, and the password must be consistent with the password used to encrypt the data*/Const Decipher= Crypto.createdecipher (' aes192 ', password); /*The first parameter is a buffer object or a string that specifies the data that needs to be decrypted the second parameter is used to specify the encoding format used for the decrypted data, the specified parameter value is ' hex ', ' binary ', ' base64 ', etc., and the third parameter is used to specify the output to decrypt the data  When using the encoding format, the optional parameter value is ' Utf-8 ', ' ASCII ' or ' binary '; */Let decrypted= decipher.update (data, ' hex ', ' utf-8 ')); Decrypted+ = decipher.final (' Utf-8 '); returndecrypted;}; Console.log (Asedecode (aseencode (data, password), password)); //output I'm not an idiot .

Nodejs's crypto module public key encryption and decryption

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.