PageRenderTime 800ms CodeModel.GetById 658ms app.highlight 4ms RepoModel.GetById 137ms app.codeStats 0ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 58 lines | 43 code | 7 blank | 8 comment | 4 complexity | afede4e57e6ec738b4d24cca54b25290 MD5 | raw file
 1/**
 2 * CTRMode
 3 * 
 4 * An ActionScript 3 implementation of the counter confidentiality mode
 5 * Copyright (c) 2007 Henri Torgemane
 6 * 
 7 * See LICENSE.txt for full license information.
 8 */
 9package com.hurlant.crypto.symmetric
10{
11	import flash.utils.ByteArray;
12
13	public class CTRMode extends IVMode implements IMode
14	{
15
16		public function CTRMode(key:ISymmetricKey, padding:IPad = null) {
17			super(key, padding);
18		}
19		
20		public function encrypt(src:ByteArray):void
21		{
22			padding.pad(src);
23			var vector:ByteArray = getIV4e();
24			core(src, vector);
25		}
26		
27		public function decrypt(src:ByteArray):void
28		{
29			var vector:ByteArray = getIV4d();
30			core(src, vector);
31			padding.unpad(src);
32		}
33		
34		private function core(src:ByteArray, iv:ByteArray):void {
35			var X:ByteArray = new ByteArray;
36			var Xenc:ByteArray = new ByteArray;
37			X.writeBytes(iv);
38			for (var i:uint=0;i<src.length;i+=blockSize) {
39				Xenc.position=0;
40				Xenc.writeBytes(X);
41				key.encrypt(Xenc);
42				for (var j:uint=0;j<blockSize;j++) {
43					src[i+j] ^= Xenc[j];
44				}
45				
46 				for (j=blockSize-1;j>=0;--j) {
47					X[j]++;
48					if (X[j]!=0)
49						break;
50				}
51			}
52		}
53		public function toString():String {
54			return key.toString()+"-ctr";
55		}
56		
57	}
58}