PageRenderTime 418ms CodeModel.GetById 201ms app.highlight 15ms RepoModel.GetById 197ms app.codeStats 1ms

/flash-src/third-party/com/hurlant/crypto/symmetric/OFBMode.as

http://github.com/gimite/web-socket-js
ActionScript | 52 lines | 39 code | 5 blank | 8 comment | 2 complexity | 88a73b5eff4a1f3e960a4bdd3a33a82e MD5 | raw file
 1/**
 2 * OFBMode
 3 * 
 4 * An ActionScript 3 implementation of the OFB 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 OFBMode extends IVMode implements IMode
14	{
15		public function OFBMode(key:ISymmetricKey, padding:IPad=null)
16		{
17			super(key, null);
18		}
19		
20		public function encrypt(src:ByteArray):void
21		{
22			var vector:ByteArray = getIV4e();
23			core(src, vector);
24		}
25		
26		public function decrypt(src:ByteArray):void
27		{
28			var vector:ByteArray = getIV4d();
29			core(src, vector);
30		}
31		
32		private function core(src:ByteArray, iv:ByteArray):void { 
33			var l:uint = src.length;
34			var tmp:ByteArray = new ByteArray;
35			for (var i:uint=0;i<src.length;i+=blockSize) {
36				key.encrypt(iv);
37				tmp.position=0;
38				tmp.writeBytes(iv);
39				var chunk:uint = (i+blockSize<l)?blockSize:l-i;
40				for (var j:uint=0;j<chunk;j++) {
41					src[i+j] ^= iv[j];
42				}
43				iv.position=0;
44				iv.writeBytes(tmp);
45			}
46		}
47		public function toString():String {
48			return key.toString()+"-ofb";
49		}
50		
51	}
52}