4
* An abstract class for confidentialy modes that rely on an initialization vector.
5
* Copyright (c) 2007 Henri Torgemane
7
* See LICENSE.txt for full license information.
9
package com.hurlant.crypto.symmetric
11
import com.hurlant.crypto.prng.Random;
12
import com.hurlant.crypto.tests.TestCase;
13
import com.hurlant.util.Memory;
15
import flash.utils.ByteArray;
18
* An "abtract" class to avoid redundant code in subclasses
22
protected var key:ISymmetricKey;
23
protected var padding:IPad;
24
// random generator used to generate IVs
25
protected var prng:Random;
26
// optional static IV. used for testing only.
27
protected var iv:ByteArray;
28
// generated IV is stored here.
29
protected var lastIV:ByteArray;
30
protected var blockSize:uint;
33
public function IVMode(key:ISymmetricKey, padding:IPad = null) {
35
blockSize = key.getBlockSize();
36
if (padding == null) {
37
padding = new PKCS5(blockSize);
39
padding.setBlockSize(blockSize);
41
this.padding = padding;
45
lastIV = new ByteArray;
48
public function getBlockSize():uint {
49
return key.getBlockSize();
51
public function dispose():void {
54
for (i=0;i<iv.length;i++) {
55
iv[i] = prng.nextByte();
61
for (i=0;i<iv.length;i++) {
62
lastIV[i] = prng.nextByte();
75
* Optional function to force the IV value.
76
* Normally, an IV gets generated randomly at every encrypt() call.
77
* Also, use this to set the IV before calling decrypt()
78
* (if not set before decrypt(), the IV is read from the beginning of the stream.)
80
public function set IV(value:ByteArray):void {
83
lastIV.writeBytes(iv);
85
public function get IV():ByteArray {
89
protected function getIV4e():ByteArray {
90
var vec:ByteArray = new ByteArray;
94
prng.nextBytes(vec, blockSize);
97
lastIV.writeBytes(vec);
100
protected function getIV4d():ByteArray {
101
var vec:ByteArray = new ByteArray;
105
throw new Error("an IV must be set before calling decrypt()");
b'\\ No newline at end of file'