记录最近项目需要用到rsa加密遇到的问题 ---------- Message too long for RSA,以及解决方法。希望能帮到你们哦!

背景

我项目是用的npm里的 jsencrypt 插件进行加密的。

加密代码如下:

let encryptedData = (data, key=PUBLIC_KEY) => {
    let encryptor = new JSEncrypt()
    encryptor.setPublicKey(key)
    let cptData = encryptor.encrypt(data)
    return cptData
}

问题

项目中需要加密一个很长的字符串,因为太长的缘故所以加密失败,加密结果返回false,并抛出如下错误:

Message too long for RSA

解决方法:

最终找到另一个npm依赖包----------> encryptlong <------------(使劲戳它,进入官网查看具体使用方法,这里不多做介绍),完美解决这个问题了。

修改后的代码:

const {JSEncrypt} = require('encryptlong')



let encryptedData = (data, key=PUBLIC_KEY) => {

    let encryptor = new JSEncrypt()

    encryptor.setPublicKey(key)

    let cptData = encryptor.encryptLong(data)

    return cptData

}

修改后效果:

看完了点个赞再走呗,老铁!

Logo

基于 Vue 的企业级 UI 组件库和中后台系统解决方案,为数万开发者服务。

更多推荐