Node-rsa module encryption has some pitfalls.

Source: Internet
Author: User

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}

 

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.