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kACQlrEHIItixV"
//  在JsEncrypt原型上写了分段加密方法 encryptLong 使用时替换encrypt方法即可
JSEncrypt.prototype.encryptLong = function (string) {
    var k = this.getKey();
    var maxLength = (((k.n.bitLength() + 7) >> 3) - 11);
    try {
      var lt = "";
      var ct = "";
  
      if (string.length > maxLength) {
        lt = string.match(/.{1,50}/g);
        lt.forEach(function (entry) {
          var t1 = k.encrypt(entry);
          ct += t1;
        });
        return hex2b64(ct);
      }
      var t = k.encrypt(string);
      var y = hex2b64(t);
      return y;
    } catch (ex) {
      return false;
    }
  };
  // //  在JsEncrypt原型上写了分段加密方法 encryptLong 使用时替换decrypt方法即可
  JSEncrypt.prototype.decryptLong = function (string) {
    var k = this.getKey();
    var maxLength = ((k.n.bitLength() + 7) >> 3);
    //var maxLength = 128;
    try {

        var str = bytesToHex(string);
        //var b=hex2Bytes(str);

        var inputLen = str.length;

        var ct = "";
        if (str.length > maxLength) {

            var lt = str.match(/.{1,256}/g);
            lt.forEach(function (entry) {
                var t1 = k.decrypt(entry);
                ct += t1;
            });
            return ct;
        }
        var y = k.decrypt(bytesToHex(string));
        return y;
    } catch (ex) {
        return false;
    }
};
/**
 * 公钥加密
 * @param {*} id 
 * @returns 
 */
export function encryption(id){
    let encryptor = new JSEncrypt()
    encryptor.setPublicKey(pubKey)
    return encryptor.encrypt(id)
}
/**
 * 私钥解密
 * @param {*} id 
 * @returns 
 */
export function decrypt(id){
    let decrypt = new JSEncrypt()
    decrypt.setPrivateKey(priKey)
    return decrypt.decrypt(id)
}