NGToolsCSharp/NGTools/MYJS/JsEncryptHelper.js

32 lines
68 KiB
JavaScript
Raw Normal View History

2024-09-13 08:44:13 +00:00
var JSEncryptExports = {};
(function (exports) {
function BigInteger(a, b, c) { null != a && ("number" == typeof a ? this.fromNumber(a, b, c) : null == b && "string" != typeof a ? this.fromString(a, 256) : this.fromString(a, b)) } function nbi() { return new BigInteger(null) } function am1(a, b, c, d, e, f) { for (; --f >= 0;) { var g = b * this[a++] + c[d] + e; e = Math.floor(g / 67108864), c[d++] = 67108863 & g } return e } function am2(a, b, c, d, e, f) { for (var g = 32767 & b, h = b >> 15; --f >= 0;) { var i = 32767 & this[a], j = this[a++] >> 15, k = h * i + j * g; i = g * i + ((32767 & k) << 15) + c[d] + (1073741823 & e), e = (i >>> 30) + (k >>> 15) + h * j + (e >>> 30), c[d++] = 1073741823 & i } return e } function am3(a, b, c, d, e, f) { for (var g = 16383 & b, h = b >> 14; --f >= 0;) { var i = 16383 & this[a], j = this[a++] >> 14, k = h * i + j * g; i = g * i + ((16383 & k) << 14) + c[d] + e, e = (i >> 28) + (k >> 14) + h * j, c[d++] = 268435455 & i } return e } function int2char(a) { return BI_RM.charAt(a) } function intAt(a, b) { var c = BI_RC[a.charCodeAt(b)]; return null == c ? -1 : c } function bnpCopyTo(a) { for (var b = this.t - 1; b >= 0; --b) a[b] = this[b]; a.t = this.t, a.s = this.s } function bnpFromInt(a) { this.t = 1, this.s = 0 > a ? -1 : 0, a > 0 ? this[0] = a : -1 > a ? this[0] = a + DV : this.t = 0 } function nbv(a) { var b = nbi(); return b.fromInt(a), b } function bnpFromString(a, b) { var c; if (16 == b) c = 4; else if (8 == b) c = 3; else if (256 == b) c = 8; else if (2 == b) c = 1; else if (32 == b) c = 5; else { if (4 != b) return void this.fromRadix(a, b); c = 2 } this.t = 0, this.s = 0; for (var d = a.length, e = !1, f = 0; --d >= 0;) { var g = 8 == c ? 255 & a[d] : intAt(a, d); 0 > g ? "-" == a.charAt(d) && (e = !0) : (e = !1, 0 == f ? this[this.t++] = g : f + c > this.DB ? (this[this.t - 1] |= (g & (1 << this.DB - f) - 1) << f, this[this.t++] = g >> this.DB - f) : this[this.t - 1] |= g << f, f += c, f >= this.DB && (f -= this.DB)) } 8 == c && 0 != (128 & a[0]) && (this.s = -1, f > 0 && (this[this.t - 1] |= (1 << this.DB - f) - 1 << f)), this.clamp(), e && BigInteger.ZERO.subTo(this, this) } function bnpClamp() { for (var a = this.s & this.DM; this.t > 0 && this[this.t - 1] == a;)--this.t } function bnToString(a) { if (this.s < 0) return "-" + this.negate().toString(a); var b; if (16 == a) b = 4; else if (8 == a) b = 3; else if (2 == a) b = 1; else if (32 == a) b = 5; else { if (4 != a) return this.toRadix(a); b = 2 } var c, d = (1 << b) - 1, e = !1, f = "", g = this.t, h = this.DB - g * this.DB % b; if (g-- > 0) for (h < this.DB && (c = this[g] >> h) > 0 && (e = !0, f = int2char(c)) ; g >= 0;) b > h ? (c = (this[g] & (1 << h) - 1) << b - h, c |= this[--g] >> (h += this.DB - b)) : (c = this[g] >> (h -= b) & d, 0 >= h && (h += this.DB, --g)), c > 0 && (e = !0), e && (f += int2char(c)); return e ? f : "0" } function bnNegate() { var a = nbi(); return BigInteger.ZERO.subTo(this, a), a } function bnAbs() { return this.s < 0 ? this.negate() : this } function bnCompareTo(a) { var b = this.s - a.s; if (0 != b) return b; var c = this.t; if (b = c - a.t, 0 != b) return this.s < 0 ? -b : b; for (; --c >= 0;) if (0 != (b = this[c] - a[c])) return b; return 0 } function nbits(a) { var b, c = 1; return 0 != (b = a >>> 16) && (a = b, c += 16), 0 != (b = a >> 8) && (a = b, c += 8), 0 != (b = a >> 4) && (a = b, c += 4), 0 != (b = a >> 2) && (a = b, c += 2), 0 != (b = a >> 1) && (a = b, c += 1), c } function bnBitLength() { return this.t <= 0 ? 0 : this.DB * (this.t - 1) + nbits(this[this.t - 1] ^ this.s & this.DM) } function bnpDLShiftTo(a, b) { var c; for (c = this.t - 1; c >= 0; --c) b[c + a] = this[c]; for (c = a - 1; c >= 0; --c) b[c] = 0; b.t = this.t + a, b.s = this.s } function bnpDRShiftTo(a, b) { for (var c = a; c < this.t; ++c) b[c - a] = this[c]; b.t = Math.max(this.t - a, 0), b.s = this.s } function bnpLShiftTo(a, b) { var c, d = a % this.DB, e = this.DB - d, f = (1 << e) - 1, g = Math.floor(a / this.DB), h = this.s << d & this.DM; for (c = this.t - 1; c >= 0; --c) b[c + g + 1] = this[c] >> e | h, h = (this[c] & f) << d; for (c = g -
this.integerToByteHex = function (a) { var b = a.toString(16); return b.length % 2 == 1 && (b = "0" + b), b }, this.bigIntToMinTwosComplementsHex = function (a) {
var b = a.toString(16); if ("-" != b.substr(0, 1)) b.length % 2 == 1 ? b = "0" + b : b.match(/^[0-7]/) || (b = "00" + b);
else { var c = b.substr(1), d = c.length; d % 2 == 1 ? d += 1 : b.match(/^[0-7]/) || (d += 2); for (var e = "", f = 0; d > f; f++) e += "f"; var g = new BigInteger(e, 16), h = g.xor(a).add(BigInteger.ONE); b = h.toString(16).replace(/^-/, "") } return b
}, this.getPEMStringFromHex = function (a, b) { var c = CryptoJS.enc.Hex.parse(a), d = CryptoJS.enc.Base64.stringify(c), e = d.replace(/(.{64})/g, "$1\r\n"); return e = e.replace(/\r\n$/, ""), "-----BEGIN " + b + "-----\r\n" + e + "\r\n-----END " + b + "-----\r\n" }
}, KJUR.asn1.ASN1Object = function () { var a = ""; this.getLengthHexFromValue = function () { if ("undefined" == typeof this.hV || null == this.hV) throw "this.hV is null or undefined."; if (this.hV.length % 2 == 1) throw "value hex must be even length: n=" + a.length + ",v=" + this.hV; var b = this.hV.length / 2, c = b.toString(16); if (c.length % 2 == 1 && (c = "0" + c), 128 > b) return c; var d = c.length / 2; if (d > 15) throw "ASN.1 length too long to represent by 8x: n = " + b.toString(16); var e = 128 + d; return e.toString(16) + c }, this.getEncodedHex = function () { return (null == this.hTLV || this.isModified) && (this.hV = this.getFreshValueHex(), this.hL = this.getLengthHexFromValue(), this.hTLV = this.hT + this.hL + this.hV, this.isModified = !1), this.hTLV }, this.getValueHex = function () { return this.getEncodedHex(), this.hV }, this.getFreshValueHex = function () { return "" } }, KJUR.asn1.DERAbstractString = function (a) { KJUR.asn1.DERAbstractString.superclass.constructor.call(this); this.getString = function () { return this.s }, this.setString = function (a) { this.hTLV = null, this.isModified = !0, this.s = a, this.hV = stohex(this.s) }, this.setStringHex = function (a) { this.hTLV = null, this.isModified = !0, this.s = null, this.hV = a }, this.getFreshValueHex = function () { return this.hV }, "undefined" != typeof a && ("undefined" != typeof a.str ? this.setString(a.str) : "undefined" != typeof a.hex && this.setStringHex(a.hex)) }, JSX.extend(KJUR.asn1.DERAbstractString, KJUR.asn1.ASN1Object), KJUR.asn1.DERAbstractTime = function () { KJUR.asn1.DERAbstractTime.superclass.constructor.call(this); this.localDateToUTC = function (a) { utc = a.getTime() + 6e4 * a.getTimezoneOffset(); var b = new Date(utc); return b }, this.formatDate = function (a, b) { var c = this.zeroPadding, d = this.localDateToUTC(a), e = String(d.getFullYear()); "utc" == b && (e = e.substr(2, 2)); var f = c(String(d.getMonth() + 1), 2), g = c(String(d.getDate()), 2), h = c(String(d.getHours()), 2), i = c(String(d.getMinutes()), 2), j = c(String(d.getSeconds()), 2); return e + f + g + h + i + j + "Z" }, this.zeroPadding = function (a, b) { return a.length >= b ? a : new Array(b - a.length + 1).join("0") + a }, this.getString = function () { return this.s }, this.setString = function (a) { this.hTLV = null, this.isModified = !0, this.s = a, this.hV = stohex(this.s) }, this.setByDateValue = function (a, b, c, d, e, f) { var g = new Date(Date.UTC(a, b - 1, c, d, e, f, 0)); this.setByDate(g) }, this.getFreshValueHex = function () { return this.hV } }, JSX.extend(KJUR.asn1.DERAbstractTime, KJUR.asn1.ASN1Object), KJUR.asn1.DERAbstractStructured = function (a) { KJUR.asn1.DERAbstractString.superclass.constructor.call(this); this.setByASN1ObjectArray = function (a) { this.hTLV = null, this.isModified = !0, this.asn1Array = a }, this.appendASN1Object = function (a) { this.hTLV = null, this.isModified = !0, this.asn1Array.push(a) }, this.asn1Array = new Array, "undefined" != typeof a && "undefined" != typeof a.array && (this.asn1Array = a.array) }, JSX.extend(KJUR.asn1.DERAbstractStructured, KJUR.asn1.ASN1Object), KJUR.asn1.DERBoolean = function () { KJUR.asn1.DERBoolean.superclass.constructor.call(this), this.hT = "01", this.hTLV = "0101ff" }, JSX.extend(KJUR.asn1.DERBoolean, KJUR.asn1.ASN1Object), KJUR.asn1.DERInteger = function (a) { KJUR.asn1.DERInteger.superclass.constructor.call(this), this.hT = "02", this.setByBigInteger = function (a) { this.hTLV = null, this.isModified = !0, this.hV = KJUR.asn1.ASN1Util.bigIntToMinTwosComplementsHex(a) }, this.setByInteger = function (a) { var b = new BigInteger(String(a), 10); this.setByBigInteger(b) }, this.setValueHex = function (a) { this.hV = a }, this.getFreshValueHex = function () { return this.hV }, "undefined" != typeof a && ("undefined" != typeof a.bigint ? this.setByBigInteger(a.bigint) : "undefined" != typeof a["int"] ? this.setByInteger(a["int"]) : "undefined" != typeof a.hex && this.setValueHex(a.hex)) }, JSX.extend(KJUR.asn1.DERInteger, KJUR.asn1.ASN1Object), KJUR.asn1.DERBitSt
})(JSEncryptExports);
//自定义封装的参数加密
var JSEncrypt = JSEncryptExports.JSEncrypt;
(function ($) {
var defaults = {
publicKey: "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCC0hrRIjb3noDWNtbDpANbjt5Iwu2NFeDwU16Ec87ToqeoIm2KI+cOs81JP9aTDk/jkAlU97mN8wZkEMDr5utAZtMVht7GLX33Wx9XjqxUsDfsGkqNL8dXJklWDu9Zh80Ui2Ug+340d5dZtKtd+nv09QZqGjdnSp9PTfFDBY133QIDAQAB",
data: {}
};
$.encryptRequest = function (options) {
debugger;
var options = $.extend(defaults, options);
var encrypt = new JSEncrypt();
encrypt.setPublicKey(options.publicKey);
var sendData = new Object();
for (var key in options.data) {
sendData[key] = encrypt.encrypt(options.data[key]);
}
return sendData;
}
})(jQuery);