【加解密】bcryptjs | CryptoJS | JSEncrypt | node-rsa 加密| 解密 | RSA | ASE | MD5

加解密


1、 bcryptjs 加密 - 只可加密,比对密码,不可解密

:推荐node环境下可使用,浏览器不知道可不可以用,可用去【https://www.npmjs.com/package/bcryptjs】查看浏览器用法,但我不会。如下图


在这里插入图片描述


  • 下载
npm i bcryptjs 

  • 作用:字符串加密,已加密的字符串不可破解,只可比对。
  • 优点:加密后不可解密
  • 缺点:已加密的字符不可解密。
  • 用作领域:敏感信息加密,例如登录密码
  • npm地址:npm - https://www.npmjs.com/package/bcryptjs

src/utils/bcrypt.js

import bcryptjs from 'bcryptjs'//不可逆加密 bcryptjs 

/**
 * 不可逆加密 ,一旦加密不可解密,只可比对 | 传如一个参数>加密 | 2个参数>密码比对
 * @param {String} txt 加密、比对的文本
 * @param {String} hash 加密的哈希 
 * @returns {[String|Boolear]} 返回值
 */
const by = (...args) => {
    let res = null
    const [t, h] = args

    // 比对
    if (args.length === 2) {
        res = bcryptjs.compareSync(t, h)
    } else if (args.length === 1) {
        // 加密
        res = bcryptjs.hashSync(t)
    }
    return res
}
export default by

使用cyj加密

import cyj from './src/utils/bcrypt.js '

const pwd = '728637263我是隐私信息'

//加密
const 加密= cyj(pwd)

console.log(加密)//$2a$10$BsXXaevfIG8Og7mKSF5qFu0vSugvJbYWyr3apz9BElCV254.SoIYe
console.log(cyj(pwd, 加密))//true
console.log(cyj(pwd, '我是老6'))//false

在这里插入图片描述

2、CryptoJS AES加解密 - 单个key加密

  • 下载
npm i crypto-js

  • 介绍:一个key可以加密与解密,就像锁钥匙,一把钥匙既可以开锁也可以解锁
  • 优点:加密速度较快
  • npm地址:https://www.npmjs.com/package/crypto-js

CryptoJS .js

import CryptoJS from 'crypto-js'// aes加解密

/**
 * AES加解密 
 * @param {String} txt 加解密的文本
 * @param {String} key 密钥
 * @param {Number} t default:0 加密 | 0 加密 | 1 解密 | 操作类型
 * @returns 加密后的字符串
 */
const AES = (txt, key, t = 0) => {
    // 加密
    if (t === 0) {
        if (txt) {
            const encrypt = CryptoJS.AES.encrypt(txt, key)
            return encrypt.toString()
        }
        return null
    } else {
        // 解密
        if (txt) {
            const decrypted = CryptoJS.AES.decrypt(txt, key).toString(CryptoJS.enc.Utf8)

            return decrypted
        }
        return null
    }
}

/**
 * 创建AES密钥
 * @param {Number}  num defalut:10 生成几位数的key
 * @returns {String} 返回密钥
 */
const createAESKey = (num = 10) => {
    const library = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*+-./~=()[]{};:'?><,`";
    let key = ""
    for (var i = 0; i < num; i++) {
        let randomPoz = Math.floor(Math.random() * library.length);
        key += library.substring(randomPoz, randomPoz + 1);
    }
    return key
}


export {
    AES,
    createAESKey
} 

使用CryptoJS

import {
    AES,
    createAESKey
} from './CryptoJS.js'

//获取key
const 钥匙 = createAESKey()
const 账号 = '我是老6'

const 加密 = AES(账号, 钥匙)
const 解密 = AES(加密, 钥匙, 1)

console.log(加密)
console.log(解密)

3、 JSEncrypt | RSA加密 - 2把钥匙

注意:jsencrypt只能在前端使用,不能在后端使用

  • 下载
npm i jsencrypt

该库仅可在浏览器使用

  • 作用:加密与解密,有2把钥匙,一把公钥,一把私钥,公钥负责加密,私钥负责解密。
  • 优点:相对安全,后端有2把钥匙,一把公钥,一把私钥。把公钥发给前端,前端负责加密数据,后端拿着私钥负责解密数据,这样一来,只用拿着私钥的人知道明文是什么了。
  • 缺点:加密速度较慢
  • npm地址:https://www.npmjs.com/package/jsencrypt

jsencrypt.js

import JSEncrypt from 'jsencrypt'  // 引入jsencrypt库  RSA 加、解密

/**
 * RSA 加、解密 | 仅前端可用
 * @param {String} txt 加解密字符
 * @param {String} key 密钥
 * @param {Number,default:0 加密 | 0 加密 |1 解密} t 操作类型 
 * @returns {String} 返回加解密字符
 */
const bwsRsa = (txt, key, t = 0) => {
    const jsencrypt = new JSEncrypt()
    let resData = null

    // 加密
    if (t === 0) {
        jsencrypt.setPublicKey(key)
        resData = jsencrypt.encrypt(txt)
        // 获取公私key
    } else {
        jsencrypt.setPrivateKey(key)
        resData = jsencrypt.decrypt(txt)
    }

    return resData
}

export default bwsRsa 

使用rsa加密

import bwsRsa from '. /jsencrypt.js'
import rsaPriPubKey from '. /rsaPriPubKey.js'//这是提前约定好的公、私钥,就不做代码演示,一般由后端约定

// 公、私钥
const { pubkey, prikey } = rsaPriPubKey

const 密码 = '我是老6'
const 加密 = bwsRs(密码, pubkey)
const 解密 = bwsRs(加密, prikey,1)

console.log(加密)
console.log(解密)

4、node-rsa - 后端RSA解加密 (可搭配前端jsencrypt)

注意

  • node-rsa只能在后端使用
  • jsencrypt只能在前端使用

  • 安装
cnpm i node-rsa

node-rsa封装

  • 该封装仅后端node环境下可用,前端不可用。
  • 该封装可配合前端 jsencrypt一起使用,例如前端jsencrypt的加密数据,可用此封装解密等。

servers/rsa.js

import NodeRSA from "node-rsa"

/**
 * 后端node获取公、私钥
 * keySize {Number} 生成私钥、密钥大小,与jsencrypt保持一致,默认1024
 * @returns {Object} { pubkey,prikey} pubkey 公钥 | prikey私钥
 */

const nodeGetKey = (keySize = 1024) => {
    const nodeKey = new NodeRSA({ b: keySize })
    const pubkey = nodeKey.exportKey('public')
    const prikey = nodeKey.exportKey('private')

    return {
        pubkey,
        prikey
    }
}

/**
 * 后端Node环境RSA加解密
 * @param {String} txt 加、解密的文本
 * @param {String} key 公、私钥
 * @param {Number} t 默认:0 | 0 解密 | 1 加密
 * @returns 
 */
const nodeRsa = (txt, key, t = 0) => {
    // 解密
    if (t === 0) {
        const nodeKey = new NodeRSA(key)
        nodeKey.setOptions({ encryptionScheme: 'pkcs1' }) // 因为jsencrypt自身使用的是pkcs1加密方案, nodejs需要修改成pkcs1。
        const decrypted = nodeKey.decrypt(txt, 'utf8')

        return decrypted
    } else {
        // 加密
        const nodeKey = new NodeRSA(key)
        nodeKey.setOptions({ encryptionScheme: 'pkcs1' })// 因为jsencrypt自身使用的是pkcs1加密方案, nodejs需要修改成pkcs1。
        const encrypted = nodeKey.encrypt(txt, 'base64')

        return encrypted
    }

}

export {
    nodeRsa,
    nodeGetKey,
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值