root / trunk / web / dojo / dojox / encoding / crypto / SimpleAES.js @ 12
History | View | Annotate | Download (5.28 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
4 | see: http://dojotoolkit.org/license for details
|
||
5 | */
|
||
6 | |||
7 | |||
8 | if(!dojo._hasResource["dojox.encoding.crypto.SimpleAES"]){ |
||
9 | dojo._hasResource["dojox.encoding.crypto.SimpleAES"]=true; |
||
10 | dojo.provide("dojox.encoding.crypto.SimpleAES");
|
||
11 | dojo.require("dojox.encoding.base64");
|
||
12 | dojo.require("dojox.encoding.crypto._base");
|
||
13 | (function(){
|
||
14 | var _1=[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]; |
||
15 | var _2=[[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]]; |
||
16 | function _3(_4,w){ |
||
17 | var Nb=4; |
||
18 | var Nr=w.length/Nb-1; |
||
19 | var _5=[[],[],[],[]];
|
||
20 | for(var i=0;i<4*Nb;i++){ |
||
21 | _5[i%4][Math.floor(i/4)]=_4[i]; |
||
22 | } |
||
23 | _5=_6(_5,w,0,Nb);
|
||
24 | for(var _7=1;_7<Nr;_7++){ |
||
25 | _5=_8(_5,Nb); |
||
26 | _5=_9(_5,Nb); |
||
27 | _5=_a(_5,Nb); |
||
28 | _5=_6(_5,w,_7,Nb); |
||
29 | } |
||
30 | _5=_8(_5,Nb); |
||
31 | _5=_9(_5,Nb); |
||
32 | _5=_6(_5,w,Nr,Nb); |
||
33 | var _b=new Array(4*Nb); |
||
34 | for(var i=0;i<4*Nb;i++){ |
||
35 | _b[i]=_5[i%4][Math.floor(i/4)]; |
||
36 | } |
||
37 | return _b;
|
||
38 | }; |
||
39 | function _8(s,Nb){ |
||
40 | for(var r=0;r<4;r++){ |
||
41 | for(var c=0;c<Nb;c++){ |
||
42 | s[r][c]=_1[s[r][c]]; |
||
43 | } |
||
44 | } |
||
45 | return s;
|
||
46 | }; |
||
47 | function _9(s,Nb){ |
||
48 | var t=new Array(4); |
||
49 | for(var r=1;r<4;r++){ |
||
50 | for(var c=0;c<4;c++){ |
||
51 | t[c]=s[r][(c+r)%Nb]; |
||
52 | } |
||
53 | for(var c=0;c<4;c++){ |
||
54 | s[r][c]=t[c]; |
||
55 | } |
||
56 | } |
||
57 | return s;
|
||
58 | }; |
||
59 | function _a(s,Nb){ |
||
60 | for(var c=0;c<4;c++){ |
||
61 | var a=new Array(4); |
||
62 | var b=new Array(4); |
||
63 | for(var i=0;i<4;i++){ |
||
64 | a[i]=s[i][c]; |
||
65 | b[i]=s[i][c]&128?s[i][c]<<1^283:s[i][c]<<1; |
||
66 | } |
||
67 | s[0][c]=b[0]^a[1]^b[1]^a[2]^a[3]; |
||
68 | s[1][c]=a[0]^b[1]^a[2]^b[2]^a[3]; |
||
69 | s[2][c]=a[0]^a[1]^b[2]^a[3]^b[3]; |
||
70 | s[3][c]=a[0]^b[0]^a[1]^a[2]^b[3]; |
||
71 | } |
||
72 | return s;
|
||
73 | }; |
||
74 | function _6(_c,w,_d,Nb){ |
||
75 | for(var r=0;r<4;r++){ |
||
76 | for(var c=0;c<Nb;c++){ |
||
77 | _c[r][c]^=w[_d*4+c][r];
|
||
78 | } |
||
79 | } |
||
80 | return _c;
|
||
81 | }; |
||
82 | function _e(_f){ |
||
83 | var Nb=4; |
||
84 | var Nk=_f.length/4; |
||
85 | var Nr=Nk+6; |
||
86 | var w=new Array(Nb*(Nr+1)); |
||
87 | var _10=new Array(4); |
||
88 | for(var i=0;i<Nk;i++){ |
||
89 | var r=[_f[4*i],_f[4*i+1],_f[4*i+2],_f[4*i+3]]; |
||
90 | w[i]=r; |
||
91 | } |
||
92 | for(var i=Nk;i<(Nb*(Nr+1));i++){ |
||
93 | w[i]=new Array(4); |
||
94 | for(var t=0;t<4;t++){ |
||
95 | _10[t]=w[i-1][t];
|
||
96 | } |
||
97 | if(i%Nk==0){ |
||
98 | _10=_11(_12(_10)); |
||
99 | for(var t=0;t<4;t++){ |
||
100 | _10[t]^=_2[i/Nk][t]; |
||
101 | } |
||
102 | }else{
|
||
103 | if(Nk>6&&i%Nk==4){ |
||
104 | _10=_11(_10); |
||
105 | } |
||
106 | } |
||
107 | for(var t=0;t<4;t++){ |
||
108 | w[i][t]=w[i-Nk][t]^_10[t]; |
||
109 | } |
||
110 | } |
||
111 | return w;
|
||
112 | }; |
||
113 | function _11(w){ |
||
114 | for(var i=0;i<4;i++){ |
||
115 | w[i]=_1[w[i]]; |
||
116 | } |
||
117 | return w;
|
||
118 | }; |
||
119 | function _12(w){ |
||
120 | w[4]=w[0]; |
||
121 | for(var i=0;i<4;i++){ |
||
122 | w[i]=w[i+1];
|
||
123 | } |
||
124 | return w;
|
||
125 | }; |
||
126 | function _13(_14,_15,_16){ |
||
127 | if(!(_16==128||_16==192||_16==256)){ |
||
128 | return ""; |
||
129 | } |
||
130 | var _17=_16/8; |
||
131 | var _18=new Array(_17); |
||
132 | for(var i=0;i<_17;i++){ |
||
133 | _18[i]=_15.charCodeAt(i)&255;
|
||
134 | } |
||
135 | var key=_3(_18,_e(_18));
|
||
136 | key=key.concat(key.slice(0,_17-16)); |
||
137 | var _19=16; |
||
138 | var _1a=new Array(_19); |
||
139 | var _1b=(new Date()).getTime(); |
||
140 | for(var i=0;i<4;i++){ |
||
141 | _1a[i]=(_1b>>>i*8)&255; |
||
142 | } |
||
143 | for(var i=0;i<4;i++){ |
||
144 | _1a[i+4]=(_1b/4294967296>>>i*8)&255; |
||
145 | } |
||
146 | var _1c=_e(key);
|
||
147 | var _1d=Math.ceil(_14.length/_19);
|
||
148 | var _1e=new Array(_1d); |
||
149 | for(var b=0;b<_1d;b++){ |
||
150 | for(var c=0;c<4;c++){ |
||
151 | _1a[15-c]=(b>>>c*8)&255; |
||
152 | } |
||
153 | for(var c=0;c<4;c++){ |
||
154 | _1a[15-c-4]=(b/4294967296>>>c*8); |
||
155 | } |
||
156 | var _1f=_3(_1a,_1c);
|
||
157 | var _20=b<_1d-1?_19:(_14.length-1)%_19+1; |
||
158 | var ct=""; |
||
159 | for(var i=0;i<_20;i++){ |
||
160 | var _21=_14.charCodeAt(b*_19+i);
|
||
161 | var _22=_21^_1f[i];
|
||
162 | ct+=((_22<16)?"0":"")+_22.toString(16); |
||
163 | } |
||
164 | _1e[b]=ct; |
||
165 | } |
||
166 | var _23=""; |
||
167 | for(var i=0;i<8;i++){ |
||
168 | _23+=((_1a[i]<16)?"0":"")+_1a[i].toString(16); |
||
169 | } |
||
170 | return _23+" "+_1e.join(" "); |
||
171 | }; |
||
172 | function _24(s){ |
||
173 | var ret=[];
|
||
174 | s.replace(/(..)/g,function(str){ |
||
175 | ret.push(parseInt(str,16));
|
||
176 | }); |
||
177 | return ret;
|
||
178 | }; |
||
179 | function _25(_26,_27,_28){ |
||
180 | if(!(_28==128||_28==192||_28==256)){ |
||
181 | return ""; |
||
182 | } |
||
183 | var _29=_28/8; |
||
184 | var _2a=new Array(_29); |
||
185 | for(var i=0;i<_29;i++){ |
||
186 | _2a[i]=_27.charCodeAt(i)&255;
|
||
187 | } |
||
188 | var _2b=_e(_2a);
|
||
189 | var key=_3(_2a,_2b);
|
||
190 | key=key.concat(key.slice(0,_29-16)); |
||
191 | var _2c=_e(key);
|
||
192 | _26=_26.split(" ");
|
||
193 | var _2d=16; |
||
194 | var _2e=new Array(_2d); |
||
195 | var _2f=_26[0]; |
||
196 | _2e=_24(_2f); |
||
197 | var _30=new Array(_26.length-1); |
||
198 | for(var b=1;b<_26.length;b++){ |
||
199 | for(var c=0;c<4;c++){ |
||
200 | _2e[15-c]=((b-1)>>>c*8)&255; |
||
201 | } |
||
202 | for(var c=0;c<4;c++){ |
||
203 | _2e[15-c-4]=((b/4294967296-1)>>>c*8)&255; |
||
204 | } |
||
205 | var _31=_3(_2e,_2c);
|
||
206 | var pt=""; |
||
207 | var tmp=_24(_26[b]);
|
||
208 | for(var i=0;i<tmp.length;i++){ |
||
209 | var _32=_26[b].charCodeAt(i);
|
||
210 | var _33=tmp[i]^_31[i];
|
||
211 | pt+=String.fromCharCode(_33); |
||
212 | } |
||
213 | _30[b-1]=pt;
|
||
214 | } |
||
215 | return _30.join(""); |
||
216 | }; |
||
217 | function _34(str){ |
||
218 | return str.replace(/[\0\t\n\v\f\r\xa0!-]/g,function(c){ |
||
219 | return "!"+c.charCodeAt(0)+"!"; |
||
220 | }); |
||
221 | }; |
||
222 | function _35(str){ |
||
223 | return str.replace(/!\d\d?\d?!/g,function(c){ |
||
224 | return String.fromCharCode(c.slice(1,-1)); |
||
225 | }); |
||
226 | }; |
||
227 | dojox.encoding.crypto.SimpleAES=new (function(){ |
||
228 | this.encrypt=function(_36,key){ |
||
229 | return _13(_36,key,256); |
||
230 | }; |
||
231 | this.decrypt=function(_37,key){ |
||
232 | return _25(_37,key,256); |
||
233 | }; |
||
234 | })(); |
||
235 | })(); |
||
236 | } |