[记录]JS 下 RSA长字符串分段加密 | 祭夜博客
  • 欢迎光临,这个博客颜色有点多

[记录]JS 下 RSA长字符串分段加密

其它笔记 msojocs 4年前 (2020-10-21) 2750次浏览 已收录 2个评论 扫描二维码
文章目录[隐藏]

前言

恕我说句实话,网上的没一个我能用的,还是自己写一个好用 #(喷)

测试说明

加密以下字符串,传输至后端解密,搜索引擎里的总是回有几个分段解不出来

{"JSZX_cookie":"ASPSESSIONIDACCCRDRA=AAAAAAAAAAAAAAAAAAAAAA; ASPSESSIONIDQQQBBSRA=AAAAAAAAAAAAAAAAAAAAAAA","link":"UTp=Xs&jkdk=Y&ObjId=6666666666&Id=11757","healthStatus":[{"title":"现居住地","key":"place","select":["——请选择——","航空港校内","龙泉校内","新气象小区","成信家园","成都(校外)","外地"],"index":1,"outLand":{"province":"","city":"","area":""}},{"title":"居住地状态","key":"placeStatus","select":["——请选择——","一般地区","疫情防控重点地区","所在小区被隔离管控"],"index":1},{"title":"工作状态","key":"workStatus","select":["——请选择——","航空港校内上班或学习","龙泉校内上班或学习","在校外完成实习任务","在家","在校外"],"index":1},{"title":"个人健康状况","key":"personHealthStatus","select":["——请选择——","正常","有可疑症状","疑似感染","确诊感染","确诊感染但已康复"],"index":1},{"title":"个人生活状态","key":"personLifeStatus","select":["——请选择——","正常","居家隔离观察","集中隔离观察","居家治疗","住院治疗"],"index":1},{"title":"家庭成员状况","key":"personStatus","select":["——请选择——","全部正常","有人有可疑症状","有人疑似感染","有人确诊感染","有人确诊感染但已康复"],"index":1}],"healthStatusOtherInfo":"","outIn":{"toPlace":"","toResult":"","out":[["??","今天","明天","后天"],["??","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22"]],"outIndex":[0,0],"in":[["??","当天","第2天","第3天"],["??","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23"]],"inIndex":[0,0]},"monthStatus":[{"type":"important","title":"(1)曾前往疫情防控重点地区?","isGo":false,"details":""},{"type":"danger","title":"(2)接触过疫情防控重点地区高危人员?","isGo":false,"details":""},{"type":"suspected","title":"(3)接触过感染者或疑似患者?","isGo":false,"details":""}],"transport":{"method":["—请选择—","飞机","火车","汽车","轮船","私家车或专车","其他"],"methodIndex":0,"toolId":"","backTime":{"month":"","day":""}},"checkInTime":"2020-10-21 06:00,最后变动:10-21 11:43","requestRet":"暂未填写,一般应于上午10:00前提交申请","autoCheckInTime":"07:00:00","isAutoCheckIn":true,"__webviewId__":62,"time":"07:00:00","userCode":"SRTAAAAAAAAAAAAAAAAA","action":0,"userId":"6666666666","userPass":"999999999"}

代码

function RSAEncryptLong(string){
    var k = this;
    var maxLength = (((k.n.bitLength() + 7) >> 3) - 11);
    try {
        var subStr="", encryptedString = "";
        var subStart = 0;
        var bitLen = 0;
        for(var i = 0, len = string.length; i < len; i++){
            //js 是使用 Unicode 编码的,每个字符所占用的字节数不同
            var charCode = string.charCodeAt(i);
            // if(i >= 218 && i<= 275)
            //     console.log(i, charCode.toString(16), string[i])
            var increment = 0;
            if(charCode <= 0x007f) {
                increment = 1;
            }else if(charCode <= 0x07ff){
                increment = 2;
            }else if(charCode <= 0xffff){
                increment = 3;
            }else{
                increment = 4;
            }
            bitLen += increment;
        
            //字节数到达上限,获取子字符串加密并追加到总字符串后。更新下一个字符串起始位置及字节计算。
            if (bitLen > maxLength) {
                // 本位字符加上会过长,回到上一位
                i--
                subStr = string.substring(subStart, i);
                console.log(subStart, i, subStr);
                let t = k.encrypt(subStr);
                // console.log(t)
                encryptedString += t
                // substring第i位不会被取到。-1是因为进入下次循环时,i+1导致上一位被忽略,得回去把它带上
                subStart = i--;
                bitLen = 0;
            }
        }
        subStr=string.substring(subStart,len)
        console.log(subStr)
        encryptedString += k.encrypt(subStr);
        return encryptedString
        // return hex2b64(encryptedString);
    } catch (ex) {
      return false;
    }
}

祭夜の咖啡馆 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:[记录]JS 下 RSA长字符串分段加密
喜欢 (2)
[1690127128@qq.com]
分享 (0)
发表我的评论
取消评论
OwO表情
贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(2)个小伙伴在吐槽
  1. 如果是长文本这样加密,再采集就难了。
    小熊博客2020-10-30 15:19 回复 Windows 7 | Chrome 78.0.3904.108
    • cqcadmin
      采集?指的是什么 #(疑问)
      祭夜2020-10-30 15:22 回复 Windows 10 | Chrome 87.0.4280.20