jsencrypt参数前端加密c#解密

Posted 坚持不懈,才能出彩

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jsencrypt参数前端加密c#解密相关的知识,希望对你有一定的参考价值。

      写程序时一般是通过form表单或者ajax方式将参数提交到服务器进行验证,如何防止提交的请求不被抓包后串改,虽然无法说绝对安全却给非法提交提高了难度,本篇采用jsencypt在前端进行加密的并且用C#在后端解密,在投票提交分数等H5应用上可以使用的上,并且进行简单的封装。

1.demo

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script src="/Scripts/jquery-1.10.2.min.js"></script>
    <script src="/Scripts/JsEncryptHelper.js"></script>
</head>
<body>

</body>
</html>

<body>
    <form id="form1>
        <div>
            <label>Public Key</label><br />  
            <label for="input">Text to encrypt:</label><br />
            name:<input id="username" name="username" type="text"></input><br />
            password:<input id="passwd" name="passwd" type="password"></input><br />
            <input id="testme" type="button" value="submit" /><br />
        </div>
    </form>
</body>

<script>
    $(function () {

        $(\'#testme\').click(function () {
         
            console.log("开始发送数据请求");
            var data = {};
            data.username = $(\'#username\').val();
            data.passwd = $(\'#passwd\').val();

            console.log("加密前数据:", JSON.stringify(data));
            var result = $.encryptRequest({
                data: data
            });
            console.log("加密后数据:", JSON.stringify(result));
            $.ajax({
                url: \'/Yhz/TestDecrypt\',
                dataType: \'jsonp\',
                type:\'post\',
                data: result,
                success: function (res)
                {
                    console.log("服务端解密成功:", JSON.stringify(res));
                    
                }
            })

        });
    });
</script>

 

输入账号密码后加密后到服务端解密后返回

 

2.客户端

写一个JsEncryptHelper.js  以后需要加密的页面都可以应用

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 - 1; c >= 0; --c) b[c] = 0; b[g] = h, b.t = this.t + g + 1, b.s = this.s, b.clamp() } function bnpRShiftTo(a, b) { b.s = this.s; var c = Math.floor(a / this.DB); if (c >= this.t) return void (b.t = 0); var d = a % this.DB, e = this.DB - d, f = (1 << d) - 1; b[0] = this[c] >> d; for (var g = c + 1; g < this.t; ++g) b[g - c - 1] |= (this[g] & f) << e, b[g - c] = this[g] >> d; d > 0 && (b[this.t - c - 1] |= (this.s & f) << e), b.t = this.t - c, b.clamp() } function bnpSubTo(a, b) { for (var c = 0, d = 0, e = Math.min(a.t, this.t) ; e > c;) d += this[c] - a[c], b[c++] = d & this.DM, d >>= this.DB; if (a.t < this.t) { for (d -= a.s; c < this.t;) d += this[c], b[c++] = d & this.DM, d >>= this.DB; d += this.s } else { for (d += this.s; c < a.t;) d -= a[c], b[c++] = d & this.DM, d >>= this.DB; d -= a.s } b.s = 0 > d ? -1 : 0, -1 > d ? b[c++] = this.DV + d : d > 0 && (b[c++] = d), b.t = c, b.clamp() } function bnpMultiplyTo(a, b) { var c = this.abs(), d = a.abs(), e = c.t; for (b.t = e + d.t; --e >= 0;) b[e] = 0; for (e = 0; e < d.t; ++e) b[e + c.t] = c.am(0, d[e], b, e, 0, c.t); b.s = 0, b.clamp(), this.s != a.s && BigInteger.ZERO.subTo(b, b) } function bnpSquareTo(a) { for (var b = this.abs(), c = a.t = 2 * b.t; --c >= 0;) a[c] = 0; for (c = 0; c < b.t - 1; ++c) { var d = b.am(c, b[c], a, 2 * c, 0, 1); (a[c + b.t] += b.am(c + 1, 2 * b[c], a, 2 * c + 1, d, b.t - c - 1)) >= b.DV && (a[c + b.t] -= b.DV, a[c + b.t + 1] = 1) } a.t > 0 && (a[a.t - 1] += b.am(c, b[c], a, 2 * c, 0, 1)), a.s = 0, a.clamp() } function bnpDivRemTo(a, b, c) { var d = a.abs(); if (!(d.t <= 0)) { var e = this.abs(); if (e.t < d.t) return null != b && b.fromInt(0), void (null != c && this.copyTo(c)); null == c && (c = nbi()); var f = nbi(), g = this.s, h = a.s, i = this.DB - nbits(d[d.t - 1]); i > 0 ? (d.lShiftTo(i, f), e.lShiftTo(i, c)) : (d.copyTo(f), e.copyTo(c)); var j = f.t, k = f[j - 1]; if (0 != k) { var l = k * (1 << this.F1) + (j > 1 ? f[j - 2] >> this.F2 : 0), m = this.FV / l, n = (1 << this.F1) / l, o = 1 << this.F2, p = c.t, q = p - j, r = null == b ? nbi() : b; for (f.dlShiftTo(q, r), c.compareTo(r) >= 0 && (c[c.t++] = 1, c.subTo(r, c)), BigInteger.ONE.dlShiftTo(j, r), r.subTo(f, f) ; f.t < j;) f[f.t++] = 0; for (; --q >= 0;) { var s = c[--p] == k ? this.DM : Math.floor(c[p] * m + (c[p - 1] + o) * n); if ((c[p] += f.am(0, s, c, q, 0, j)) < s) for (f.dlShiftTo(q, r), c.subTo(r, c) ; c[p] < --s;) c.subTo(r, c) } null != b && (c.drShiftTo(j, b), g != h && BigInteger.ZERO.subTo(b, b)), c.t = j, c.clamp(), i > 0 && c.rShiftTo(i, c), 0 > g && BigInteger.ZERO.subTo(c, c) } } } function bnMod(a) { var b = nbi(); return this.abs().divRemTo(a, null, b), this.s < 0 && b.compareTo(BigInteger.ZERO) > 0 && a.subTo(b, b), b } function Classic(a) { this.m = a } function cConvert(a) { return a.s < 0 || a.compareTo(this.m) >= 0 ? a.mod(this.m) : a } function cRevert(a) { return a } function cReduce(a) { a.divRemTo(this.m, null, a) } function cMulTo(a, b, c) { a.multiplyTo(b, c), this.reduce(c) } function cSqrTo(a, b) { a.squareTo(b), this.reduce(b) } function bnpInvDigit() { if (this.t < 1) return 0; var a = this[0]; if (0 == (1 & a)) return 0; var b = 3 & a; return b = b * (2 - (15 & a) * b) & 15, b = b * (2 - (255 & a) * b) & 255, b = b * (2 - ((65535 & a) * b & 65535)) & 65535, b = b * (2 - a * b % this.DV) % this.DV, b > 0 ? this.DV - b : -b } function Montgomery(a) { this.m = a, this.mp = a.invDigit(), this.mpl = 32767 & this.mp, this.mph = this.mp >> 15, this.um = (1 << a.DB - 15) - 1, this.mt2 = 2 * a.t } function montConvert(a) { var b = nbi(); return a.abs().dlShiftTo(this.m.t, b), b.divRemTo(this.m, null, b), a.s < 0 && b.compareTo(BigInteger.ZERO) > 0 && this.m.subTo(b, b), b } function montRevert(a) { var b = nbi(); return a.copyTo(b), this.reduce(b), b } function montReduce(a) { for (; a.t <= this.mt2;) a[a.t++] = 0; for (var b = 0; b < this.m.t; ++b) { var c = 32767 & a[b], d = c * this.mpl + ((c * this.mph + (a[b] >> 15) * this.mpl & this.um) << 15) & a.DM; for (c = b + this.m.t, a[c] += this.m.am(0, d, a, b, 0, this.m.t) ; a[c] >= a.DV;) a[c] -= a.DV, a[++c]++ } a.clamp(), a.drShiftTo(this.m.t, a), a.compareTo(this.m) >= 0 && a.subTo(this.m, a) } function montSqrTo(a, b) { a.squareTo(b), this.reduce(b) } function montMulTo(a, b, c) { a.multiplyTo(b, c), this.reduce(c) } function bnpIsEven() { return 0 == (this.t > 0 ? 1 & this[0] : this.s) } function bnpExp(a, b) { if (a > 4294967295 || 1 > a) return BigInteger.ONE; var c = nbi(), d = nbi(), e = b.convert(this), f = nbits(a) - 1; for (e.copyTo(c) ; --f >= 0;) if (b.sqrTo(c, d), (a & 1 << f) > 0) b.mulTo(d, e, c); else { var g = c; c = d, d = g } return b.revert(c) } function bnModPowInt(a, b) { var c; return c = 256 > a || b.isEven() ? new Classic(b) : new Montgomery(b), this.exp(a, c) } function bnClone() { var a = nbi(); return this.copyTo(a), a } function bnIntValue() { if (this.s < 0) { if (1 == this.t) return this[0] - this.DV; if (0 == this.t) return -1 } else { if (1 == this.t) return this[0]; if (0 == this.t) return 0 } return (this[1] & (1 << 32 - this.DB) - 1) << this.DB | this[0] } function bnByteValue() { return 0 == this.t ? this.s : this[0] << 24 >> 24 } function bnShortValue() { return 0 == this.t ? this.s : this[0] << 16 >> 16 } function bnpChunkSize(a) { return Math.floor(Math.LN2 * this.DB / Math.log(a)) } function bnSigNum() { return this.s < 0 ? -1 : this.t <= 0 || 1 == this.t && this[0] <= 0 ? 0 : 1 } function bnpToRadix(a) { if (null == a && (a = 10), 0 == this.signum() || 2 > a || a > 36) return "0"; var b = this.chunkSize(a), c = Math.pow(a, b), d = nbv(c), e = nbi(), f = nbi(), g = ""; for (this.divRemTo(d, e, f) ; e.signum() > 0;) g = (c + f.intValue()).toString(a).substr(1) + g, e.divRemTo(d, e, f); return f.intValue().toString(a) + g } function bnpFromRadix(a, b) { this.fromInt(0), null == b && (b = 10); for (var c = this.chunkSize(b), d = Math.pow(b, c), e = !1, f = 0, g = 0, h = 0; h < a.length; ++h) { var i = intAt(a, h); 0 > i ? "-" == a.charAt(h) && 0 == this.signum() && (e = !0) : (g = b * g + i, ++f >= c && (this.dMultiply(d), this.dAddOffset(g, 0), f = 0, g = 0)) } f > 0 && (this.dMultiply(Math.pow(b, f)), this.dAddOffset(g, 0)), e && BigInteger.ZERO.subTo(this, this) } function bnpFromNumber(a, b, c) { if ("number" == typeof b) if (2 > a) this.fromInt(1); else for (this.fromNumber(a, c), this.testBit(a - 1) || this.bitwiseTo(BigInteger.ONE.shiftLeft(a - 1), op_or, this), this.isEven() && this.dAddOffset(1, 0) ; !this.isProbablePrime(b) ;) this.dAddOffset(2, 0), this.bitLength() > a && this.subTo(BigInteger.ONE.shiftLeft(a - 1), this); else { var d = new Array, e = 7 & a; d.length = (a >> 3) + 1, b.nextBytes(d), e > 0 ? d[0] &= (1 << e) - 1 : d[0] = 0, this.fromString(d, 256) } } function bnToByteArray() { var a = this.t, b = new Array; b[0] = this.s; var c, d = this.DB - a * this.DB % 8, e = 0; if (a-- > 0) for (d < this.DB && (c = this[a] >> d) != (this.s & this.DM) >> d && (b[e++] = c | this.s << this.DB - d) ; a >= 0;) 8 > d ? (c = (this[a] & (1 << d) - 1) << 8 - d, c |= this[--a] >> (d += this.DB - 8)) : (c = this[a] >> (d -= 8) & 255, 0 >= d && (d += this.DB, --a)), 0 != (128 & c) && (c |= -256), 0 == e && (128 & this.s) != (128 & c) && ++e, (e > 0 || c != this.s) && (b[e++] = c); return b } function bnEquals(a) { return 0 == this.compareTo(a) } function bnMin(a) { return this.compareTo(a) < 0 ? this : a } function bnMax(a) { return this.compareTo(a) > 0 ? this : a } function bnpBitwiseTo(a, b, c) { var d, e, f = Math.min(a.t, this.t); for (d = 0; f > d; ++d) c[d] = b(this[d], a[d]); if (a.t < this.t) { for (e = a.s & this.DM, d = f; d < this.t; ++d) c[d] = b(this[d], e); c.t = this.t } else { for (e = this.s & this.DM, d = f; d < a.t; ++d) c[d] = b(e, a[d]); c.t = a.t } c.s = b(this.s, a.s), c.clamp() } function op_and(a, b) { return a & b } function bnAnd(a) { var b = nbi(); return this.bitwiseTo(a, op_and, b), b } function op_or(a, b) { return a | b } function bnOr(a) { var b = nbi(); return this.bitwiseTo(a, op_or, b), b } function op_xor(a, b) { return a ^ b } function bnXor(a) { var b = nbi(); return this.bitwiseTo(a, op_xor, b), b } function op_andnot(a, b) { return a & ~b } function bnAndNot(a) { var b = nbi(); return this.bitwiseTo(a, op_andnot, b), b } function bnNot() { for (var a = nbi(), b = 0; b < this.t; ++b) a[b] = this.DM & ~this[b]; return a.t = this.t, a.s = ~this.s, a } function bnShiftLeft(a) { var b = nbi(); return 0 > a ? this.rShiftTo(-a, b) : this.lShiftTo(a, b), b } function bnShiftRight(a) { var b = nbi(); return 0 > a ? this.lShiftTo(-a, b) : this.rShiftTo(a, b), b } function lbit(a) { if (0 == a) return -1; var b = 0; return 0 == (65535 & a) && (a >>= 16, b += 16), 0 == (255 & a) && (a >>= 8, b += 8), 0 == (15 & a) && (a >>= 4, b += 4), 0 == (3 & a) && (a >>= 2, b += 2), 0 == (1 & a) && ++b, b } function bnGetLowestSetBit() { for (var a = 0; a < this.t; ++a) if (0 != this[a]) return a * this.DB + lbit(this[a]); return this.s < 0 ? this.t * this.DB : -1 } function cbit(a) { for (var b = 0; 0 != a;) a &= a - 1, ++b; return b } function bnBitCount() { for (var a = 0, b = this.s & this.DM, c = 0; c < this.t; ++c) a += cbit(this[c] ^ b); return a } function bnTestBit(a) { var b = Math.floor(a / this.DB); return b >= this.t ? 0 != this.s : 0 != (this[b] & 1 << a % this.DB) } function bnpChangeBit(a, b) { var c = BigInteger.ONE.shiftLeft(a); return this.bitwiseTo(c, b, c), c } function bnSetBit(a) { return this.changeBit(a, op_or) } function bnClearBit(a) { return this.changeBit(a, op_andnot) } function bnFlipBit(a) { return this.changeBit(a, op_xor) } function bnpAddTo(a, b) { for (var c = 0, d = 0, e = Math.min(a.t, this.t) ; e > c;) d += this[c] + a[c], b[c++] = d & this.DM, d >>= this.DB; if (a.t < this.t) { for (d += a.s; c < this.t;) d += this[c], b[c++] = d & this.DM, d >>= this.DB; d += this.s } else { for (d += this.s; c < a.t;) d += a[c], b[c++] = d & this.DM, d >>= this.DB; d += a.s } b.s = 0 > d ? -1 : 0, d > 0 ? b[c++] = d : -1 > d && (b[c++] = this.DV + d), b.t = c, b.clamp() } function bnAdd(a) { var b = nbi(); return this.addTo(a, b), b } function bnSubtract(a) { var b = nbi(); return this.subTo(a, b), b } function bnMultiply(a) { var b = nbi(); return this.multiplyTo(a, b), b } function bnSquare() { var a = nbi(); return this.squareTo(a), a } function bnDivide(a) { var b = nbi(); return this.divRemTo(a, b, null), b } function bnRemainder(a) { var b = nbi(); return this.divRemTo(a, null, b), b } function bnDivideAndRemainder(a) { var b = nbi(), c = nbi(); return this.divRemTo(a, b, c), new Array(b, c) } function bnpDMultiply(a) { this[this.t] = this.am(0, a - 1, this, 0, 0, this.t), ++this.t, this.clamp() } function bnpDAddOffset(a, b) { if (0 != a) { for (; this.t <= b;) this[this.t++] = 0; for (this[b] += a; this[b] >= this.DV;) this[b] -= this.DV, ++b >= this.t && (this[this.t++] = 0), ++this[b] } } function NullExp() { } function nNop(a) { return a } function nMulTo(a, b, c) { a.multiplyTo(b, c) } function nSqrTo(a, b) { a.squareTo(b) } function bnPow(a) { return this.exp(a, new NullExp) } function bnpMultiplyLowerTo(a, b, c) { var d = Math.min(this.t + a.t, b); for (c.s = 0, c.t = d; d > 0;) c[--d] = 0; var e; for (e = c.t - this.t; e > d; ++d) c[d + this.t] = this.am(0, a[d], c, d, 0, this.t); for (e = Math.min(a.t, b) ; e > d; ++d) this.am(0, a[d], c, d, 0, b - d); c.clamp() } function bnpMultiplyUpperTo(a, b, c) { --b; var d = c.t = this.t + a.t - b; for (c.s = 0; --d >= 0;) c[d] = 0; for (d = Math.max(b - this.t, 0) ; d < a.t; ++d) c[this.t + d - b] = this.am(b - d, a[d], c, 0, 0, this.t + d - b); c.clamp(), c.drShiftTo(1, c) } function Barrett(a) { this.r2 = nbi(), this.q3 = nbi(), BigInteger.ONE.dlShiftTo(2 * a.t, this.r2), this.mu = this.r2.divide(a), this.m = a } function barrettConvert(a) { if (a.s < 0 || a.t >

以上是关于jsencrypt参数前端加密c#解密的主要内容,如果未能解决你的问题,请参考以下文章

基于C# 的RSA 前端JS加密后端进行解密。

jsencrypt实现前端RSA非对称加密解密(vue项目)

jsencrypt.js加密java后端解密

jsencrypt.js加密java后端解密

VUE内使用RSA加解密

使用JSEncrypt加密解密