www.pudn.com > encryption.rar > CBCCipher.java


/*
Christoforos Pirillos @ Villanova University - May 1999
based on code from the book "Java Network Programming" by Hughes
*/

package encryption;
/**

/**The CBCCipher class is a Cipher that attaches to an existing
block-cipher and applies CBC stream-encryption to it.*/

public class CBCCipher extends Cipher {

/**The Cipher used for encryption*/
protected Cipher c;

protected byte[] oldEncipher, oldDecipher;

/**The block size of the cipher used*/
protected int bS;

/**Accepts a Cipher to be used and an initialization vector that will be
used to initialize the cipher-block-chaining*/

public CBCCipher (Cipher c, byte[] iv) {
	this.c=c;
	bS=c.blockSize();
	oldEncipher=new byte[bS];
	oldDecipher=new byte[bS];
	if (iv!= null) {
		System.arraycopy (iv,0,oldEncipher,0,bS);
		System.arraycopy (iv,0,oldDecipher,0,bS);
	}
}

/**same as CBCCipher (Cipher, byte[]) but assumes byte[]=0*/

public CBCCipher (Cipher c) {
	this (c,null);
}

/**Encrypts a block using X-OR on the plaintext with the previous
ciphertext block and then encrypt this with the Cipher*/

public void encipherBlock (byte[] plain, int po, byte[] cipher, int co) {
	for (int i=0;i