mirror of
https://github.com/burrow-dev/burrow-server.git
synced 2026-11-22 01:37:50 -05:00
Brought up to date with the private repository (feea4c7)
This commit is contained in:
1 parent
1aa738d692
commit
1b1df1ae48
7925 files changed
+2617237
-20
No files matched your search
@@ -0,0 +1,87 @@
|
||||
package com.hurlant.crypto.symmetric
|
||||
{
|
||||
import com.hurlant.util.Memory;
|
||||
import flash.utils.ByteArray;
|
||||
|
||||
public class ECBMode implements IMode, ICipher
|
||||
{
|
||||
|
||||
private var key:ISymmetricKey;
|
||||
|
||||
private var padding:IPad;
|
||||
|
||||
public function ECBMode(key:ISymmetricKey, padding:IPad = null)
|
||||
{
|
||||
super();
|
||||
this.key = key;
|
||||
if(padding == null)
|
||||
{
|
||||
padding = new PKCS5(key.getBlockSize());
|
||||
}
|
||||
else
|
||||
{
|
||||
padding.setBlockSize(key.getBlockSize());
|
||||
}
|
||||
this.padding = padding;
|
||||
}
|
||||
|
||||
public function getBlockSize() : uint
|
||||
{
|
||||
return this.key.getBlockSize();
|
||||
}
|
||||
|
||||
public function encrypt(src:ByteArray) : void
|
||||
{
|
||||
this.padding.pad(src);
|
||||
src.position = 0;
|
||||
var blockSize:uint = this.key.getBlockSize();
|
||||
var tmp:ByteArray = new ByteArray();
|
||||
var dst:ByteArray = new ByteArray();
|
||||
for(var i:uint = 0; i < src.length; i += blockSize)
|
||||
{
|
||||
tmp.length = 0;
|
||||
src.readBytes(tmp,0,blockSize);
|
||||
this.key.encrypt(tmp);
|
||||
dst.writeBytes(tmp);
|
||||
}
|
||||
src.length = 0;
|
||||
src.writeBytes(dst);
|
||||
}
|
||||
|
||||
public function decrypt(src:ByteArray) : void
|
||||
{
|
||||
src.position = 0;
|
||||
var blockSize:uint = this.key.getBlockSize();
|
||||
if(src.length % blockSize != 0)
|
||||
{
|
||||
throw new Error("ECB mode cipher length must be a multiple of blocksize " + blockSize);
|
||||
}
|
||||
var tmp:ByteArray = new ByteArray();
|
||||
var dst:ByteArray = new ByteArray();
|
||||
for(var i:uint = 0; i < src.length; i += blockSize)
|
||||
{
|
||||
tmp.length = 0;
|
||||
src.readBytes(tmp,0,blockSize);
|
||||
this.key.decrypt(tmp);
|
||||
dst.writeBytes(tmp);
|
||||
}
|
||||
this.padding.unpad(dst);
|
||||
src.length = 0;
|
||||
src.writeBytes(dst);
|
||||
}
|
||||
|
||||
public function dispose() : void
|
||||
{
|
||||
this.key.dispose();
|
||||
this.key = null;
|
||||
this.padding = null;
|
||||
Memory.gc();
|
||||
}
|
||||
|
||||
public function toString() : String
|
||||
{
|
||||
return this.key.toString() + "-ecb";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user