node. JS Advanced: 5-minute Entry Asymmetric encryption method

Source: Internet
Author: User
Tags begin rsa private key asymmetric encryption

Objective

Just answered Segmentfault's previous brother's question, "asymmetric decryption error." This belongs to node. js in the security of the application, meet the same problem should be a lot of people, based on the answer to the question, here is a brief summary.

The theoretical knowledge of asymmetric encryption can be referred to the author's previous article "Nodejs Advanced: Theory of Crypto Module".

The complete code can be found in the NODEJS study notes, and you are welcome to focus on the program ape Little card GitHub.

Encryption, decryption methods

In node. js, the module that is responsible for security is crypto . Asymmetric encryption, public key encryption, private key decryption, plus decryption of the corresponding APIs are as follows.

Cryptographic functions:

crypto.publicEncrypt(key, buffer)

Decryption function:

crypto.privateDecrypt(privateKey, buffer)
Getting Started examples

Suppose there are the followingutils.js

//Utils.jsConstCrypto= require(' Crypto ');//Encryption Methodexports.Encrypt =(Data,Key= {  //Note that the second parameter is a buffer type  return Crypto.Publicencrypt(Key, Buffer. from(data));};//Decryption Methodexports.Decrypt =(Encrypted,Key= {  //Note that the encrypted is of type buffer  return Crypto.Privatedecrypt(Key,Encrypted;};

Test Code app.js :

ConstUtils= require('./utils ');ConstKeys= require('./keys ');ConstPlainText= ' Hello, I'm a program ape little card ';Constcrypted= Utils.Encrypt(PlainText, Keys.PubKey); //EncryptionConstDecrypted= Utils.Decrypt(crypted, Keys.Privkey); //DecryptionConsole.Log(decrypted.toString()); //Hello, I'm a program ape little card

Attach the public key, private keykeys.js

Exports.privkey = '-----BEGIN RSA PRIVATE KEY-----miicxqibaakbgqdfwnl8fchyki/tgo1ilb+ ilgr8zecknno8xrdwttbbf5emg0qv8gs0agkh649rb75i+tmu2jsnuvj61cncl/7ct2kaz6czzo1vygtzhlfnxd4v7ra+aiwlzaxt/h3ee+/ cfsl4vaji5wxh4mq4vtu7uejeogaogxacaiqifyrk3widaqabaogbakdrunylqfy2fnuvaqaadnvavoxqa+psw4g/ D3inzjjhbrtlwdl2tzuximezqeefueqvhorota/xvg/r3tshid/qc71efmpvbjbqjjivjubjtzj/o+l2wxqzsvqewzyatm6te3kzeg/culnmil +xu7xsumslbgpaurymha1jnkfpakea48auogsv8vfnr2quymilz20lkczffk2aq2+9isz1zjcvo+iuft71y3+ etwomzczcuj5sn0w7lcsxnqyzcfdspqjban3o2vdqf3gua0q5vhmk9avsoxlmcfra1rikufotrtc609rfx4dcfxdxh09uvu/ 8hmdau8t6ofexcbriiyjqwdmcqqczljfddhfuifo2js8k62mnj6sbh0xlirnd2+/ruutubov4zuc+ rm7gtuteoddazhym4c4yq0hfjnp25zm5xalpakbgatlpo04yi3r+dkzxquh1pyyku6m5x9tjm7cnkcikd4wmkjk5p+ s2xjyqc1aezeyqvc187djprii4oc3pn1+takbuw51/5vbj+ ZMD73MVCTT28OMSKOX6KU29F0LVEH8IOHILOO285VG5ZTMXIY58TAIPVQXA7EU8HPQHTHWA9QP6-----END RSA PRIVATE KEY-----'; Exports.pubkey = '-----BEGIN public KEY-----MIGFMA0GCSQGSIB3DQEBAQUAA4GNADCBIQKBGQDFWNL8FCHYKI/TGO1ILB+ILGR8zecknno8xrdwttbbf5emg0qv8gs0agkh649rb75i+tmu2jsnuvj61cncl/7ct2kaz6czzo1vygtzhlfnxd4v7ra+aiwlzaxt/h3ee+/ Cfsl4vaji5wxh4mq4vtu7uejeogaogxacaiqifyrk3widaqab-----END Public KEY-----';
Summary

It can be seen that asymmetric encryption and decryption through node. JS is quite handy. For more usage, refer to the official documentation.

RELATED LINKS

Program Ape Little Card GitHub

Nodejs Study Notes

Asymmetric decryption Error

Https://nodejs.org/api/crypto.html

node. JS Advanced: 5-minute Entry Asymmetric encryption method

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.