Home > Web Front-end > JS Tutorial > body text

Code examples for asymmetric encryption in Node.js

亚连
Release: 2018-06-09 10:51:29
Original
2657 people have browsed it

This article mainly shares with you the Node.js asymmetric encryption method and code examples. Friends who are interested in this can refer to it.

Preface

I just answered the question "Asymmetric decryption error" raised by a brother on SegmentFault. This is a security application of Node.js. There should be many people encountering the same problem. Based on the questions answered, here is a brief summary.

For theoretical knowledge of asymmetric encryption, you can refer to the author's previous article "NODEJS Advanced: CRYPTO Module Theory".

The complete code can be found in "Nodejs Learning Notes". You are also welcome to pay attention to programmer Xiaoka's GitHub.

Encryption and decryption methods

In Node.js, the module responsible for security is crypto. In asymmetric encryption, public key encryption, private key decryption, and the corresponding APIs for encryption and decryption are as follows.

Encryption function:

crypto.publicEncrypt(key, buffer)
Copy after login

Decryption function:

crypto.privateDecrypt(privateKey, buffer)
Copy after login

Getting started example

Assume that there is the following utils.js

// utils.js
const crypto = require('crypto');
// 加密方法
exports.encrypt = (data, key) => {
 // 注意,第二个参数是Buffer类型
 return crypto.publicEncrypt(key, Buffer.from(data));
};
// 解密方法
exports.decrypt = (encrypted, key) => {
 // 注意,encrypted是Buffer类型
 return crypto.privateDecrypt(key, encrypted);
};
Copy after login

Test code app .js:

const utils = require('./utils');
const keys = require('./keys');
const plainText = '你好,我是程序猿小卡';
const crypted = utils.encrypt(plainText, keys.pubKey); // 加密
const decrypted = utils.decrypt(crypted, keys.privKey); // 解密
console.log(decrypted.toString()); // 你好,我是程序猿小卡
Copy after login

Attached are the public key and private key keys.js:

exports.privKey = `-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDFWnl8fChyKI/Tgo1ILB+IlGr8ZECKnnO8XRDwttBbf5EmG0qV
8gs0aGkh649rb75I+tMu2JSNuVj61CncL/7Ct2kAZ6CZZo1vYgtzhlFnxd4V7Ra+
aIwLZaXT/h3eE+/cFsL4VAJI5wXh4Mq4Vtu7uEjeogAOgXACaIqiFyrk3wIDAQAB
AoGBAKdrunYlqfY2fNUVAqAAdnvaVOxqa+psw4g/d3iNzjJhBRTLwDl2TZUXImEZ
QeEFueqVhoROTa/xVg/r3tshiD/QC71EfmPVBjBQJJIvJUbjtZJ/O+L2WxqzSvqe
wzYaTm6Te3kZeG/cULNMIL+xU7XsUmslbGPAurYmHA1jNKFpAkEA48aUogSv8VFn
R2QuYmilz20LkCzffK2aq2+9iSz1ZjCvo+iuFt71Y3+etWomzcZCuJ5sn0w7lcSx
nqyzCFDspQJBAN3O2VdQF3gua0Q5VHmK9AvsoXLmCfRa1RiKuFOtrtC609RfX4DC
FxDxH09UVu/8Hmdau8t6OFExcBriIYJQwDMCQQCZLjFDDHfuiFo2js8K62mnJ6SB
H0xlIrND2+/RUuTuBov4ZUC+rM7GTUtEodDazhyM4C4Yq0HfJNp25Zm5XALpAkBG
atLpO04YI3R+dkzxQUH1PyyKU6m5X9TjM7cNKcikD4wMkjK5p+S2xjYQc1AeZEYq
vc187dJPRIi4oC3PN1+tAkBuW51/5vBj+zmd73mVcTt28OmSKOX6kU29F0lvEh8I
oHiLOo285vG5ZtmXiY58tAiPVQXa7eU8hPQHTHWa9qp6
-----END RSA PRIVATE KEY-----
`;
exports.pubKey = `-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDFWnl8fChyKI/Tgo1ILB+IlGr8
ZECKnnO8XRDwttBbf5EmG0qV8gs0aGkh649rb75I+tMu2JSNuVj61CncL/7Ct2kA
Z6CZZo1vYgtzhlFnxd4V7Ra+aIwLZaXT/h3eE+/cFsL4VAJI5wXh4Mq4Vtu7uEje
ogAOgXACaIqiFyrk3wIDAQAB
-----END PUBLIC KEY-----
`;
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

How to implement the image preview function in the WeChat applet

How to implement Baidu statistics using vue

How to use WeChat applet to achieve the effect of MUI number input box

Basic usage of render function in Vue (detailed tutorial)

How to use the gallery slider component in the WeChat applet

How to use the scroll-view component to implement scrolling animation in the WeChat applet

Usage of checkbox component in WeChat mini program

The above is the detailed content of Code examples for asymmetric encryption in Node.js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template