PageRenderTime 132ms CodeModel.GetById 70ms app.highlight 4ms RepoModel.GetById 56ms app.codeStats 0ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 55 lines | 40 code | 4 blank | 11 comment | 4 complexity | 46d80b9fea6333667bcad804908a6c29 MD5 | raw file
 1/**
 2 * CBCMode
 3 * 
 4 * An ActionScript 3 implementation of the CBC 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	/**
14	 * CBC confidentiality mode. why not.
15	 */
16	public class CBCMode extends IVMode implements IMode
17	{
18		
19		public function CBCMode(key:ISymmetricKey, padding:IPad = null) {
20			super(key, padding);
21		}
22
23		public function encrypt(src:ByteArray):void {
24			padding.pad(src);
25			var vector:ByteArray = getIV4e();
26			for (var i:uint=0;i<src.length;i+=blockSize) {
27				for (var j:uint=0;j<blockSize;j++) {
28					src[i+j] ^= vector[j];
29				}
30				key.encrypt(src, i);
31				vector.position=0;
32				vector.writeBytes(src, i, blockSize);
33			}
34		}
35		public function decrypt(src:ByteArray):void {
36			var vector:ByteArray = getIV4d();
37			var tmp:ByteArray = new ByteArray;
38			for (var i:uint=0;i<src.length;i+=blockSize) {
39				tmp.position=0;
40				tmp.writeBytes(src, i, blockSize);
41				key.decrypt(src, i);
42				for (var j:uint=0;j<blockSize;j++) {
43					src[i+j] ^= vector[j];
44				}
45				vector.position=0;
46				vector.writeBytes(tmp, 0, blockSize);
47			}
48			padding.unpad(src);
49		}
50		
51		public function toString():String {
52			return key.toString()+"-cbc";
53		}
54	}
55}