Node-rsa module encryption has some pitfalls.
Some of the above are described:
RSA is a block encryption algorithm. Therefore, for plaintext, You need to divide them into fixed block lengths. Considering the length of the input data, there are several types of encryption/Decryption padding:
The front-end needs to encrypt the rsa password and then transmit it to the backend. Therefore, a small library needs to be introduced, so node-rsa is used.
Import NodeRSA from 'node-rsa 'RSA _ PUBLIC_KEY = 'public key' function rsaEncrypt (message, key) {let clientKey = new NodeRSA (RSA_PUBLIC_KEY) let encrypted = clientKey. encrypt (message, 'base64') return encrypted}
The encrypted result may be incorrect ~ In
The node-rsa module uses the pkcs1_ep EP filling mode by default for encryption and decryption, while pkcs1 is used by default in js.In node-rsa, the function can be modified.
Function rsaEncrypt (message, key) {let clientKey = new NodeRSA (RSA_PUBLIC_KEY) // by default, pkcs1_oaep is used for encryption and decryption in the node-rsa module, in js, pkcs1clientKey is used by default for encryption and decryption. setOptions ({encryptionScheme: 'pkcs1'}) // Add this line of code let encrypted = clientKey. encrypt (message, 'base64') return encrypted}