Object built-in object

ObjectCipher

Symmetric encryption algorithm object

Cipher object belongs tocryptomodule, create:

1
var c = new crypto.Cipher(crypto.AES, crypto.ECB, ...);

The following takes AES as an example to demonstrate how to use Cipher objects for encryption and decryption:

First, we need to obtain a symmetrically encrypted Cipher object, such as the AES encryption algorithm:

1 2 3
const crypto = require('crypto'); const key = crypto.randomBytes(16); // generate a 16-byte random key const cipher = new crypto.Cipher(crypto.AES, crypto.ECB, key);

In the above code, we randomBytesgenerated a 16-byte random number as the AES key through the method. Then, we created a Cipher object of the AES algorithm, using the ECB block cipher working mode, and using the generated random number as the key.

Next, we encryptencrypt the plaintext using the Cipher object's method:

1 2 3
const plaintext = 'Hello, world!'; const encrypted = cipher.encrypt(plaintext); console.log(encrypted); // output encrypted data

In the above code, we use encryptthe method to encrypt the string Hello, world!and output the encrypted result.

Regarding the above code, you need to pay attention to the following points:

  • Before performing encryption, you need to create a symmetrically encrypted Cipher object and set parameters such as encryption algorithm, password, and block cipher working mode.
  • After calling the encryption method, we can get the encrypted data, which is usually aBufferobject. It should be noted that the output is binary data, which needs to be printed in the corresponding method, such as using Hexetc. for conversion.

If we want to decrypt the data, we also need a Cipher object, which is also based on the AES algorithm and uses the same key and block cipher working mode:

1 2 3
const decipher = new crypto.Cipher(crypto.AES, crypto.ECB, key); const decrypted = decipher.decrypt(encrypted); console.log(decrypted.toString()); // output decrypted data

Here, we create a new Cipher object, use the same key and password working mode, and use decryptthe method to decrypt the encrypted data. The result of decryption is aBufferObject needs to be converted to a string to be output correctly.

inheritance relationship

Constructor

Cipher

Cipher constructor, only used for ARC4 initialization

1 2
new Cipher(Integer provider, Buffer key);

Call parameters:

  • provider: Integer, specify the encryption algorithm
  • key:Buffer, specify the encryption and decryption password

Cipher constructor

1 2 3
new Cipher(Integer provider, Integer mode, Buffer key);

Call parameters:

  • provider: Integer, specify the encryption algorithm
  • mode: Integer, specifies the block cipher working mode
  • key:Buffer, specify the encryption and decryption password

Cipher constructor

1 2 3 4
new Cipher(Integer provider, Integer mode, Buffer key, Buffer iv);

Call parameters:

  • provider: Integer, specify the encryption algorithm
  • mode: Integer, specifies the block cipher working mode
  • key:Buffer, specify the encryption and decryption password
  • iv:Buffer, specify the initial vector

member properties

name

String, returns the current algorithm name

1
readonly String Cipher.name;

keySize

Integer, returns the current algorithm password length in bits

1
readonly Integer Cipher.keySize;

ivSize

Integer, returns the current algorithm initial vector length, in bytes

1
readonly Integer Cipher.ivSize;

blockSize

Integer, returns the current algorithm data block length in bytes

1
readonly Integer Cipher.blockSize;

member function

paddingMode

Use fill mode

1
Cipher.paddingMode(Integer mode);

Call parameters:

  • mode: Integer, specifies the padding mode, the default is PADDING_PKCS7

encrypt

Encrypt data using current algorithm cipher

1
Buffer Cipher.encrypt(Buffer data) async;

Call parameters:

  • data:Buffer, specify the data to be encrypted

Return results:

  • Buffer, returns the encrypted data

decrypt

Decrypt data using current algorithm cipher

1
Buffer Cipher.decrypt(Buffer data) async;

Call parameters:

  • data:Buffer, specify the data to be decrypted

Return results:

  • Buffer, returns the decrypted data

toString

Returns the string representation of the object. Generally, "[Native Object]" is returned. The object can be re-implemented according to its own characteristics.

1
String Cipher.toString();

Return results:

  • String, returns the string representation of the object

toJSON

Returns a JSON format representation of the object, generally returning a collection of readable properties defined by the object.

1
Value Cipher.toJSON(String key = "");

Call parameters:

  • key: String, not used

Return results:

  • Value, returns a value containing JSON serializable