User:Decltype/aes.js

// Licensed under CC-BY-3.0 -- http://www.movable-type.co.uk/scripts/aes.html /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ /*  AES implementation in JavaScript (c) Chris Veness 2005-2010                                   */ /*  - see http://csrc.nist.gov/publications/PubsFIPS.html#197                                    */ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

var Aes={};Aes.Cipher=function(e,a){var d=4;var h=a.length/d-1;var g=],[],[],[;for(var f=0;f<4*d;f++){g[f%4][Math.floor(f/4)]=e[f]}g=Aes.AddRoundKey(g,a,0,d);for(var c=1;c<h;c++){g=Aes.SubBytes(g,d);g=Aes.ShiftRows(g,d);g=Aes.MixColumns(g,d);g=Aes.AddRoundKey(g,a,c,d)}g=Aes.SubBytes(g,d);g=Aes.ShiftRows(g,d);g=Aes.AddRoundKey(g,a,h,d);var b=new Array(4*d);for(var f=0;f<4*d;f++){b[f]=g[f%4][Math.floor(f/4)]}return b};Aes.KeyExpansion=function(f){var d=4;var b=f.length/4;var g=b+6;var e=new Array(d*(g+1));var h=new Array(4);for(var c=0;c6&&c%b==4){h=Aes.SubWord(h)}}for(var j=0;j<4;j++){e[c][j]=e[c-b][j]^h[j]}}return e};Aes.SubBytes=function(b,a){for(var d=0;d<4;d++){for(var e=0;e<a;e++){b[d][e]=Aes.Sbox[b[d][e]]}}return b};Aes.ShiftRows=function(d,a){var b=new Array(4);for(var e=1;e<4;e++){for(var f=0;f<4;f++){b[f]=d[e][(f+e)%a]}for(var f=0;f<4;f++){d[e][f]=b[f]}}return d};Aes.MixColumns=function(h,f){for(var j=0;j<4;j++){var e=new Array(4);var d=new Array(4);for(var g=0;g<4;g++){e[g]=h[g][j];d[g]=h[g][j]&128?h[g][j]<<1^283:h[g][j]<<1}h[0][j]=d[0]^e[1]^d[1]^e[2]^e[3];h[1][j]=e[0]^d[1]^e[2]^d[2]^e[3];h[2][j]=e[0]^e[1]^d[2]^e[3]^d[3];h[3][j]=e[0]^d[0]^e[1]^e[2]^d[3]}return h};Aes.AddRoundKey=function(f,a,d,b){for(var e=0;e<4;e++){for(var g=0;g<b;g++){f[e][g]^=a[d*4+g][e]}}return f};Aes.SubWord=function(a){for(var b=0;b<4;b++){a[b]=Aes.Sbox[a[b]]}return a};Aes.RotWord=function(a){var c=a[0];for(var b=0;b<3;b++){a[b]=a[b+1]}a[3]=c;return a};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(j,a,t){var k=16;if(!(t==128||t==192||t==256)){return""}j=Utf8.encode(j);a=Utf8.encode(a);var l=t/8;var f=new Array(l);for(var r=0;r>>r*8)&255}for(var r=0;r<4;r++){e[r+4]=g&255}var n="";for(var r=0;r<8;r++){n+=String.fromCharCode(e[r])}var v=Aes.KeyExpansion(y);var q=Math.ceil(j.length/k);var m=new Array(q);for(var w=0;w>>u*8)&255}for(var u=0;u<4;u++){e[15-u-4]=(w/4294967296>>>u*8)}var h=Aes.Cipher(e,v);var p=w<q-1?k:(j.length-1)%k+1;var o=new Array(p);for(var r=0;r<p;r++){o[r]=h[r]^j.charCodeAt(w*k+r);o[r]=String.fromCharCode(o[r])}m[w]=o.join("")}var x=n+m.join("");x=Base64.encode(x);return x};AesCtr.decrypt=function(t,e,p){var m=16;if(!(p==128||p==192||p==256)){return""}t=Base64.decode(t);e=Utf8.encode(e);var n=p/8;var j=new Array(n);for(var o=0;o>>q*8)&255}for(var q=0;q<4;q++){f[15-q-4]=(((s+1)/4294967296-1)>>>q*8)&255}var l=Aes.Cipher(f,r);var d=new Array(t[s].length);for(var o=0;o0){while(m++<3){f+="=";q+="\0"}}for(m=0;m>18&63;k=r>>12&63;j=r>>6&63;h=r&63;i[m/3]=d.charAt(o)+d.charAt(k)+d.charAt(j)+d.charAt(h)}l=i.join("");l=l.slice(0,l.length-f.length)+f;return l};Base64.decode=function(n,e){e=(typeof e=="undefined")?false:e;var g,b,a,o,k,i,h,q,j=[],p,m;var f=Base64.code;m=e?n.decodeUTF8:n;for(var l=0;l>>16&255;b=q>>>8&255;a=q&255;j[l/4]=String.fromCharCode(g,b,a);if(h==64){j[l/4]=String.fromCharCode(g,b)}if(i==64){j[l/4]=String.fromCharCode(g)}}p=j.join("");return e?p.decodeUTF8:p};var Utf8={};Utf8.encode=function(a){var b=a.replace(/[\u0080-\u07ff]/g,function(e){var d=e.charCodeAt(0);return String.fromCharCode(192|d>>6,128|d&63)});b=b.replace(/[\u0800-\uffff]/g,function(e){var d=e.charCodeAt(0);return String.fromCharCode(224|d>>12,128|d>>6&63,128|d&63)});return b};Utf8.decode=function(b){var a=b.replace(/[\u00c0-\u00df][\u0080-\u00bf]/g,function(e){var d=(e.charCodeAt(0)&31)<<6|e.charCodeAt(1)&63;return String.fromCharCode(d)});a=a.replace(/[\u00e0-\u00ef][\u0080-\u00bf][\u0080-\u00bf]/g,function(e){var d=((e.charCodeAt(0)&15)<<12)|((e.charCodeAt(1)&63)<<6)|(e.charCodeAt(2)&63);return String.fromCharCode(d)});return a};