- Expected 0x2: got 0x30
原因可能是传输的key 没有指定证书格式类型,
// 加密
const publicKey = 'MIGfM???????????????????????????' // 公钥 (换成自己正常的公钥)
const nodersa = new NodeRsa(publicKey, 'pkcs8-public') // 使用key, 第二个参数'pkcs8-public' 指定key 是什么类型, 根据你的publickey privatekey 是怎么生成的,指定其类型格式
nodersa.setOptions({encryptionScheme: 'pkcs1'})
const encryptedData = nodersa.encrypt(str, 'base64')
// 解密
const privateKey = '?????????????????????????????????????????????????????????????????' // 私钥 (你的私钥)
const nodersa = new NodeRsa(privateKey, 'pkcs8-private') // 注意参数指定类型
nodersa.setOptions({encryptionScheme: 'pkcs1'})
const decryptedData= nodersa.decrypt(str, 'utf8')
node-rsa: npm 官方文档 https://www.npmjs.com/package/node-rsa#importexport-keys