收藏
回答

rsa加密超过长度文本如何实现?

问题类型 插件 AppID 插件版本号 AppID 微信版本 基础库版本
需求 wxf25d506ff81e19fb 0.003 knkghgfhfhf 6.5.3 2.0.0

- 需求的场景描述(希望解决的问题)

目前插件rsa加解密对于长文本是如何处理的呢?急需

如果分段实现,那中间的长度与切分方法有参数有资料可参考么?

目前我找到的仅有这些

//方法2
 
JSEncrypt.prototype.encryptLong2 = function (string) {
 
    var k = this.getKey();
 
    try {
 
        var lt = "";
 
        var ct = "";
 
        //RSA每次加密117bytes,需要辅助方法判断字符串截取位置
 
        //1.获取字符串截取点
 
        var bytes = new Array();
 
        bytes.push(0);
 
        var byteNo = 0;
 
        var len, c;
 
        len = string.length;
 
        var temp = 0;
 
        for (var i = 0; i < len; i++) {
 
            c = string.charCodeAt(i);
 
            if (c >= 0x010000 && c <= 0x10FFFF) {
 
                byteNo += 4;
 
            } else if (c >= 0x000800 && c <= 0x00FFFF) {
 
                byteNo += 3;
 
            } else if (c >= 0x000080 && c <= 0x0007FF) {
 
                byteNo += 2;
 
            } else {
 
                byteNo += 1;
 
            }
 
            if ((byteNo % 117) >= 114 || (byteNo % 117) == 0) {
 
                if (byteNo - temp >= 114) {
 
                    bytes.push(i);
 
                    temp = byteNo;
 
                }
 
            }
 
        }
 
        //2.截取字符串并分段加密
 
        if (bytes.length > 1) {
 
            for (var i = 0; i < bytes.length - 1; i++) {
 
                var str;
 
                if (i == 0) {
 
                    str = string.substring(0, bytes[i + 1] + 1);
 
                } else {
 
                    str = string.substring(bytes[i] + 1, bytes[i + 1] + 1);
 
                }
 
                var t1 = k.encrypt(str);
 
                ct += t1;
 
            }
 
            ;
 
            if (bytes[bytes.length - 1] != string.length - 1) {
 
                var lastStr = string.substring(bytes[bytes.length - 1] + 1);
 
                ct += k.encrypt(lastStr);
 
            }
 
            return hexToBytes(ct);
 
        }
 
        var t = k.encrypt(string);
 
        var y = hexToBytes(t);
 
        return y;
 
    } catch (ex) {
 
        return false;
 
    }
 
};
 
 
 
JSEncrypt.prototype.decryptLong2 = function (string) {
 
    var k = this.getKey();
 
    // var maxLength = ((k.n.bitLength()+7)>>3);
 
    var MAX_DECRYPT_BLOCK = 128;
 
    try {
 
        var ct = "";
 
        var t1;
 
        var bufTmp;
 
        var hexTmp;
 
        var str = bytesToHex(string);
 
        var buf = hexToBytes(str);
 
        var inputLen = buf.length;
 
        //开始长度
 
        var offSet = 0;
 
        //结束长度
 
        var endOffSet = MAX_DECRYPT_BLOCK;
 
 
 
        //分段加密
 
        while (inputLen - offSet > 0) {
 
            if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
 
                bufTmp = buf.slice(offSet, endOffSet);
 
                hexTmp = bytesToHex(bufTmp);
 
                t1 = k.decrypt(hexTmp);
 
                ct += t1;
 
                 
 
            } else {
 
                bufTmp = buf.slice(offSet, inputLen);
 
                hexTmp = bytesToHex(bufTmp);
 
                t1 = k.decrypt(hexTmp);
 
                ct += t1;
 
              
 
            }
 
            offSet += MAX_DECRYPT_BLOCK;
 
            endOffSet += MAX_DECRYPT_BLOCK;
 
        }
 
        return ct;
 
    } catch (ex) {
 
        return false;
 
    }
 
};


- 希望提供的能力

插件rsa加解密目前的长度很有很哦,稍微一个json就加密不了

回答关注问题邀请回答
收藏

2 个回答

登录 后发表内容