PageRenderTime 65ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/js/lib/Socket.IO-node/support/socket.io-client/lib/vendor/web-socket-js/flash-src/com/hurlant/crypto/symmetric/DESKey.as

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 365 lines | 277 code | 53 blank | 35 comment | 17 complexity | 20fcbad156109400920285df56cadfbc MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /**
  2. * DESKey
  3. *
  4. * An Actionscript 3 implementation of the Data Encryption Standard (DES)
  5. * Copyright (c) 2007 Henri Torgemane
  6. *
  7. * Derived from:
  8. * The Bouncy Castle Crypto package,
  9. * Copyright (c) 2000-2004 The Legion Of The Bouncy Castle
  10. * (http://www.bouncycastle.org)
  11. *
  12. * See LICENSE.txt for full license information.
  13. */
  14. package com.hurlant.crypto.symmetric
  15. {
  16. import flash.utils.ByteArray;
  17. import com.hurlant.util.Hex;
  18. import com.hurlant.util.Memory;
  19. public class DESKey implements ISymmetricKey
  20. {
  21. /**
  22. * what follows is mainly taken from "Applied Cryptography", by Bruce
  23. * Schneier, however it also bears great resemblance to Richard
  24. * Outerbridge's D3DES...
  25. */
  26. private static const Df_Key:Array = [ 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32,
  27. 0x10, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67 ];
  28. private static const bytebit:Array = [ 128, 64, 32, 16, 8, 4, 2, 1 ];
  29. private static const bigbyte:Array = [ 0x800000, 0x400000, 0x200000, 0x100000, 0x80000, 0x40000, 0x20000, 0x10000, 0x8000,
  30. 0x4000, 0x2000, 0x1000, 0x800, 0x400, 0x200, 0x100, 0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1 ];
  31. /*
  32. * Use the key schedule specified in the Standard (ANSI X3.92-1981).
  33. */
  34. private static const pc1:Array = [ 56, 48, 40, 32, 24, 16, 8, 0, 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2,
  35. 59, 51, 43, 35, 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 60, 52, 44, 36, 28, 20, 12,
  36. 4, 27, 19, 11, 3 ];
  37. private static const totrot:Array = [ 1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19, 21, 23, 25, 27, 28 ];
  38. private static const pc2:Array = [ 13, 16, 10, 23, 0, 4, 2, 27, 14, 5, 20, 9, 22, 18, 11, 3, 25, 7, 15, 6, 26, 19, 12, 1, 40,
  39. 51, 30, 36, 46, 54, 29, 39, 50, 44, 32, 47, 43, 48, 38, 55, 33, 52, 45, 41, 49, 35, 28, 31 ];
  40. private static const SP1:Array = [ 0x01010400, 0x00000000, 0x00010000, 0x01010404, 0x01010004, 0x00010404, 0x00000004,
  41. 0x00010000, 0x00000400, 0x01010400, 0x01010404, 0x00000400, 0x01000404, 0x01010004, 0x01000000, 0x00000004,
  42. 0x00000404, 0x01000400, 0x01000400, 0x00010400, 0x00010400, 0x01010000, 0x01010000, 0x01000404, 0x00010004,
  43. 0x01000004, 0x01000004, 0x00010004, 0x00000000, 0x00000404, 0x00010404, 0x01000000, 0x00010000, 0x01010404,
  44. 0x00000004, 0x01010000, 0x01010400, 0x01000000, 0x01000000, 0x00000400, 0x01010004, 0x00010000, 0x00010400,
  45. 0x01000004, 0x00000400, 0x00000004, 0x01000404, 0x00010404, 0x01010404, 0x00010004, 0x01010000, 0x01000404,
  46. 0x01000004, 0x00000404, 0x00010404, 0x01010400, 0x00000404, 0x01000400, 0x01000400, 0x00000000, 0x00010004,
  47. 0x00010400, 0x00000000, 0x01010004 ];
  48. private static const SP2:Array = [ 0x80108020, 0x80008000, 0x00008000, 0x00108020, 0x00100000, 0x00000020, 0x80100020,
  49. 0x80008020, 0x80000020, 0x80108020, 0x80108000, 0x80000000, 0x80008000, 0x00100000, 0x00000020, 0x80100020,
  50. 0x00108000, 0x00100020, 0x80008020, 0x00000000, 0x80000000, 0x00008000, 0x00108020, 0x80100000, 0x00100020,
  51. 0x80000020, 0x00000000, 0x00108000, 0x00008020, 0x80108000, 0x80100000, 0x00008020, 0x00000000, 0x00108020,
  52. 0x80100020, 0x00100000, 0x80008020, 0x80100000, 0x80108000, 0x00008000, 0x80100000, 0x80008000, 0x00000020,
  53. 0x80108020, 0x00108020, 0x00000020, 0x00008000, 0x80000000, 0x00008020, 0x80108000, 0x00100000, 0x80000020,
  54. 0x00100020, 0x80008020, 0x80000020, 0x00100020, 0x00108000, 0x00000000, 0x80008000, 0x00008020, 0x80000000,
  55. 0x80100020, 0x80108020, 0x00108000 ];
  56. private static const SP3:Array = [ 0x00000208, 0x08020200, 0x00000000, 0x08020008, 0x08000200, 0x00000000, 0x00020208,
  57. 0x08000200, 0x00020008, 0x08000008, 0x08000008, 0x00020000, 0x08020208, 0x00020008, 0x08020000, 0x00000208,
  58. 0x08000000, 0x00000008, 0x08020200, 0x00000200, 0x00020200, 0x08020000, 0x08020008, 0x00020208, 0x08000208,
  59. 0x00020200, 0x00020000, 0x08000208, 0x00000008, 0x08020208, 0x00000200, 0x08000000, 0x08020200, 0x08000000,
  60. 0x00020008, 0x00000208, 0x00020000, 0x08020200, 0x08000200, 0x00000000, 0x00000200, 0x00020008, 0x08020208,
  61. 0x08000200, 0x08000008, 0x00000200, 0x00000000, 0x08020008, 0x08000208, 0x00020000, 0x08000000, 0x08020208,
  62. 0x00000008, 0x00020208, 0x00020200, 0x08000008, 0x08020000, 0x08000208, 0x00000208, 0x08020000, 0x00020208,
  63. 0x00000008, 0x08020008, 0x00020200 ];
  64. private static const SP4:Array = [ 0x00802001, 0x00002081, 0x00002081, 0x00000080, 0x00802080, 0x00800081, 0x00800001,
  65. 0x00002001, 0x00000000, 0x00802000, 0x00802000, 0x00802081, 0x00000081, 0x00000000, 0x00800080, 0x00800001,
  66. 0x00000001, 0x00002000, 0x00800000, 0x00802001, 0x00000080, 0x00800000, 0x00002001, 0x00002080, 0x00800081,
  67. 0x00000001, 0x00002080, 0x00800080, 0x00002000, 0x00802080, 0x00802081, 0x00000081, 0x00800080, 0x00800001,
  68. 0x00802000, 0x00802081, 0x00000081, 0x00000000, 0x00000000, 0x00802000, 0x00002080, 0x00800080, 0x00800081,
  69. 0x00000001, 0x00802001, 0x00002081, 0x00002081, 0x00000080, 0x00802081, 0x00000081, 0x00000001, 0x00002000,
  70. 0x00800001, 0x00002001, 0x00802080, 0x00800081, 0x00002001, 0x00002080, 0x00800000, 0x00802001, 0x00000080,
  71. 0x00800000, 0x00002000, 0x00802080 ];
  72. private static const SP5:Array = [ 0x00000100, 0x02080100, 0x02080000, 0x42000100, 0x00080000, 0x00000100, 0x40000000,
  73. 0x02080000, 0x40080100, 0x00080000, 0x02000100, 0x40080100, 0x42000100, 0x42080000, 0x00080100, 0x40000000,
  74. 0x02000000, 0x40080000, 0x40080000, 0x00000000, 0x40000100, 0x42080100, 0x42080100, 0x02000100, 0x42080000,
  75. 0x40000100, 0x00000000, 0x42000000, 0x02080100, 0x02000000, 0x42000000, 0x00080100, 0x00080000, 0x42000100,
  76. 0x00000100, 0x02000000, 0x40000000, 0x02080000, 0x42000100, 0x40080100, 0x02000100, 0x40000000, 0x42080000,
  77. 0x02080100, 0x40080100, 0x00000100, 0x02000000, 0x42080000, 0x42080100, 0x00080100, 0x42000000, 0x42080100,
  78. 0x02080000, 0x00000000, 0x40080000, 0x42000000, 0x00080100, 0x02000100, 0x40000100, 0x00080000, 0x00000000,
  79. 0x40080000, 0x02080100, 0x40000100 ];
  80. private static const SP6:Array = [ 0x20000010, 0x20400000, 0x00004000, 0x20404010, 0x20400000, 0x00000010, 0x20404010,
  81. 0x00400000, 0x20004000, 0x00404010, 0x00400000, 0x20000010, 0x00400010, 0x20004000, 0x20000000, 0x00004010,
  82. 0x00000000, 0x00400010, 0x20004010, 0x00004000, 0x00404000, 0x20004010, 0x00000010, 0x20400010, 0x20400010,
  83. 0x00000000, 0x00404010, 0x20404000, 0x00004010, 0x00404000, 0x20404000, 0x20000000, 0x20004000, 0x00000010,
  84. 0x20400010, 0x00404000, 0x20404010, 0x00400000, 0x00004010, 0x20000010, 0x00400000, 0x20004000, 0x20000000,
  85. 0x00004010, 0x20000010, 0x20404010, 0x00404000, 0x20400000, 0x00404010, 0x20404000, 0x00000000, 0x20400010,
  86. 0x00000010, 0x00004000, 0x20400000, 0x00404010, 0x00004000, 0x00400010, 0x20004010, 0x00000000, 0x20404000,
  87. 0x20000000, 0x00400010, 0x20004010 ];
  88. private static const SP7:Array = [ 0x00200000, 0x04200002, 0x04000802, 0x00000000, 0x00000800, 0x04000802, 0x00200802,
  89. 0x04200800, 0x04200802, 0x00200000, 0x00000000, 0x04000002, 0x00000002, 0x04000000, 0x04200002, 0x00000802,
  90. 0x04000800, 0x00200802, 0x00200002, 0x04000800, 0x04000002, 0x04200000, 0x04200800, 0x00200002, 0x04200000,
  91. 0x00000800, 0x00000802, 0x04200802, 0x00200800, 0x00000002, 0x04000000, 0x00200800, 0x04000000, 0x00200800,
  92. 0x00200000, 0x04000802, 0x04000802, 0x04200002, 0x04200002, 0x00000002, 0x00200002, 0x04000000, 0x04000800,
  93. 0x00200000, 0x04200800, 0x00000802, 0x00200802, 0x04200800, 0x00000802, 0x04000002, 0x04200802, 0x04200000,
  94. 0x00200800, 0x00000000, 0x00000002, 0x04200802, 0x00000000, 0x00200802, 0x04200000, 0x00000800, 0x04000002,
  95. 0x04000800, 0x00000800, 0x00200002 ];
  96. private static const SP8:Array = [ 0x10001040, 0x00001000, 0x00040000, 0x10041040, 0x10000000, 0x10001040, 0x00000040,
  97. 0x10000000, 0x00040040, 0x10040000, 0x10041040, 0x00041000, 0x10041000, 0x00041040, 0x00001000, 0x00000040,
  98. 0x10040000, 0x10000040, 0x10001000, 0x00001040, 0x00041000, 0x00040040, 0x10040040, 0x10041000, 0x00001040,
  99. 0x00000000, 0x00000000, 0x10040040, 0x10000040, 0x10001000, 0x00041040, 0x00040000, 0x00041040, 0x00040000,
  100. 0x10041000, 0x00001000, 0x00000040, 0x10040040, 0x00001000, 0x00041040, 0x10001000, 0x00000040, 0x10000040,
  101. 0x10040000, 0x10040040, 0x10000000, 0x00040000, 0x10001040, 0x00000000, 0x10041040, 0x00040040, 0x10000040,
  102. 0x10040000, 0x10001000, 0x10001040, 0x00000000, 0x10041040, 0x00041000, 0x00041000, 0x00001040, 0x00001040,
  103. 0x00040040, 0x10000000, 0x10041000 ];
  104. protected var key:ByteArray;
  105. protected var encKey:Array;
  106. protected var decKey:Array;
  107. public function DESKey(key:ByteArray) {
  108. this.key = key;
  109. this.encKey = generateWorkingKey(true, key, 0);
  110. this.decKey = generateWorkingKey(false, key, 0);
  111. }
  112. public function getBlockSize():uint
  113. {
  114. return 8;
  115. }
  116. public function decrypt(block:ByteArray, index:uint=0):void
  117. {
  118. desFunc(decKey, block, index, block, index);
  119. }
  120. public function dispose():void
  121. {
  122. var i:uint=0;
  123. for (i=0;i<encKey.length;i++) { encKey[i]=0; }
  124. for (i=0;i<decKey.length;i++) { decKey[i]=0; }
  125. encKey=null;
  126. decKey=null;
  127. for (i=0;i<key.length;i++) { key[i]=0; }
  128. key.length = 0;
  129. key = null;
  130. Memory.gc();
  131. }
  132. public function encrypt(block:ByteArray, index:uint=0):void
  133. {
  134. desFunc(encKey, block, index, block, index);
  135. }
  136. /**
  137. * generate an integer based working key based on our secret key and what we
  138. * processing we are planning to do.
  139. *
  140. * Acknowledgements for this routine go to James Gillogly & Phil Karn.
  141. */
  142. protected function generateWorkingKey(encrypting:Boolean, key:ByteArray, off:uint):Array
  143. {
  144. //int[] newKey = new int[32];
  145. var newKey:Array = [];
  146. //boolean[] pc1m = new boolean[56], pcr = new boolean[56];
  147. var pc1m:ByteArray = new ByteArray;
  148. var pcr:ByteArray = new ByteArray;
  149. var l:uint;
  150. for (var j:uint = 0; j < 56; j++)
  151. {
  152. l = pc1[j];
  153. pc1m[j] = ((key[off + (l >>> 3)] & bytebit[l & 07]) != 0);
  154. }
  155. for (var i:uint = 0; i < 16; i++)
  156. {
  157. var m:uint;
  158. var n:uint;
  159. if (encrypting)
  160. {
  161. m = i << 1;
  162. }
  163. else
  164. {
  165. m = (15 - i) << 1;
  166. }
  167. n = m + 1;
  168. newKey[m] = newKey[n] = 0;
  169. for (j = 0; j < 28; j++)
  170. {
  171. l = j + totrot[i];
  172. if (l < 28)
  173. {
  174. pcr[j] = pc1m[l];
  175. }
  176. else
  177. {
  178. pcr[j] = pc1m[l - 28];
  179. }
  180. }
  181. for (j = 28; j < 56; j++)
  182. {
  183. l = j + totrot[i];
  184. if (l < 56)
  185. {
  186. pcr[j] = pc1m[l];
  187. }
  188. else
  189. {
  190. pcr[j] = pc1m[l - 28];
  191. }
  192. }
  193. for (j = 0; j < 24; j++)
  194. {
  195. if (pcr[pc2[j]])
  196. {
  197. newKey[m] |= bigbyte[j];
  198. }
  199. if (pcr[pc2[j + 24]])
  200. {
  201. newKey[n] |= bigbyte[j];
  202. }
  203. }
  204. }
  205. //
  206. // store the processed key
  207. //
  208. for (i = 0; i != 32; i += 2)
  209. {
  210. var i1:uint;
  211. var i2:uint;
  212. i1 = newKey[i];
  213. i2 = newKey[i + 1];
  214. newKey[i] = ((i1 & 0x00fc0000) << 6) | ((i1 & 0x00000fc0) << 10) | ((i2 & 0x00fc0000) >>> 10)
  215. | ((i2 & 0x00000fc0) >>> 6);
  216. newKey[i + 1] = ((i1 & 0x0003f000) << 12) | ((i1 & 0x0000003f) << 16) | ((i2 & 0x0003f000) >>> 4)
  217. | (i2 & 0x0000003f);
  218. }
  219. return newKey;
  220. }
  221. /**
  222. * the DES engine.
  223. */
  224. protected function desFunc(wKey:Array, inp:ByteArray, inOff:uint, out:ByteArray, outOff:uint):void
  225. {
  226. var work:uint;
  227. var right:uint;
  228. var left:uint;
  229. left = (inp[inOff + 0] & 0xff) << 24;
  230. left |= (inp[inOff + 1] & 0xff) << 16;
  231. left |= (inp[inOff + 2] & 0xff) << 8;
  232. left |= (inp[inOff + 3] & 0xff);
  233. right = (inp[inOff + 4] & 0xff) << 24;
  234. right |= (inp[inOff + 5] & 0xff) << 16;
  235. right |= (inp[inOff + 6] & 0xff) << 8;
  236. right |= (inp[inOff + 7] & 0xff);
  237. work = ((left >>> 4) ^ right) & 0x0f0f0f0f;
  238. right ^= work;
  239. left ^= (work << 4);
  240. work = ((left >>> 16) ^ right) & 0x0000ffff;
  241. right ^= work;
  242. left ^= (work << 16);
  243. work = ((right >>> 2) ^ left) & 0x33333333;
  244. left ^= work;
  245. right ^= (work << 2);
  246. work = ((right >>> 8) ^ left) & 0x00ff00ff;
  247. left ^= work;
  248. right ^= (work << 8);
  249. right = ((right << 1) | ((right >>> 31) & 1)) & 0xffffffff;
  250. work = (left ^ right) & 0xaaaaaaaa;
  251. left ^= work;
  252. right ^= work;
  253. left = ((left << 1) | ((left >>> 31) & 1)) & 0xffffffff;
  254. for (var round:uint = 0; round < 8; round++)
  255. {
  256. var fval:uint;
  257. work = (right << 28) | (right >>> 4);
  258. work ^= wKey[round * 4 + 0];
  259. fval = SP7[work & 0x3f];
  260. fval |= SP5[(work >>> 8) & 0x3f];
  261. fval |= SP3[(work >>> 16) & 0x3f];
  262. fval |= SP1[(work >>> 24) & 0x3f];
  263. work = right ^ wKey[round * 4 + 1];
  264. fval |= SP8[work & 0x3f];
  265. fval |= SP6[(work >>> 8) & 0x3f];
  266. fval |= SP4[(work >>> 16) & 0x3f];
  267. fval |= SP2[(work >>> 24) & 0x3f];
  268. left ^= fval;
  269. work = (left << 28) | (left >>> 4);
  270. work ^= wKey[round * 4 + 2];
  271. fval = SP7[work & 0x3f];
  272. fval |= SP5[(work >>> 8) & 0x3f];
  273. fval |= SP3[(work >>> 16) & 0x3f];
  274. fval |= SP1[(work >>> 24) & 0x3f];
  275. work = left ^ wKey[round * 4 + 3];
  276. fval |= SP8[work & 0x3f];
  277. fval |= SP6[(work >>> 8) & 0x3f];
  278. fval |= SP4[(work >>> 16) & 0x3f];
  279. fval |= SP2[(work >>> 24) & 0x3f];
  280. right ^= fval;
  281. }
  282. right = (right << 31) | (right >>> 1);
  283. work = (left ^ right) & 0xaaaaaaaa;
  284. left ^= work;
  285. right ^= work;
  286. left = (left << 31) | (left >>> 1);
  287. work = ((left >>> 8) ^ right) & 0x00ff00ff;
  288. right ^= work;
  289. left ^= (work << 8);
  290. work = ((left >>> 2) ^ right) & 0x33333333;
  291. right ^= work;
  292. left ^= (work << 2);
  293. work = ((right >>> 16) ^ left) & 0x0000ffff;
  294. left ^= work;
  295. right ^= (work << 16);
  296. work = ((right >>> 4) ^ left) & 0x0f0f0f0f;
  297. left ^= work;
  298. right ^= (work << 4);
  299. out[outOff + 0] = ((right >>> 24) & 0xff);
  300. out[outOff + 1] = ((right >>> 16) & 0xff);
  301. out[outOff + 2] = ((right >>> 8) & 0xff);
  302. out[outOff + 3] = (right & 0xff);
  303. out[outOff + 4] = ((left >>> 24) & 0xff);
  304. out[outOff + 5] = ((left >>> 16) & 0xff);
  305. out[outOff + 6] = ((left >>> 8) & 0xff);
  306. out[outOff + 7] = (left & 0xff);
  307. }
  308. public function toString():String {
  309. return "des";
  310. }
  311. }
  312. }