encryption.min.js 5.8 KB
var Aes={};Aes.Cipher=function(r,e){for(var o=4,n=e.length/o-1,a=[[],[],[],[]],t=0;4*o>t;t++)a[t%4][Math.floor(t/4)]=r[t];a=Aes.AddRoundKey(a,e,0,o);for(var f=1;n>f;f++)a=Aes.SubBytes(a,o),a=Aes.ShiftRows(a,o),a=Aes.MixColumns(a,o),a=Aes.AddRoundKey(a,e,f,o);a=Aes.SubBytes(a,o),a=Aes.ShiftRows(a,o),a=Aes.AddRoundKey(a,e,n,o);for(var c=new Array(4*o),t=0;4*o>t;t++)c[t]=a[t%4][Math.floor(t/4)];return c},Aes.KeyExpansion=function(r){for(var e=4,o=r.length/4,n=o+6,a=new Array(e*(n+1)),t=new Array(4),f=0;o>f;f++){var c=[r[4*f],r[4*f+1],r[4*f+2],r[4*f+3]];a[f]=c}for(var f=o;e*(n+1)>f;f++){a[f]=new Array(4);for(var A=0;4>A;A++)t[A]=a[f-1][A];if(f%o==0){t=Aes.SubWord(Aes.RotWord(t));for(var A=0;4>A;A++)t[A]^=Aes.Rcon[f/o][A]}else o>6&&f%o==4&&(t=Aes.SubWord(t));for(var A=0;4>A;A++)a[f][A]=a[f-o][A]^t[A]}return a},Aes.SubBytes=function(r,e){for(var o=0;4>o;o++)for(var n=0;e>n;n++)r[o][n]=Aes.Sbox[r[o][n]];return r},Aes.ShiftRows=function(r,e){for(var o=new Array(4),n=1;4>n;n++){for(var a=0;4>a;a++)o[a]=r[n][(a+n)%e];for(var a=0;4>a;a++)r[n][a]=o[a]}return r},Aes.MixColumns=function(r,e){for(var o=0;4>o;o++){for(var n=new Array(4),a=new Array(4),t=0;4>t;t++)n[t]=r[t][o],a[t]=128&r[t][o]?r[t][o]<<1^283:r[t][o]<<1;r[0][o]=a[0]^n[1]^a[1]^n[2]^n[3],r[1][o]=n[0]^a[1]^n[2]^a[2]^n[3],r[2][o]=n[0]^n[1]^a[2]^n[3]^a[3],r[3][o]=n[0]^a[0]^n[1]^n[2]^a[3]}return r},Aes.AddRoundKey=function(r,e,o,n){for(var a=0;4>a;a++)for(var t=0;n>t;t++)r[a][t]^=e[4*o+t][a];return r},Aes.SubWord=function(r){for(var e=0;4>e;e++)r[e]=Aes.Sbox[r[e]];return r},Aes.RotWord=function(r){for(var e=r[0],o=0;3>o;o++)r[o]=r[o+1];return r[3]=e,r},Aes.Sbox=[99,124,119,123,242,107,111,197,48,1,103,43,254,215,171,118,202,130,201,125,250,89,71,240,173,212,162,175,156,164,114,192,183,253,147,38,54,63,247,204,52,165,229,241,113,216,49,21,4,199,35,195,24,150,5,154,7,18,128,226,235,39,178,117,9,131,44,26,27,110,90,160,82,59,214,179,41,227,47,132,83,209,0,237,32,252,177,91,106,203,190,57,74,76,88,207,208,239,170,251,67,77,51,133,69,249,2,127,80,60,159,168,81,163,64,143,146,157,56,245,188,182,218,33,16,255,243,210,205,12,19,236,95,151,68,23,196,167,126,61,100,93,25,115,96,129,79,220,34,42,144,136,70,238,184,20,222,94,11,219,224,50,58,10,73,6,36,92,194,211,172,98,145,149,228,121,231,200,55,109,141,213,78,169,108,86,244,234,101,122,174,8,186,120,37,46,28,166,180,198,232,221,116,31,75,189,139,138,112,62,181,102,72,3,246,14,97,53,87,185,134,193,29,158,225,248,152,17,105,217,142,148,155,30,135,233,206,85,40,223,140,161,137,13,191,230,66,104,65,153,45,15,176,84,187,22],Aes.Rcon=[[0,0,0,0],[1,0,0,0],[2,0,0,0],[4,0,0,0],[8,0,0,0],[16,0,0,0],[32,0,0,0],[64,0,0,0],[128,0,0,0],[27,0,0,0],[54,0,0,0]];var AesCtr={};AesCtr.encrypt=function(r,e,o){var n=16;if(128!=o&&192!=o&&256!=o)return"";r=Utf8.encode(r),e=Utf8.encode(e);for(var a=o/8,t=new Array(a),f=0;a>f;f++)t[f]=isNaN(e.charCodeAt(f))?0:e.charCodeAt(f);var c=Aes.Cipher(t,Aes.KeyExpansion(t));c=c.concat(c.slice(0,a-16));for(var A=new Array(n),d=(new Date).getTime(),i=Math.floor(d/1e3),u=d%1e3,f=0;4>f;f++)A[f]=i>>>8*f&255;for(var f=0;4>f;f++)A[f+4]=255&u;for(var s="",f=0;8>f;f++)s+=String.fromCharCode(A[f]);for(var h=Aes.KeyExpansion(c),v=Math.ceil(r.length/n),C=new Array(v),y=0;v>y;y++){for(var l=0;4>l;l++)A[15-l]=y>>>8*l&255;for(var l=0;4>l;l++)A[15-l-4]=y/4294967296>>>8*l;for(var g=Aes.Cipher(A,h),S=v-1>y?n:(r.length-1)%n+1,w=new Array(S),f=0;S>f;f++)w[f]=g[f]^r.charCodeAt(y*n+f),w[f]=String.fromCharCode(w[f]);C[y]=w.join("")}var p=s+C.join("");return p=Base64.encode(p)},AesCtr.decrypt=function(r,e,o){var n=16;if(128!=o&&192!=o&&256!=o)return"";r=Base64.decode(r),e=Utf8.encode(e);for(var a=o/8,t=new Array(a),f=0;a>f;f++)t[f]=isNaN(e.charCodeAt(f))?0:e.charCodeAt(f);var c=Aes.Cipher(t,Aes.KeyExpansion(t));c=c.concat(c.slice(0,a-16));var A=new Array(8);ctrTxt=r.slice(0,8);for(var f=0;8>f;f++)A[f]=ctrTxt.charCodeAt(f);for(var d=Aes.KeyExpansion(c),i=Math.ceil((r.length-8)/n),u=new Array(i),s=0;i>s;s++)u[s]=r.slice(8+s*n,8+s*n+n);r=u;for(var h=new Array(r.length),s=0;i>s;s++){for(var v=0;4>v;v++)A[15-v]=s>>>8*v&255;for(var v=0;4>v;v++)A[15-v-4]=(s+1)/4294967296-1>>>8*v&255;for(var C=Aes.Cipher(A,d),y=new Array(r[s].length),f=0;f<r[s].length;f++)y[f]=C[f]^r[s].charCodeAt(f),y[f]=String.fromCharCode(y[f]);h[s]=y.join("")}var l=h.join("");return l=Utf8.decode(l)};var Base64={};Base64.code="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",Base64.encode=function(r,e){e="undefined"==typeof e?!1:e;var o,n,a,t,f,c,A,d,i,u,s,h=[],v="",C=Base64.code;if(u=e?r.encodeUTF8():r,i=u.length%3,i>0)for(;i++<3;)v+="=",u+="\x00";for(i=0;i<u.length;i+=3)o=u.charCodeAt(i),n=u.charCodeAt(i+1),a=u.charCodeAt(i+2),t=o<<16|n<<8|a,f=t>>18&63,c=t>>12&63,A=t>>6&63,d=63&t,h[i/3]=C.charAt(f)+C.charAt(c)+C.charAt(A)+C.charAt(d);return s=h.join(""),s=s.slice(0,s.length-v.length)+v},Base64.decode=function(r,e){e="undefined"==typeof e?!1:e;var o,n,a,t,f,c,A,d,i,u,s=[],h=Base64.code;u=e?r.decodeUTF8():r;for(var v=0;v<u.length;v+=4)t=h.indexOf(u.charAt(v)),f=h.indexOf(u.charAt(v+1)),c=h.indexOf(u.charAt(v+2)),A=h.indexOf(u.charAt(v+3)),d=t<<18|f<<12|c<<6|A,o=d>>>16&255,n=d>>>8&255,a=255&d,s[v/4]=String.fromCharCode(o,n,a),64==A&&(s[v/4]=String.fromCharCode(o,n)),64==c&&(s[v/4]=String.fromCharCode(o));return i=s.join(""),e?i.decodeUTF8():i};var Utf8={};Utf8.encode=function(r){var e=r.replace(/[\u0080-\u07ff]/g,function(r){var e=r.charCodeAt(0);return String.fromCharCode(192|e>>6,128|63&e)});return e=e.replace(/[\u0800-\uffff]/g,function(r){var e=r.charCodeAt(0);return String.fromCharCode(224|e>>12,128|e>>6&63,128|63&e)})},Utf8.decode=function(r){var e=r.replace(/[\u00c0-\u00df][\u0080-\u00bf]/g,function(r){var e=(31&r.charCodeAt(0))<<6|63&r.charCodeAt(1);return String.fromCharCode(e)});return e=e.replace(/[\u00e0-\u00ef][\u0080-\u00bf][\u0080-\u00bf]/g,function(r){var e=(15&r.charCodeAt(0))<<12|(63&r.charCodeAt(1))<<6|63&r.charCodeAt(2);return String.fromCharCode(e)})};