js实现AES加密解密功能(简易又全面)
更新时间:2024年05月07日 11:14:59 作者:胡桃夹夹子
在项目中传输数据,可通过对请求数据或响应数据进行加密,防止信息泄露,下面这篇文章主要给大家介绍了js实现AES加密解密功能的相关资料,需要的朋友可以参考下
常规是直接安装CryptoJS库,但为了减少项目体积,使用这简单的20k文件就ok
一览:
代码中使用的是Pkcs7,但我需要的填充方式是ZeroPadding
所以稍微有修改:
q = (p.pad = {}).ZeroPadding = { pad: function (data, blockSize) { var blockSizeBytes = blockSize * 4 data.clamp() data.sigBytes += blockSizeBytes - ((data.sigBytes % blockSizeBytes) || blockSizeBytes) }, unpad: function (data) { // Shortcut var dataWords = data.words; // Unpad var i = data.sigBytes - 1; for (var i = data.sigBytes - 1; i >= 0; i--) { if (((dataWords[i >>> 2] >>> (24 - (i % 4) * 8)) & 255)) { data.sigBytes = i + 1; break; } } } }
完整代码如下:
var CryptoJS = CryptoJS || (function (u, p) { const d = {} const l = d.lib = {} const s = function () {} const t = l.Base = { extend: function (a) { s.prototype = this const c = new s() a && c.mixIn(a) c.hasOwnProperty('init') || (c.init = function () { c.$super.init.apply(this, arguments) }) c.init.prototype = c c.$super = this return c }, create: function () { const a = this.extend() a.init.apply(a, arguments) return a }, init: function () {}, mixIn: function (a) { for (const c in a) a.hasOwnProperty(c) && (this[c] = a[c]) a.hasOwnProperty('toString') && (this.toString = a.toString) }, clone: function () { return this.init.prototype.extend(this) } } var r = l.WordArray = t.extend({ init: function (a, c) { a = this.words = a || [] this.sigBytes = c != p ? c : 4 * a.length }, toString: function (a) { return (a || v).stringify(this) }, concat: function (a) { const c = this.words const e = a.words const j = this.sigBytes a = a.sigBytes this.clamp() if (j % 4) { for (var k = 0; k < a; k++) c[j + k >>> 2] |= (e[k >>> 2] >>> 24 - 8 * (k % 4) & 255) << 24 - 8 * ((j + k) % 4) } else if (e.length > 65535) { for (k = 0; k < a; k += 4) c[j + k >>> 2] = e[k >>> 2] } else c.push.apply(c, e) this.sigBytes += a return this }, clamp: function () { const a = this.words const c = this.sigBytes a[c >>> 2] &= 4294967295 << 32 - 8 * (c % 4) a.length = u.ceil(c / 4) }, clone: function () { const a = t.clone.call(this) a.words = this.words.slice(0) return a }, random: function (a) { for (var c = [], e = 0; e < a; e += 4) c.push(4294967296 * u.random() | 0) return new r.init(c, a) } }) const w = d.enc = {} var v = w.Hex = { stringify: function (a) { const c = a.words a = a.sigBytes for (var e = [], j = 0; j < a; j++) { const k = c[j >>> 2] >>> 24 - 8 * (j % 4) & 255 e.push((k >>> 4).toString(16)) e.push((k & 15).toString(16)) } return e.join('') }, parse: function (a) { for (var c = a.length, e = [], j = 0; j < c; j += 2) { e[j >>> 3] |= parseInt(a.substr(j, 2), 16) << 24 - 4 * (j % 8) } return new r.init(e, c / 2) } } const b = w.Latin1 = { stringify: function (a) { const c = a.words a = a.sigBytes for (var e = [], j = 0; j < a; j++) e.push(String.fromCharCode(c[j >>> 2] >>> 24 - 8 * (j % 4) & 255)) return e.join('') }, parse: function (a) { for (var c = a.length, e = [], j = 0; j < c; j++) e[j >>> 2] |= (a.charCodeAt(j) & 255) << 24 - 8 * (j % 4) return new r.init(e, c) } } const x = w.Utf8 = { stringify: function (a) { try { return decodeURIComponent(escape(b.stringify(a))) } catch (c) { throw Error('Malformed UTF-8 data') } }, parse: function (a) { return b.parse(unescape(encodeURIComponent(a))) } } const q = l.BufferedBlockAlgorithm = t.extend({ reset: function () { this._data = new r.init() this._nDataBytes = 0 }, _append: function (a) { typeof a == 'string' && (a = x.parse(a)) this._data.concat(a) this._nDataBytes += a.sigBytes }, _process: function (a) { const c = this._data const e = c.words let j = c.sigBytes const k = this.blockSize var b = j / (4 * k) var b = a ? u.ceil(b) : u.max((b | 0) - this._minBufferSize, 0) a = b * k j = u.min(4 * a, j) if (a) { for (var q = 0; q < a; q += k) this._doProcessBlock(e, q) q = e.splice(0, a) c.sigBytes -= j } return new r.init(q, j) }, clone: function () { const a = t.clone.call(this) a._data = this._data.clone() return a }, _minBufferSize: 0 }) l.Hasher = q.extend({ cfg: t.extend(), init: function (a) { this.cfg = this.cfg.extend(a) this.reset() }, reset: function () { q.reset.call(this) this._doReset() }, update: function (a) { this._append(a) this._process() return this }, finalize: function (a) { a && this._append(a) return this._doFinalize() }, blockSize: 16, _createHelper: function (a) { return function (b, e) { return (new a.init(e)).finalize(b) } }, _createHmacHelper: function (a) { return function (b, e) { return (new n.HMAC.init(a, e)).finalize(b) } } }) var n = d.algo = {} return d }(Math)); (function () { const u = CryptoJS const p = u.lib.WordArray u.enc.Base64 = { stringify: function (d) { let l = d.words const p = d.sigBytes const t = this._map d.clamp() d = [] for (let r = 0; r < p; r += 3) { for (let w = (l[r >>> 2] >>> 24 - 8 * (r % 4) & 255) << 16 | (l[r + 1 >>> 2] >>> 24 - 8 * ((r + 1) % 4) & 255) << 8 | l[r + 2 >>> 2] >>> 24 - 8 * ((r + 2) % 4) & 255, v = 0; v < 4 && r + 0.75 * v < p; v++) d.push(t.charAt(w >>> 6 * (3 - v) & 63)) } if (l = t.charAt(64)) { for (; d.length % 4;) d.push(l) } return d.join('') }, parse: function (d) { let l = d.length const s = this._map var t = s.charAt(64) t && (t = d.indexOf(t), t != -1 && (l = t)) for (var t = [], r = 0, w = 0; w < l; w++) { if (w % 4) { const v = s.indexOf(d.charAt(w - 1)) << 2 * (w % 4) const b = s.indexOf(d.charAt(w)) >>> 6 - 2 * (w % 4) t[r >>> 2] |= (v | b) << 24 - 8 * (r % 4) r++ } } return p.create(t, r) }, _map: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=' } })(); (function (u) { function p (b, n, a, c, e, j, k) { b = b + (n & a | ~n & c) + e + k return (b << j | b >>> 32 - j) + n } function d (b, n, a, c, e, j, k) { b = b + (n & c | a & ~c) + e + k return (b << j | b >>> 32 - j) + n } function l (b, n, a, c, e, j, k) { b = b + (n ^ a ^ c) + e + k return (b << j | b >>> 32 - j) + n } function s (b, n, a, c, e, j, k) { b = b + (a ^ (n | ~c)) + e + k return (b << j | b >>> 32 - j) + n } for (var t = CryptoJS, r = t.lib, w = r.WordArray, v = r.Hasher, r = t.algo, b = [], x = 0; x < 64; x++) b[x] = 4294967296 * u.abs(u.sin(x + 1)) | 0 r = r.MD5 = v.extend({ _doReset: function () { this._hash = new w.init([1732584193, 4023233417, 2562383102, 271733878]) }, _doProcessBlock: function (q, n) { for (var a = 0; a < 16; a++) { var c = n + a var e = q[c] q[c] = (e << 8 | e >>> 24) & 16711935 | (e << 24 | e >>> 8) & 4278255360 } var a = this._hash.words var c = q[n + 0] var e = q[n + 1] const j = q[n + 2] const k = q[n + 3] const z = q[n + 4] const r = q[n + 5] const t = q[n + 6] const w = q[n + 7] const v = q[n + 8] const A = q[n + 9] const B = q[n + 10] const C = q[n + 11] const u = q[n + 12] const D = q[n + 13] const E = q[n + 14] const x = q[n + 15] var f = a[0] var m = a[1] var g = a[2] var h = a[3] var f = p(f, m, g, h, c, 7, b[0]) var h = p(h, f, m, g, e, 12, b[1]) var g = p(g, h, f, m, j, 17, b[2]) var m = p(m, g, h, f, k, 22, b[3]) var f = p(f, m, g, h, z, 7, b[4]) var h = p(h, f, m, g, r, 12, b[5]) var g = p(g, h, f, m, t, 17, b[6]) var m = p(m, g, h, f, w, 22, b[7]) var f = p(f, m, g, h, v, 7, b[8]) var h = p(h, f, m, g, A, 12, b[9]) var g = p(g, h, f, m, B, 17, b[10]) var m = p(m, g, h, f, C, 22, b[11]) var f = p(f, m, g, h, u, 7, b[12]) var h = p(h, f, m, g, D, 12, b[13]) var g = p(g, h, f, m, E, 17, b[14]) var m = p(m, g, h, f, x, 22, b[15]) var f = d(f, m, g, h, e, 5, b[16]) var h = d(h, f, m, g, t, 9, b[17]) var g = d(g, h, f, m, C, 14, b[18]) var m = d(m, g, h, f, c, 20, b[19]) var f = d(f, m, g, h, r, 5, b[20]) var h = d(h, f, m, g, B, 9, b[21]) var g = d(g, h, f, m, x, 14, b[22]) var m = d(m, g, h, f, z, 20, b[23]) var f = d(f, m, g, h, A, 5, b[24]) var h = d(h, f, m, g, E, 9, b[25]) var g = d(g, h, f, m, k, 14, b[26]) var m = d(m, g, h, f, v, 20, b[27]) var f = d(f, m, g, h, D, 5, b[28]) var h = d(h, f, m, g, j, 9, b[29]) var g = d(g, h, f, m, w, 14, b[30]) var m = d(m, g, h, f, u, 20, b[31]) var f = l(f, m, g, h, r, 4, b[32]) var h = l(h, f, m, g, v, 11, b[33]) var g = l(g, h, f, m, C, 16, b[34]) var m = l(m, g, h, f, E, 23, b[35]) var f = l(f, m, g, h, e, 4, b[36]) var h = l(h, f, m, g, z, 11, b[37]) var g = l(g, h, f, m, w, 16, b[38]) var m = l(m, g, h, f, B, 23, b[39]) var f = l(f, m, g, h, D, 4, b[40]) var h = l(h, f, m, g, c, 11, b[41]) var g = l(g, h, f, m, k, 16, b[42]) var m = l(m, g, h, f, t, 23, b[43]) var f = l(f, m, g, h, A, 4, b[44]) var h = l(h, f, m, g, u, 11, b[45]) var g = l(g, h, f, m, x, 16, b[46]) var m = l(m, g, h, f, j, 23, b[47]) var f = s(f, m, g, h, c, 6, b[48]) var h = s(h, f, m, g, w, 10, b[49]) var g = s(g, h, f, m, E, 15, b[50]) var m = s(m, g, h, f, r, 21, b[51]) var f = s(f, m, g, h, u, 6, b[52]) var h = s(h, f, m, g, k, 10, b[53]) var g = s(g, h, f, m, B, 15, b[54]) var m = s(m, g, h, f, e, 21, b[55]) var f = s(f, m, g, h, v, 6, b[56]) var h = s(h, f, m, g, x, 10, b[57]) var g = s(g, h, f, m, t, 15, b[58]) var m = s(m, g, h, f, D, 21, b[59]) var f = s(f, m, g, h, z, 6, b[60]) var h = s(h, f, m, g, C, 10, b[61]) var g = s(g, h, f, m, j, 15, b[62]) var m = s(m, g, h, f, A, 21, b[63]) a[0] = a[0] + f | 0 a[1] = a[1] + m | 0 a[2] = a[2] + g | 0 a[3] = a[3] + h | 0 }, _doFinalize: function () { let b = this._data let n = b.words let a = 8 * this._nDataBytes let c = 8 * b.sigBytes n[c >>> 5] |= 128 << 24 - c % 32 const e = u.floor(a / 4294967296) n[(c + 64 >>> 9 << 4) + 15] = (e << 8 | e >>> 24) & 16711935 | (e << 24 | e >>> 8) & 4278255360 n[(c + 64 >>> 9 << 4) + 14] = (a << 8 | a >>> 24) & 16711935 | (a << 24 | a >>> 8) & 4278255360 b.sigBytes = 4 * (n.length + 1) this._process() b = this._hash n = b.words for (a = 0; a < 4; a++) c = n[a], n[a] = (c << 8 | c >>> 24) & 16711935 | (c << 24 | c >>> 8) & 4278255360 return b }, clone: function () { const b = v.clone.call(this) b._hash = this._hash.clone() return b } }) t.MD5 = v._createHelper(r) t.HmacMD5 = v._createHmacHelper(r) })(Math); (function () { const u = CryptoJS var p = u.lib const d = p.Base const l = p.WordArray var p = u.algo const s = p.EvpKDF = d.extend({ cfg: d.extend({ keySize: 4, hasher: p.MD5, iterations: 1 }), init: function (d) { this.cfg = this.cfg.extend(d) }, compute: function (d, r) { for (var p = this.cfg, s = p.hasher.create(), b = l.create(), u = b.words, q = p.keySize, p = p.iterations; u.length < q;) { n && s.update(n) var n = s.update(d).finalize(r) s.reset() for (let a = 1; a < p; a++) n = s.finalize(n), s.reset() b.concat(n) } b.sigBytes = 4 * q return b } }) u.EvpKDF = function (d, l, p) { return s.create(p).compute(d, l) } })() CryptoJS.lib.Cipher || (function (u) { var p = CryptoJS const d = p.lib const l = d.Base const s = d.WordArray const t = d.BufferedBlockAlgorithm const r = p.enc.Base64 const w = p.algo.EvpKDF const v = d.Cipher = t.extend({ cfg: l.extend(), createEncryptor: function (e, a) { return this.create(this._ENC_XFORM_MODE, e, a) }, createDecryptor: function (e, a) { return this.create(this._DEC_XFORM_MODE, e, a) }, init: function (e, a, b) { this.cfg = this.cfg.extend(b) this._xformMode = e this._key = a this.reset() }, reset: function () { t.reset.call(this) this._doReset() }, process: function (e) { this._append(e) return this._process() }, finalize: function (e) { e && this._append(e) return this._doFinalize() }, keySize: 4, ivSize: 4, _ENC_XFORM_MODE: 1, _DEC_XFORM_MODE: 2, _createHelper: function (e) { return { encrypt: function (b, k, d) { return (typeof k == 'string' ? c : a).encrypt(e, b, k, d) }, decrypt: function (b, k, d) { return (typeof k == 'string' ? c : a).decrypt(e, b, k, d) } } } }) d.StreamCipher = v.extend({ _doFinalize: function () { return this._process(!0) }, blockSize: 1 }) var b = p.mode = {} const x = function (e, a, b) { let c = this._iv c ? this._iv = u : c = this._prevBlock for (let d = 0; d < b; d++) { e[a + d] ^= c[d] } } let q = (d.BlockCipherMode = l.extend({ createEncryptor: function (e, a) { return this.Encryptor.create(e, a) }, createDecryptor: function (e, a) { return this.Decryptor.create(e, a) }, init: function (e, a) { this._cipher = e this._iv = a } })).extend() q.Encryptor = q.extend({ processBlock: function (e, a) { const b = this._cipher const c = b.blockSize x.call(this, e, a, c) b.encryptBlock(e, a) this._prevBlock = e.slice(a, a + c) } }) q.Decryptor = q.extend({ processBlock: function (e, a) { const b = this._cipher const c = b.blockSize const d = e.slice(a, a + c) b.decryptBlock(e, a) x.call(this, e, a, c) this._prevBlock = d } }) b = b.CBC = q q = (p.pad = {}).Pkcs7 = { pad: function (a, b) { for (var c = 4 * b, c = c - a.sigBytes % c, d = c << 24 | c << 16 | c << 8 | c, l = [], n = 0; n < c; n += 4) l.push(d) c = s.create(l, c) a.concat(c) }, unpad: function (a) { a.sigBytes -= a.words[a.sigBytes - 1 >>> 2] & 255 } } d.BlockCipher = v.extend({ cfg: v.cfg.extend({ mode: b, padding: q }), reset: function () { v.reset.call(this) var a = this.cfg const b = a.iv var a = a.mode if (this._xformMode == this._ENC_XFORM_MODE) var c = a.createEncryptor else c = a.createDecryptor, this._minBufferSize = 1 this._mode = c.call(a, this, b && b.words) }, _doProcessBlock: function (a, b) { this._mode.processBlock(a, b) }, _doFinalize: function () { const a = this.cfg.padding if (this._xformMode == this._ENC_XFORM_MODE) { a.pad(this._data, this.blockSize) var b = this._process(!0) } else b = this._process(!0), a.unpad(b) return b }, blockSize: 4 }) const n = d.CipherParams = l.extend({ init: function (a) { this.mixIn(a) }, toString: function (a) { return (a || this.formatter).stringify(this) } }) var b = (p.format = {}).OpenSSL = { stringify: function (a) { const b = a.ciphertext a = a.salt return (a ? s.create([1398893684, 1701076831 ]).concat(a).concat(b) : b).toString(r) }, parse: function (a) { a = r.parse(a) const b = a.words if (b[0] == 1398893684 && b[1] == 1701076831) { var c = s.create(b.slice(2, 4)) b.splice(0, 4) a.sigBytes -= 16 } return n.create({ ciphertext: a, salt: c }) } } var a = d.SerializableCipher = l.extend({ cfg: l.extend({ format: b }), encrypt: function (a, b, c, d) { d = this.cfg.extend(d) let l = a.createEncryptor(c, d) b = l.finalize(b) l = l.cfg return n.create({ ciphertext: b, key: c, iv: l.iv, algorithm: a, mode: l.mode, padding: l.padding, blockSize: a.blockSize, formatter: d.format }) }, decrypt: function (a, b, c, d) { d = this.cfg.extend(d) b = this._parse(b, d.format) return a.createDecryptor(c, d).finalize(b.ciphertext) }, _parse: function (a, b) { return typeof a == 'string' ? b.parse(a, this) : a } }) var p = (p.kdf = {}).OpenSSL = { execute: function (a, b, c, d) { d || (d = s.random(8)) a = w.create({ keySize: b + c }).compute(a, d) c = s.create(a.words.slice(b), 4 * c) a.sigBytes = 4 * b return n.create({ key: a, iv: c, salt: d }) } } var c = d.PasswordBasedCipher = a.extend({ cfg: a.cfg.extend({ kdf: p }), encrypt: function (b, c, d, l) { l = this.cfg.extend(l) d = l.kdf.execute(d, b.keySize, b.ivSize) l.iv = d.iv b = a.encrypt.call(this, b, c, d.key, l) b.mixIn(d) return b }, decrypt: function (b, c, d, l) { l = this.cfg.extend(l) c = this._parse(c, l.format) d = l.kdf.execute(d, b.keySize, b.ivSize, c.salt) l.iv = d.iv return a.decrypt.call(this, b, c, d.key, l) } }) }()); (function () { for (var u = CryptoJS, p = u.lib.BlockCipher, d = u.algo, l = [], s = [], t = [], r = [], w = [], v = [], b = [], x = [], q = [], n = [], a = [], c = 0; c < 256; c++) a[c] = c < 128 ? c << 1 : c << 1 ^ 283 for (var e = 0, j = 0, c = 0; c < 256; c++) { var k = j ^ j << 1 ^ j << 2 ^ j << 3 ^ j << 4 var k = k >>> 8 ^ k & 255 ^ 99 l[e] = k s[k] = e const z = a[e] const F = a[z] const G = a[F] let y = 257 * a[k] ^ 16843008 * k t[e] = y << 24 | y >>> 8 r[e] = y << 16 | y >>> 16 w[e] = y << 8 | y >>> 24 v[e] = y y = 16843009 * G ^ 65537 * F ^ 257 * z ^ 16843008 * e b[k] = y << 24 | y >>> 8 x[k] = y << 16 | y >>> 16 q[k] = y << 8 | y >>> 24 n[k] = y e ? (e = z ^ a[a[a[G ^ z]]], j ^= a[a[j]]) : e = j = 1 } const H = [0, 1, 2, 4, 8, 16, 32, 64, 128, 27, 54 ] var d = d.AES = p.extend({ _doReset: function () { for (var a = this._key, c = a.words, d = a.sigBytes / 4, a = 4 * ((this._nRounds = d + 6) + 1), e = this._keySchedule = [], j = 0; j < a; j++) { if (j < d) e[j] = c[j] else { var k = e[j - 1] j % d ? d > 6 && j % d == 4 && (k = l[k >>> 24] << 24 | l[k >>> 16 & 255] << 16 | l[k >>> 8 & 255] << 8 | l[k & 255]) : (k = k << 8 | k >>> 24, k = l[k >>> 24] << 24 | l[k >>> 16 & 255] << 16 | l[k >>> 8 & 255] << 8 | l[k & 255], k ^= H[j / d | 0] << 24) e[j] = e[j - d] ^ k } } c = this._invKeySchedule = [] for (d = 0; d < a; d++) { j = a - d, k = d % 4 ? e[j] : e[j - 4], c[d] = d < 4 || j <= 4 ? k : b[l[k >>> 24]] ^ x[l[k >>> 16 & 255]] ^ q[l[k >>> 8 & 255]] ^ n[l[k & 255]] } }, encryptBlock: function (a, b) { this._doCryptBlock(a, b, this._keySchedule, t, r, w, v, l) }, decryptBlock: function (a, c) { let d = a[c + 1] a[c + 1] = a[c + 3] a[c + 3] = d this._doCryptBlock(a, c, this._invKeySchedule, b, x, q, n, s) d = a[c + 1] a[c + 1] = a[c + 3] a[c + 3] = d }, _doCryptBlock: function (a, b, c, d, e, j, l, f) { for (var m = this._nRounds, g = a[b] ^ c[0], h = a[b + 1] ^ c[1], k = a[b + 2] ^ c[2], n = a[b + 3] ^ c[3], p = 4, r = 1; r < m; r++) { var q = d[g >>> 24] ^ e[h >>> 16 & 255] ^ j[k >>> 8 & 255] ^ l[n & 255] ^ c[p++] var s = d[h >>> 24] ^ e[k >>> 16 & 255] ^ j[n >>> 8 & 255] ^ l[g & 255] ^ c[p++] var t = d[k >>> 24] ^ e[n >>> 16 & 255] ^ j[g >>> 8 & 255] ^ l[h & 255] ^ c[p++] var n = d[n >>> 24] ^ e[g >>> 16 & 255] ^ j[h >>> 8 & 255] ^ l[k & 255] ^ c[p++] var g = q var h = s var k = t } q = (f[g >>> 24] << 24 | f[h >>> 16 & 255] << 16 | f[k >>> 8 & 255] << 8 | f[n & 255]) ^ c[p++] s = (f[h >>> 24] << 24 | f[k >>> 16 & 255] << 16 | f[n >>> 8 & 255] << 8 | f[g & 255]) ^ c[p++] t = (f[k >>> 24] << 24 | f[n >>> 16 & 255] << 16 | f[g >>> 8 & 255] << 8 | f[h & 255]) ^ c[p++] n = (f[n >>> 24] << 24 | f[g >>> 16 & 255] << 16 | f[h >>> 8 & 255] << 8 | f[k & 255]) ^ c[p++] a[b] = q a[b + 1] = s a[b + 2] = t a[b + 3] = n }, keySize: 8 }) u.AES = p._createHelper(d) })() // module.exports = CryptoJS; // vue2使用 export default CryptoJS // vue3使用
总结
到此这篇关于js实现AES加密解密功能的文章就介绍到这了,更多相关js实现AES加密解密内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
escape、encodeURI 和 encodeURIComponent 的区别
escape(), encodeURI()和encodeURIComponent()是在Javascript中用于编码字符串的三个常用的方法,而他们之间的异同却困扰了很多的Javascript初学者,今天我就在这里对这三个方法详细地分析与比较一下。2009-03-03
最新评论