1 Star 0 Fork 0

Fictiony Chen/jshell

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
util.js 8.87 KB
一键复制 编辑 原始数据 按行查看 历史
Fictiony Chen 提交于 2021-04-15 12:19 . 初始版本
function util() {
// AES加密
var aes=function(){var r,n,e=14,t=!1,f=function(r){var n,e,t=[];for(r.length<16&&(t=[n=16-r.length,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n]),e=0;e<r.length;e++)t[e]=r[e];return t},c=function(r,n){var e,t,f="";if(n){if((e=r[15])>16)throw"Decryption error: Maybe bad key";if(16===e)return"";for(t=0;t<16-e;t++)f+=String.fromCharCode(r[t])}else for(t=0;t<16;t++)f+=String.fromCharCode(r[t]);return f},o=function(r,n){var e,t=[];for(n||(r=function(r){try{return unescape(encodeURIComponent(r))}catch(r){throw"Error on UTF-8 encode"}}(r)),e=0;e<r.length;e++)t[e]=255&r.charCodeAt(e);return t},a=function(r,n){var e,t=[],f=[],c=r.concat(n);for(t[0]=B(c),f=t[0],e=1;e<3;e++)t[e]=B(t[e-1].concat(c)),f=f.concat(t[e]);return{key:f.slice(0,32),iv:f.slice(32,48)}},u=function(r,n,e,t){n=s(n);var f,o=r.length/16,a=[],u=[],i="";for(f=0;f<o;f++)a.push(r.slice(16*f,16*(f+1)));for(f=a.length-1;f>=0;f--)u[f]=d(a[f],n),u[f]=g(u[f],0===f?e:a[f-1]);for(f=0;f<o-1;f++)i+=c(u[f]);return i+=c(u[f],!0),t?i:function(r){try{return decodeURIComponent(escape(r))}catch(r){throw"Bad Key"}}(i)},i=function(r,n){t=!1;var f,c=v(r,n,0);for(f=1;f<15;f++)c=h(c),c=l(c),f<e&&(c=b(c)),c=v(c,n,f);return c},d=function(r,n){t=!0;var f,c=v(r,n,e);for(f=13;f>-1;f--)c=l(c),c=h(c),c=v(c,n,f),f>0&&(c=b(c));return c},h=function(r){var n,e=t?k:A,f=[];for(n=0;n<16;n++)f[n]=e[r[n]];return f},l=function(r){var n,e=[],f=t?[0,13,10,7,4,1,14,11,8,5,2,15,12,9,6,3]:[0,5,10,15,4,9,14,3,8,13,2,7,12,1,6,11];for(n=0;n<16;n++)e[n]=r[f[n]];return e},b=function(r){var n,e=[];if(t)for(n=0;n<4;n++)e[4*n]=R[r[4*n]]^S[r[1+4*n]]^U[r[2+4*n]]^I[r[3+4*n]],e[1+4*n]=I[r[4*n]]^R[r[1+4*n]]^S[r[2+4*n]]^U[r[3+4*n]],e[2+4*n]=U[r[4*n]]^I[r[1+4*n]]^R[r[2+4*n]]^S[r[3+4*n]],e[3+4*n]=S[r[4*n]]^U[r[1+4*n]]^I[r[2+4*n]]^R[r[3+4*n]];else for(n=0;n<4;n++)e[4*n]=O[r[4*n]]^w[r[1+4*n]]^r[2+4*n]^r[3+4*n],e[1+4*n]=r[4*n]^O[r[1+4*n]]^w[r[2+4*n]]^r[3+4*n],e[2+4*n]=r[4*n]^r[1+4*n]^O[r[2+4*n]]^w[r[3+4*n]],e[3+4*n]=w[r[4*n]]^r[1+4*n]^r[2+4*n]^O[r[3+4*n]];return e},v=function(r,n,e){var t,f=[];for(t=0;t<16;t++)f[t]=r[t]^n[e][t];return f},g=function(r,n){var e,t=[];for(e=0;e<16;e++)t[e]=r[e]^n[e];return t},s=function(r){var n,e,t,f,c=[],o=[],a=[];for(n=0;n<8;n++)e=[r[4*n],r[4*n+1],r[4*n+2],r[4*n+3]],c[n]=e;for(n=8;n<60;n++){for(c[n]=[],t=0;t<4;t++)o[t]=c[n-1][t];for(n%8==0?(o=p(y(o)))[0]^=x[n/8-1]:n%8==4&&(o=p(o)),t=0;t<4;t++)c[n][t]=c[n-8][t]^o[t]}for(n=0;n<15;n++)for(a[n]=[],f=0;f<4;f++)a[n].push(c[4*n+f][0],c[4*n+f][1],c[4*n+f][2],c[4*n+f][3]);return a},p=function(r){for(var n=0;n<4;n++)r[n]=A[r[n]];return r},y=function(r){var n,e=r[0];for(n=0;n<3;n++)r[n]=r[n+1];return r[3]=e,r},C=function(r,n){var e,t=[];for(e=0;e<r.length;e+=n)t[e/n]=parseInt(r.substr(e,n),16);return t},M=function(r,n){var e,t;for(t=0,e=0;e<8;e++)t=1==(1&n)?t^r:t,r=r>127?283^r<<1:r<<1,n>>>=1;return t},m=function(r){var n,e=[];for(n=0;n<256;n++)e[n]=M(r,n);return e},A=C("637c777bf26b6fc53001672bfed7ab76ca82c97dfa5947f0add4a2af9ca472c0b7fd9326363ff7cc34a5e5f171d8311504c723c31896059a071280e2eb27b27509832c1a1b6e5aa0523bd6b329e32f8453d100ed20fcb15b6acbbe394a4c58cfd0efaafb434d338545f9027f503c9fa851a3408f929d38f5bcb6da2110fff3d2cd0c13ec5f974417c4a77e3d645d197360814fdc222a908846eeb814de5e0bdbe0323a0a4906245cc2d3ac629195e479e7c8376d8dd54ea96c56f4ea657aae08ba78252e1ca6b4c6e8dd741f4bbd8b8a703eb5664803f60e613557b986c11d9ee1f8981169d98e949b1e87e9ce5528df8ca1890dbfe6426841992d0fb054bb16",2),k=function(r){var n,e=[];for(n=0;n<r.length;n++)e[r[n]]=n;return e}(A),x=C("01020408102040801b366cd8ab4d9a2f5ebc63c697356ad4b37dfaefc591",2),O=m(2),w=m(3),I=m(9),S=m(11),U=m(13),R=m(14),B=function(){function r(r,n,e,t,c,o){return r=f(f(n,r),f(t,o)),f(r<<c|r>>>32-c,e)}function n(n,e,t,f,c,o,a){return r(e&t|~e&f,n,e,c,o,a)}function e(n,e,t,f,c,o,a){return r(e&f|t&~f,n,e,c,o,a)}function t(n,e,t,f,c,o,a){return r(t^(e|~f),n,e,c,o,a)}function f(r,n){var e=(65535&r)+(65535&n);return(r>>16)+(n>>16)+(e>>16)<<16|65535&e}return function(c){for(var o=[],a=0;a<8*c.length;a+=8)o[a>>5]|=c[a/8]<<a%32;o[(c=8*c.length)>>5]|=128<<c%32,o[14+(c+64>>>9<<4)]=c,c=1732584193,a=-271733879;for(var u=-1732584194,i=271733878,d=0;d<o.length;d+=16){var h=c,l=a,b=u,v=i;c=n(c,a,u,i,o[d+0],7,-680876936),i=n(i,c,a,u,o[d+1],12,-389564586),u=n(u,i,c,a,o[d+2],17,606105819),a=n(a,u,i,c,o[d+3],22,-1044525330),c=n(c,a,u,i,o[d+4],7,-176418897),i=n(i,c,a,u,o[d+5],12,1200080426),u=n(u,i,c,a,o[d+6],17,-1473231341),a=n(a,u,i,c,o[d+7],22,-45705983),c=n(c,a,u,i,o[d+8],7,1770035416),i=n(i,c,a,u,o[d+9],12,-1958414417),u=n(u,i,c,a,o[d+10],17,-42063),a=n(a,u,i,c,o[d+11],22,-1990404162),c=n(c,a,u,i,o[d+12],7,1804603682),i=n(i,c,a,u,o[d+13],12,-40341101),u=n(u,i,c,a,o[d+14],17,-1502002290),c=e(c,a=n(a,u,i,c,o[d+15],22,1236535329),u,i,o[d+1],5,-165796510),i=e(i,c,a,u,o[d+6],9,-1069501632),u=e(u,i,c,a,o[d+11],14,643717713),a=e(a,u,i,c,o[d+0],20,-373897302),c=e(c,a,u,i,o[d+5],5,-701558691),i=e(i,c,a,u,o[d+10],9,38016083),u=e(u,i,c,a,o[d+15],14,-660478335),a=e(a,u,i,c,o[d+4],20,-405537848),c=e(c,a,u,i,o[d+9],5,568446438),i=e(i,c,a,u,o[d+14],9,-1019803690),u=e(u,i,c,a,o[d+3],14,-187363961),a=e(a,u,i,c,o[d+8],20,1163531501),c=e(c,a,u,i,o[d+13],5,-1444681467),i=e(i,c,a,u,o[d+2],9,-51403784),u=e(u,i,c,a,o[d+7],14,1735328473),c=r((a=e(a,u,i,c,o[d+12],20,-1926607734))^u^i,c,a,o[d+5],4,-378558),i=r(c^a^u,i,c,o[d+8],11,-2022574463),u=r(i^c^a,u,i,o[d+11],16,1839030562),a=r(u^i^c,a,u,o[d+14],23,-35309556),c=r(a^u^i,c,a,o[d+1],4,-1530992060),i=r(c^a^u,i,c,o[d+4],11,1272893353),u=r(i^c^a,u,i,o[d+7],16,-155497632),a=r(u^i^c,a,u,o[d+10],23,-1094730640),c=r(a^u^i,c,a,o[d+13],4,681279174),i=r(c^a^u,i,c,o[d+0],11,-358537222),u=r(i^c^a,u,i,o[d+3],16,-722521979),a=r(u^i^c,a,u,o[d+6],23,76029189),c=r(a^u^i,c,a,o[d+9],4,-640364487),i=r(c^a^u,i,c,o[d+12],11,-421815835),u=r(i^c^a,u,i,o[d+15],16,530742520),c=t(c,a=r(u^i^c,a,u,o[d+2],23,-995338651),u,i,o[d+0],6,-198630844),i=t(i,c,a,u,o[d+7],10,1126891415),u=t(u,i,c,a,o[d+14],15,-1416354905),a=t(a,u,i,c,o[d+5],21,-57434055),c=t(c,a,u,i,o[d+12],6,1700485571),i=t(i,c,a,u,o[d+3],10,-1894986606),u=t(u,i,c,a,o[d+10],15,-1051523),a=t(a,u,i,c,o[d+1],21,-2054922799),c=t(c,a,u,i,o[d+8],6,1873313359),i=t(i,c,a,u,o[d+15],10,-30611744),u=t(u,i,c,a,o[d+6],15,-1560198380),a=t(a,u,i,c,o[d+13],21,1309151649),c=t(c,a,u,i,o[d+4],6,-145523070),i=t(i,c,a,u,o[d+11],10,-1120210379),u=t(u,i,c,a,o[d+2],15,718787259),a=t(a,u,i,c,o[d+9],21,-343485551),c=f(c,h),a=f(a,l),u=f(u,b),i=f(i,v)}for(o=[c,a,u,i],c=[],a=0;a<4*o.length;a++)c.push(16*(o[a>>2]>>a%4*8+4&15)+(o[a>>2]>>a%4*8&15));return c}}(),D=(n=(r="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/").split(""),{encode:function(r,e){var t,f,c=[],o="";for(t=0;t<16*r.length;t++)c.push(r[Math.floor(t/16)][t%16]);for(t=0;t<c.length;t+=3)o+=n[c[t]>>2],o+=n[(3&c[t])<<4|c[t+1]>>4],void 0!==c[t+1]?o+=n[(15&c[t+1])<<2|c[t+2]>>6]:o+="=",void 0!==c[t+2]?o+=n[63&c[t+2]]:o+="=";for(f=o.slice(0,64)+"\n",t=1;t<Math.ceil(o.length/64);t++)f+=o.slice(64*t,64*t+64)+(Math.ceil(o.length/64)===t+1?"":"\n");return f},decode:function(n){n=n.replace(/\n/g,"");var e,t=[],f=[],c=[];for(e=0;e<n.length;e+=4)f[0]=r.indexOf(n.charAt(e)),f[1]=r.indexOf(n.charAt(e+1)),f[2]=r.indexOf(n.charAt(e+2)),f[3]=r.indexOf(n.charAt(e+3)),c[0]=f[0]<<2|f[1]>>4,c[1]=(15&f[1])<<4|f[2]>>2,c[2]=(3&f[2])<<6|f[3],t.push(c[0],c[1],c[2]);return t=t.slice(0,t.length-t.length%16)}});return{dec:function(r,n,e){var t=D.decode(r),f=t.slice(8,16),c=a(o(n,e),f),i=c.key,d=c.iv;return t=t.slice(16,t.length),r=u(t,i,d,e)},enc:function(r,n,e){var t,c=function(r){var n,e=[];for(n=0;n<r;n++)e=e.concat(Math.floor(256*Math.random()));return e}(8),u=a(o(n,e),c),d=u.key,h=u.iv,l=[[83,97,108,116,101,100,95,95].concat(c)];return t=function(r,n,e){n=s(n);var t,c=Math.ceil(r.length/16),o=[],a=[];for(t=0;t<c;t++)o[t]=f(r.slice(16*t,16*t+16));for(r.length%16==0&&(o.push([16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16]),c++),t=0;t<o.length;t++)o[t]=g(o[t],0===t?e:a[t-1]),a[t]=i(o[t],n);return a}(r=o(r,e),d,h),t=l.concat(t),D.encode(t)},md5:function(r){return function(r){var n,e="";for(n=0;n<r.length;n++)e+=(r[n]<16?"0":"")+r[n].toString(16);return e}(B(o(r,!0)))}}}();
// Hex转Json
var hex2json=function(){var r,a,n,t={"*":[" ","ak","}-}",'"+"'],k:["a","}-}",":.:",",*,"],":":[" ",":.:"],".":[" ","av","00","{+{",'"+"'],v:["a","}-}",",*,","+.+"],0:["0","}-}",",*,","+.+"],",":[" ","}-}",",*,","+.+"],'"':[" ","a","0","{","}",":",",",'"-"',"+"]},e={k:["a",":.:"]};function u(n){var e="charAt",u=parseInt(n,16),v='"0 +{}:,"+a{}:,"'[e](u),f=r[a]||t[a];for(var c in f){var i;if((i=f[c])[e](0)==v)return o(i[e](1),i[e](2)||n)}return o((i=f[u%f.length])[e](1),i[e](2)||("0"==i[e](0)?"0":"a"))}function o(r,t){return"+"==r?(n.push(a),a='"'==t?'"':"*"):"-"==r?n.length>0?a="*"==(a=n.pop())?":":",":"*"==a?(a="k",t="a"):(a="*",t=","):r&&" "!=r&&(a=r),t}return function(o,v){return r=v?e:t,a="*",n=[],"{"+o.replace(/./g,u)+function(r){var t="";":"==a?t=":a":"."==a?t="a":'"'==a?t="*"==n.pop()?'":a':'"':r&&"k"==a&&(t=":a");for(var e=n.length;e>0;e--)t+="}";return t}(v)+"}"}}();
return [ aes, hex2json ];
};
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/fictiony/jshell.git
git@gitee.com:fictiony/jshell.git
fictiony
jshell
jshell
master

搜索帮助