ObjectCipher
Symmetric encryption algorithm object
Cipher object belongs tocryptomodule, create:
1var 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
3const 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 randomBytes
generated 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 encrypt
encrypt the plaintext using the Cipher object's method:
1
2
3const plaintext = 'Hello, world!';
const encrypted = cipher.encrypt(plaintext);
console.log(encrypted); // output encrypted data
In the above code, we use encrypt
the 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
Hex
etc. 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
3const 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 decrypt
the 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
2new 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
3new 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
4new 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
1readonly String Cipher.name;
keySize
Integer, returns the current algorithm password length in bits
1readonly Integer Cipher.keySize;
ivSize
Integer, returns the current algorithm initial vector length, in bytes
1readonly Integer Cipher.ivSize;
blockSize
Integer, returns the current algorithm data block length in bytes
1readonly Integer Cipher.blockSize;
member function
paddingMode
Use fill mode
1Cipher.paddingMode(Integer mode);
Call parameters:
- mode: Integer, specifies the padding mode, the default is PADDING_PKCS7
encrypt
Encrypt data using current algorithm cipher
1Buffer 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
1Buffer 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.
1String 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.
1Value Cipher.toJSON(String key = "");
Call parameters:
- key: String, not used
Return results:
- Value, returns a value containing JSON serializable