uniapp打包成微信小程序的RSA-OAEP加解密
import forge from './forge.min.js' function getKeyStr1(str){ let publicKey = '-----BEGIN PUBLIC KEY-----\n...\n-----END PUBLIC KEY-----' const publicObj = forge.pki.publicKeyFromPem(publicKey); const bytes = publicObj.encrypt(str,'RSA-OAEP'); const encrypted = forge.util.encode64(bytes); //encode64 return encrypted } function getUncrypted1(str){ let privateKey = '-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----' const privateObj = forge.pki.privateKeyFromPem(privateKey); const bytes = forge.util.decode64(str); const decrypted = privateObj.decrypt(bytes,'RSA-OAEP'); return decrypted }
因特殊需求微信小程序加密方式由RSA改成RSA-OAEP,PC端用node-forge,小程序换了好几种(node-forge,node-rsa...)要不就是秘钥不对,要不就是文件太大,各种报错。最后还是用forge.js直接引入。但是原有的forge.js引入也有很多报错,将里面的报错都改正后文件大小275kb。自测没问题,能够正常加解密跟后端交互,需要文件可联系我。
肖cc
QQ2398506993