PageRenderTime 339ms CodeModel.GetById 112ms app.highlight 64ms RepoModel.GetById 97ms app.codeStats 1ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 88 lines | 63 code | 6 blank | 19 comment | 10 complexity | 55fb535069345a88de9717267bbfff95 MD5 | raw file
 1/**
 2 * TripleDESKey
 3 * 
 4 * An Actionscript 3 implementation of Triple 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 */
14package com.hurlant.crypto.symmetric
15{
16	import flash.utils.ByteArray;
17	import com.hurlant.util.Memory;
18	import com.hurlant.util.Hex;
19
20	public class TripleDESKey extends DESKey
21	{
22		protected var encKey2:Array;
23		protected var encKey3:Array;
24		protected var decKey2:Array;
25		protected var decKey3:Array;
26		
27		/**
28		 * This supports 2TDES and 3TDES.
29		 * If the key passed is 128 bits, 2TDES is used.
30		 * If the key has 192 bits, 3TDES is used.
31		 * Other key lengths give "undefined" results.
32		 */
33		public function TripleDESKey(key:ByteArray)
34		{
35			super(key);
36			encKey2 = generateWorkingKey(false, key, 8);
37			decKey2 = generateWorkingKey(true, key, 8);
38			if (key.length>16) {
39				encKey3 = generateWorkingKey(true, key, 16);
40				decKey3 = generateWorkingKey(false, key, 16);
41			} else {
42				encKey3 = encKey;
43				decKey3 = decKey;
44			}
45		}
46
47		public override function dispose():void
48		{
49			super.dispose();
50			var i:uint = 0;
51			if (encKey2!=null) {
52				for (i=0;i<encKey2.length;i++) { encKey2[i]=0; }
53				encKey2=null;
54			}
55			if (encKey3!=null) {
56				for (i=0;i<encKey3.length;i++) { encKey3[i]=0; }
57				encKey3=null;
58			}
59			if (decKey2!=null) {
60				for (i=0;i<decKey2.length;i++) { decKey2[i]=0; }
61				decKey2=null
62			}
63			if (decKey3!=null) {
64				for (i=0;i<decKey3.length;i++) { decKey3[i]=0; }
65				decKey3=null;
66			}
67			Memory.gc();
68		}
69		
70		public override function encrypt(block:ByteArray, index:uint=0):void
71		{
72			desFunc(encKey, block,index, block,index);
73			desFunc(encKey2, block,index, block,index);
74			desFunc(encKey3, block,index, block,index);
75		}
76		
77		public override function decrypt(block:ByteArray, index:uint=0):void
78		{
79			desFunc(decKey3, block, index, block, index);
80			desFunc(decKey2, block, index, block, index);
81			desFunc(decKey, block, index, block, index);
82		}
83		
84		public override function toString():String {
85			return "3des";
86		}
87	}
88}