-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathaes.min.js
1 lines (1 loc) · 2.72 KB
/
aes.min.js
1
var AES={Codec:{strToWords:function(f){if(f.constructor!==String){throw"Input must be a string."}var c=unescape(encodeURIComponent(f)),d=c.length;var e=[],b=0;for(var a=0;a<d;a++){b=b<<8|c.charCodeAt(a);if((a&3)===3){e.push(b);b=0}}if(a&3){e.push(this.encodePartial(8*(a&3),b))}return e},wordsToStr:function(e){if(e.constructor!==Array){throw"Input must be an array."}var d=this.bitLength(e),c="",b;for(var a=0;a<d/8;a++){if((a&3)===0){b=e[a/4]}c+=String.fromCharCode(b>>>24);b<<=8}return decodeURIComponent(escape(c))},encodePartial:function(b,a,c){if(b===32){return a}return(c?a|0:a<<(32-b))+b*1099511627776},getPartial:function(a){return Math.round(a/1099511627776)||32},bitLength:function(d){var c=d.length,b;if(c===0){return 0}b=d[c-1];return(c-1)*32+this.getPartial(b)}},CTR:function(b,a){this._ecb=new AES.ECB(b);this._ctr=a.concat([0,0,0]).slice(0,4);var c=this;this.encrypt=function(f){var d;for(var e=0;e<f.length;e+=4){d=c._ecb.encrypt(c._ctr);c._ctr[3]++;f[e]^=d[0];f[e+1]^=d[1];f[e+2]^=d[2];f[e+3]^=d[3]}return f}},ECB:function(h){if(!this._tables[0][0][0]){this._precompute()}var d,c,e,g,l,f=this._tables[0][4],k=this._tables[1],a=h.length,b=1;if(a!==4&&a!==6&&a!==8){throw"Invalid AES key size."}this._key=[g=h.slice(0),l=[]];for(d=a;d<4*a+28;d++){e=g[d-1];if(d%a===0||(a===8&&d%a===4)){e=f[e>>>24]<<24^f[e>>16&255]<<16^f[e>>8&255]<<8^f[e&255];if(d%a===0){e=e<<8^e>>>24^b<<24;b=b<<1^(b>>7)*283}}g[d]=g[d-a]^e}for(c=0;d;c++,d--){e=g[c&3?d:d-4];if(d<=4||c<4){l[c]=e}else{l[c]=k[0][f[e>>>24]]^k[1][f[e>>16&255]]^k[2][f[e>>8&255]]^k[3][f[e&255]]}}this.encrypt=function(i){return this._crypt(i,0)};this.decrypt=function(i){return this._crypt(i,1)};this._crypt=function(q,t){if(q.length!==4){throw"Invalid AES block size."}var E=this._key[t],B=q[0]^E[0],A=q[t?3:1]^E[1],z=q[2]^E[2],y=q[t?1:3]^E[3],C,j,s,D=E.length/4-2,v,u=4,w=[0,0,0,0],x=this._tables[t],p=x[0],o=x[1],n=x[2],m=x[3],r=x[4];for(v=0;v<D;v++){C=p[B>>>24]^o[A>>16&255]^n[z>>8&255]^m[y&255]^E[u];j=p[A>>>24]^o[z>>16&255]^n[y>>8&255]^m[B&255]^E[u+1];s=p[z>>>24]^o[y>>16&255]^n[B>>8&255]^m[A&255]^E[u+2];y=p[y>>>24]^o[B>>16&255]^n[A>>8&255]^m[z&255]^E[u+3];u+=4;B=C;A=j;z=s}for(v=0;v<4;v++){w[t?3&-v:v]=r[B>>>24]<<24^r[A>>16&255]<<16^r[z>>8&255]<<8^r[y&255]^E[u++];C=B;B=A;A=z;z=y;y=C}return w}}};AES.ECB.prototype._precompute=function(){var j=this._tables[0],q=this._tables[1],h=j[4],n=q[4],g,l,f,k=[],c=[],b,p,m,o,e,a;for(g=0;g<256;g++){c[(k[g]=g<<1^(g>>7)*283)^g]=g}for(l=f=0;!h[l];l^=b||1,f=c[f]||1){o=f^f<<1^f<<2^f<<3^f<<4;o=o>>8^o&255^99;h[l]=o;n[o]=l;m=k[p=k[b=k[l]]];a=m*16843009^p*65537^b*257^l*16843008;e=k[o]*257^o*16843008;for(g=0;g<4;g++){j[g][l]=e=e<<24^e>>>8;q[g][o]=a=a<<24^a>>>8}}for(g=0;g<5;g++){j[g]=j[g].slice(0);q[g]=q[g].slice(0)}};AES.ECB.prototype._tables=[[[],[],[],[],[]],[[],[],[],[],[]]];