虚渺
虚渺
  • 发布:2022-08-15 02:10
  • 更新:2022-08-15 16:06
  • 阅读:665

求在unicloud上实现RSA4096解密的代码。

分类:uniCloud

由第三方推送过来的加密信息密匙是用RSA4096加密,找了很多js或unicloud上的rsa加解密,大多数是2048位。请问哪位大神能提供一个基于前端js或unicloud的rsa4096的解密模块?

2022-08-15 02:10 负责人:无 分享
已邀请:
DCloud_uniCloud_WYQ

DCloud_uniCloud_WYQ

node自带的crypto模块的privateDecrypt不能用吗

  • 虚渺 (作者)

    crypto好像有,但怎样引用?这第一句就出错了:const { generateKeyPairSync } = require('crypto');

    2022-08-16 16:35

  • DCloud_uniCloud_WYQ

    回复 m***@163.com: 错误是什么?

    2022-08-16 17:18

  • 虚渺 (作者)

    回复 DCloud_uniCloud_WYQ: 用const crypto = require('crypto');就可以。不过后面不会用了,大神有没有参考资料?

    我想实现就是提交一个rsa4096加密的字符串,返回解密结果;也就是说函数只需要解密,私匙直接放在函数里面。

    2022-08-17 11:19

  • DCloud_uniCloud_WYQ

    回复 m***@163.com: 搜 nodejs privateDecrypt

    2022-08-17 14:42

  • 虚渺 (作者)

    回复 DCloud_uniCloud_WYQ:

    你好,我用的测试代码如下:

    'use strict';

    exports.main = async (event, context) => {

    var reqData=event.queryStringParameters;

    if(reqData==null || reqData.msg==null) return "";


    var pKey="-----BEGIN RSA PUBLIC KEY-----"+  
    "MIICCgKCAgEAxrgtuFBZsjDHjiDx4EiLK3Jh/tyl2PVtcpdb9jhCeHAhEXcAds7E"+
    "54p/2pWTI1Jm9LJmm40fc/iDTzJdMi0Vi/7v45D9NuS7EU90OHNhY85sE2lDa/4H"+
    "k1bm0AJ3giuIkkedFlr9VQCGRbQ9rhDBXm5nU4+YUnKI5LnMjiSZxe7mHsm0EdHN"+
    "RocF6m6ERTwkvilJOAj/GUH1SV3kbK1KrtUvYAHa0uHMobOaV+Tq59rY7IygbX0+"+
    "F136TN+Dt3x8Hp1XTH0Dy39/sltMyEvU5CgzZ1luT+teu/R5p7MRVtL2HWT6ofP/"+
    "Fjh4ktwdgr+Rgnkg6cySeHaA2kxQHE5fQE+nWCVMtbTnOCwUKQg4t8rOPl5Fugun"+
    "tr+gCw9Lj5gWaRipB90e8S2QxQzLCrRtKVUCbDCfJkNHIQ6wTbHRkzqclk3qBZ1b"+
    "KK/bexsRmBcY+QBFQk132f9FWGWIVkDxeaONns3suhimm/41Cy79pc44nooPZnBq"+
    "MEDM0MCK7QtV2dUrEEakzZz6KLbEemOdWIm17d7Csfixi5G5q7AU4AzmjXeRN3s+"+
    "06jTE8fozpCot1pbKlBPsEOJArQIbErBGioBYBUJvK6lWlZ9wZE3vOAf/9zhfJkN"+
    "7IfO3TVDEERtufFBVHTiVp4UTUleBL64qkohsngM0TOTmHkg02iA/A8CAwEAAQ=="+
    "-----END RSA PUBLIC KEY-----";
    const crypto = require('crypto');
    if(!crypto)return "";
    const encrypted=crypto.publicEncrypt(pKey,Buffer.from(reqData.msg));

    return encrypted.toString("base64");

    };

    执行后提示

    Error: error:0906D06C:PEM routines:PEM_read_bio:no start line Error: error:0906D06C:PEM routines:PEM_read_bio:no start line at Object.publicEncrypt (crypto.js:390:12) at Object.exports.main (/var/user/__index.js:19:25) at y (/var/user/index.js:1:8617) at EventHandler.exports.main [as realHandler] (/var/user/index.js:1:9854) at EventHandler.handle (/var/runtime/node8/bootstrap.js:427:28) at invoke (/var/runtime/node8/bootstrap.js:225:22) at Timeout.setTimeout [as _onTimeout] (/var/runtime/node8/bootstrap.js:150:9) at ontimeout (timers.js:475:11) at tryOnTimeout (timers.js:310:5) at Timer.listOnTimeout (timers.js:270:5)


    我估计是public key格式问题,但不知道正确应该怎样,请大神指点迷津。

    2022-08-19 10:44

  • DCloud_uniCloud_WYQ

    回复 m***@163.com: 你公钥换行没了

    2022-08-19 16:14

  • 虚渺 (作者)

    回复 DCloud_uniCloud_WYQ:

    谢谢大神。加密后问题又来了,解密时提示error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong。

    代码如下:

    'use strict';


    const { RSA_PKCS1_OAEP_PADDING, RSA_PKCS1_PADDING } = require('constants');


    exports.main = async (event, context) => {

    var reqData=event.queryStringParameters;

    reqData.msg="rlxuLAhYW1VHHQEhrgFquUVUbLthHnPGr8LYz4Ft/t3TNPHrwWFMW1ZIOhPpEOP9ZqRefGP4K8MQ+8onBfkg0e/pUR3N0OunG/VE0e5ONlj4YN4ziUlX0SV+x9J8CVH2anpwX/DVsoZNx7WTuf9H1mXNp8Vzh3cHqkvhG8kPk4/CQJbdzVZBeef37lK9K8ecp3t+w87ZjirFDeWXK/LjNphpBzlT7Z+ExP+RF/ARqr/tm/jeqtZIq29BgBO+KGRfdI2qXHhslk9+c+Pwr9oPIrRnA6oNNf0nA6ArdcWUjOnwMUGK56tTgFlFzStQMTRGiYqUUmuScaoUfs3Xb8ez1gbg2RzEZb4nJbyM5Bpe3DTJFJrs8DoHgCgiaM2OLYa37OiPGLSVndwFKyk1kgKHP6mrHxeUir9j8Hwh5NNv3h1/N3L+9EohB8xeTj+Lcy6gmw/Jq4DtACcuerQiQHRjYn4NXF/OqZ6qLV9EO7XfsXyFqEFkH6a1arXvjLurb984W8MM05PhUXEzmzIWh1u4CsPMwmutVd0QmRleRtMeewbWsU9B1t2ZTE05EedMFN8Gbb27K/V7wlYdBkZVG2UfgqIaxsub+KLYORRDYH4dRN5JDJPAyNt7/hV+b2du3TbL23+P2u/pwrKaC/Vxs4ger4ES313rB/IYl5/h02O21Mk=";

    if(reqData==null || reqData.msg==null) return {flag:-1,msg:"No decrypt message."};

    var pKey="-----BEGIN RSA PRIVATE KEY-----\r\n"+

    "MIIJQgIBADANBgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQDGuC24UFmyMMeO"+

    "IPHgSIsrcmH+3KXY9W1yl1v2OEJ4cCERdwB2zsTnin/alZMjUmb0smabjR9z+INP"+

    "Ml0yLRWL/u/jkP025LsRT3Q4c2FjzmwTaUNr/geTVubQAneCK4iSR50WWv1VAIZF"+

    "tD2uEMFebmdTj5hScojkucyOJJnF7uYeybQR0c1GhwXqboRFPCS+KUk4CP8ZQfVJ"+

    "XeRsrUqu1S9gAdrS4cyhs5pX5Orn2tjsjKBtfT4XXfpM34O3fHwenVdMfQPLf3+y"+

    ……省略40行……

    "jB0Jj+WXbYHaBJS0OhQmC5xUTz3V7YHs6urvZtH3UmdvfJDde12TrdFJKcFit7zl"+

    "K41fQO2gl8tDsWU/GxUOGA1eTYe3BCwYEzgncmPvWip11NVn+JbSKrvAVE3iWuW3"+

    "PbiXpk2062U2LpxITYtuanatgNpv3lak+xYcR2ucqxfRc7bp2DxiFuJwWrwt2OOc"+

    "x3V+IUuRgR0KbM1dz7UatoCPVm/k+SgqkDsxCw1++CThohfoWkzDxCwvm6GT5C5a"+

    "E2Q3tQv61bwGq34xf86zEp685S2mOQ=="+

    "\r\n-----END RSA PRIVATE KEY-----";

    var pKeyOption={

    key:pKey,

    constants:RSA_PKCS1_PADDING

    };

    var _buffer=Buffer.from(reqData.msg,'base64');


    const crypto = require('crypto');  
    if(!crypto)return {flag:-1,msg:"Crypto did not init."};
    const decrypted=crypto.publicDecrypt(pKeyOption,_buffer);

    return decrypted;

    };

    2022-08-22 14:15

该问题目前已经被锁定, 无法添加新回复