Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RSA加密长字符串,而且直接加密中文(不encode)会有问题,这里用别人提供的方法尝试可以了,这里我直接新增一个方法复用别人分段加密的算法代码 #312

Open
wisejin opened this issue Aug 2, 2024 · 0 comments

Comments

@wisejin
Copy link

wisejin commented Aug 2, 2024

/**

  • 分段加密长字符串-支持中文的
  • @param {string} str the string to encrypt
  • @return {string} the encrypted string encoded in base64
  • @public
    */
    JSEncrypt.prototype.encryptLongSupportCh = 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 hex2b64(ct);
    }
    var t = k.encrypt(string);
    var y = hex2b64(t);
    return y;
    } catch (ex) {
    return false;
    }
    };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant