function m_hexToArray(t) {
var r = []
, i = t.length;
i % 2 != 0 && (t = h(t, i + 1)),
i = t.length;
for (var e = 0; e < i; e += 2)
r.push(parseInt(t.substr(e, 2), 16));
return r
}
function m_utf8ToHex(t) {
t = unescape(encodeURIComponent(t));
for (var r = t.length, i = [], e = 0; e < r; e++)
i[e >>> 2] |= (255 & t.charCodeAt(e)) << 24 - e % 4 * 8;
for (var n = [], o = 0; o < r; o++) {
var s = i[o >>> 2] >>> 24 - o % 4 * 8 & 255;
n.push((s >>> 4).toString(16)),
n.push((15 & s).toString(16))
}
return n.join("")
}
function h(t, r) {
var vr = [null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,0,1,2,3,4,5,6,7,8,9,null,null,null,null,null,null,null,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,null,null,null,null,null,null,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35];
var i = vr[t.charCodeAt(r)];
return null == i ? -1 : i
}
function m_getGlobalCurve(t, r, i) {
var e = t ? new y(t,r,i) : new y(B.bitLength(),T)
, n = e.mod(B.subtract(y.ONE)).add(y.ONE)
, o = h(n.toString(16), 64)
, s = w.multiply(n);
return {
privateKey: o,
publicKey: "04" + h(s.getX().toBigInteger().toString(16), 64) + h(s.getY().toBigInteger().toString(16), 64)
}
}
function m_getGlobalCurve_decodePointHex(t) {
switch (parseInt(t.substr(0, 2), 16)) {
case 0:
return this.infinity;
case 2:
case 3:
var r = this.fromBigInteger(new s_function(t.substr(2),16))
, i = this.fromBigInteger(r.multiply(r.square()).add(r.multiply(this.a)).add(this.b).toBigInteger().modPow(this.q.divide(new s_function("4")).add(s.ONE), this.q));
return i.toBigInteger().mod(u).equals(new s_function(t.substr(0, 2),16).subtract(u)) || (i = i.negate()),
new f(this,r,i);
case 4:
case 6:
case 7:
var e = (t.length - 2) / 2
, n = t.substr(2, e)
, o = t.substr(e + 2, e);
return new f(this,this.fromBigInteger(new s_function(n,16)),this.fromBigInteger(new s_functions(o,16)));
default:
return null
}
}
function s_function(t, r, i) {
null != t && ("number" == typeof t ? this.fromNumber(t, r, i) : null == r && "string" != typeof t ? fromString(t, 256) : fromString(t, r))
}
function fromString(t, r) {
var e;
if (16 == r)
e = 4;
else if (8 == r)
e = 3;
else if (256 == r)
e = 8;
else if (2 == r)
e = 1;
else if (32 == r)
e = 5;
else {
if (4 != r)
return void this.fromRadix(t, r);
e = 2
}
this.t = 0,
this.s = 0;
for (var n = t.length, o = !1, s = 0; --n >= 0; ) {
var u = 8 == e ? 255 & t[n] : h(t, n);
u < 0 ? "-" == t.charAt(n) && (o = !0) : (o = !1,
0 == s ? this[this.t++] = u : s + e > this.DB ? (this[this.t - 1] |= (u & (1 << this.DB - s) - 1) << s,
this[this.t++] = u >> this.DB - s) : this[this.t - 1] |= u << s,
(s += e) >= this.DB && (s -= this.DB))
}
8 == e && 0 != (128 & t[0]) && (this.s = -1,
s > 0 && (this[this.t - 1] |= (1 << this.DB - s) - 1 << s)),
this.clamp(),
o && i.ZERO.subTo(this, this)
}
f = function() {
function t(r, i, n, o) {
e(this, t),
this.curve = r,
this.x = i,
this.y = n,
this.z = null == o ? s_function : o,
this.zinv = null
}
console.log("---------------")
// return n(t, [{
// key: "getX",
// value: function() {
// return null === this.zinv && (this.zinv = this.z.modInverse(this.curve.q)),
// this.curve.fromBigInteger(this.x.toBigInteger().multiply(this.zinv).mod(this.curve.q))
// }
// }, {
// key: "getY",
// value: function() {
// return null === this.zinv && (this.zinv = this.z.modInverse(this.curve.q)),
// this.curve.fromBigInteger(this.y.toBigInteger().multiply(this.zinv).mod(this.curve.q))
// }
// }, {
// key: "equals",
// value: function(t) {
// return t === this || (this.isInfinity() ? t.isInfinity() : t.isInfinity() ? this.isInfinity() : !!t.y.toBigInteger().multiply(this.z).subtract(this.y.toBigInteger().multiply(t.z)).mod(this.curve.q).equals(s.ZERO) && t.x.toBigInteger().multiply(this.z).subtract(this.x.toBigInteger().multiply(t.z)).mod(this.curve.q).equals(s.ZERO))
// }
// }, {
// key: "isInfinity",
// value: function() {
// return null === this.x && null === this.y || this.z.equals(s.ZERO) && !this.y.toBigInteger().equals(s.ZERO)
// }
// }, {
// key: "negate",
// value: function() {
// return new t(this.curve,this.x,this.y.negate(),this.z)
// }
// }, {
// key: "add",
// value: function(r) {
// if (this.isInfinity())
// return r;
// if (r.isInfinity())
// return this;
// var i = this.x.toBigInteger()
// , e = this.y.toBigInteger()
// , n = this.z
// , o = r.x.toBigInteger()
// , u = r.y.toBigInteger()
// , h = r.z
// , a = this.curve.q
// , f = i.multiply(h).mod(a)
// , l = o.multiply(n).mod(a)
// , c = f.subtract(l)
// , p = e.multiply(h).mod(a)
// , v = u.multiply(n).mod(a)
// , y = p.subtract(v);
// if (s.ZERO.equals(c))
// return s.ZERO.equals(y) ? this.twice() : this.curve.infinity;
// var m = f.add(l)
// , d = n.multiply(h).mod(a)
// , g = c.square().mod(a)
// , T = c.multiply(g).mod(a)
// , F = d.multiply(y.square()).subtract(m.multiply(g)).mod(a)
// , b = c.multiply(F).mod(a)
// , w = y.multiply(g.multiply(f).subtract(F)).subtract(p.multiply(T)).mod(a)
// , B = T.multiply(d).mod(a);
// return new t(this.curve,this.curve.fromBigInteger(b),this.curve.fromBigInteger(w),B)
// }
// }, {
// key: "twice",
// value: function() {
// if (this.isInfinity())
// return this;
// if (!this.y.toBigInteger().signum())
// return this.curve.infinity;
// var r = this.x.toBigInteger()
// , i = this.y.toBigInteger()
// , e = this.z
// , n = this.curve.q
// , o = this.curve.a.toBigInteger()
// , s = r.square().multiply(h).add(o.multiply(e.square())).mod(n)
// , u = i.shiftLeft(1).multiply(e).mod(n)
// , a = i.square().mod(n)
// , f = a.multiply(r).multiply(e).mod(n)
// , l = u.square().mod(n)
// , c = s.square().subtract(f.shiftLeft(3)).mod(n)
// , p = u.multiply(c).mod(n)
// , v = s.multiply(f.shiftLeft(2).subtract(c)).subtract(l.shiftLeft(1).multiply(a)).mod(n)
// , y = u.multiply(l).mod(n);
// return new t(this.curve,this.curve.fromBigInteger(p),this.curve.fromBigInteger(v),y)
// }
// }, {
// key: "multiply",
// value: function(t) {
// if (this.isInfinity())
// return this;
// if (!t.signum())
// return this.curve.infinity;
// for (var r = t.multiply(h), i = this.negate(), e = this, n = r.bitLength() - 2; n > 0; n--) {
// e = e.twice();
// var o = r.testBit(n);
// o !== t.testBit(n) && (e = e.add(o ? this : i))
// }
// return e
// }
// }]),
t
}()
function doEncrypt_sm2(t, r) {
var i = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : 1;
t = "string" == typeof t ? m_hexToArray(m_utf8ToHex(t)) : Array.prototype.slice.call(t),
r = m_getGlobalCurve_decodePointHex(r);
var n = m.generateKeyPairHex()
, o = new c(n.privateKey,16)
, s = n.publicKey;
s.length > 128 && (s = s.substr(s.length - 128));
var u = r.multiply(o)
, h = m_hexToArray(m.leftPad(u.getX().toBigInteger().toRadix(16), 64))
, a = m_hexToArray(m.leftPad(u.getY().toBigInteger().toRadix(16), 64))
, f = m.arrayToHex(d([].concat(h, t, a)))
, l = 1
, p = 0
, v = []
, y = [].concat(h, a)
, g = function() {
v = d([].concat(e(y), [l >> 24 & 255, l >> 16 & 255, l >> 8 & 255, 255 & l])),
l++,
p = 0
};
g();
for (var T = 0, F = t.length; T < F; T++)
p === v.length && g(),
t[T] ^= 255 & v[p++];
var b = m.arrayToHex(t);
return i === w ? s + b + f : s + f + b
}
function doEncrypt (msgString, publicKey) {
let msg = msgString
if (typeof msgString !== 'string') {
msg = JSON.stringify(msgString)
}
// 1 - C1C3C2; 0 - C1C2C3; 默认为1
let cipherMode = 0 // 特别注意,此处前后端需保持一致
// 加密结果
// let encryptData = sm2.doEncrypt(msg, publicKey, cipherMode)
let encryptData = doEncrypt_sm2(msg, publicKey, cipherMode)
// 加密后的密文前需要添加04,后端才能正常解密
return '04' + encryptData
}
var publicKey = "04195D1F93F950DDCC8C8384DD47DEBBD19B2897753686DE6B2EC87B583578325DF9191865258EB22A08AEFE4AA5E0EAD59D0EFB0187B0649EEF9008222BD3DA22";
console.log("doEncrypt: " + doEncrypt("app",publicKey));