Problem while doing DESede ( triple DES encryption/ Decryption)
I am trying to do triple DES encryption and Decryption as attached in the code
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class Sample3 {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
byte[] blockFirst = {0x57,0x61,0x74,0x20,0x7a,0x61,0x6c,0x20,0x64,0x69,0x74,0x20,0x62,0x65,0x74,0x65,0x6b,0x65,0x6e,0x65,0x6e,0x20,0x65,0x6e,0x20,0x68,0x6f,0x65,0x20,0x6c,0x61,0x6e,0x67,0x20,0x69,0x73,0x20,0x64,0x69,0x74,0x20,0x61,0x6c,0x6c,0x65,0x73,0x20,0x77,0x65,0x6c,0x20,0x6e,0x69,0x65,0x74,0x20,0x62,0x69,0x6a,0x20,0x65,0x6c,0x6b,0x61};
byte[] blockSecond = {0x61,0x72,0x3f,0x20,0x56,0x6f,0x6c,0x67,0x65,0x6e,0x73,0x20,0x6d,0x69,0x6a,0x20,0x69,0x73,0x20,0x64,0x69,0x74,0x20,0x65,0x65,0x6e,0x20,0x74,0x65,0x6b,0x73,0x74,0x20,0x76,0x61,0x6e,0x20,0x6d,0x61,0x61,0x72,0x20,0x6c,0x69,0x65,0x66,0x73,0x74,0x20,0x31,0x33,0x30,0x20,0x62,0x79,0x74,0x65,0x73,0x20,0x6c,0x61,0x6e,0x67,0x2e};
byte[] finalBlock = { 0x2e,0x2e,0x00,0x00,0x00,0x00,0x00,0x00 };
byte[] key = { (byte)0x1C,(byte)0x8A,(byte)0x2F,(byte)0xBC,(byte)0xC7,(byte)0x07,(byte)0xC4,(byte)0xF2,(byte)0x34,(byte)0xE6,(byte)0x61,(byte)0x3E,(byte)0x25,(byte)0x26,(byte)0x2C,(byte)0xE0,(byte)0x04,(byte)0xE9,(byte)0x1A,(byte)0x01,(byte)0x2F,(byte)0x8A,(byte)0xE9,(byte)0xD9};
byte[] blockFirstEncry = cbcDES(blockFirst,new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },key);
byte[] ivBytes1 = new byte[8];
for(int i=0; i<8; i++)
ivBytes1 = blockFirstEncry[i+blockFirstEncry.length - 8];
byte[] blockSecondEncry = cbcDES(blockSecond,ivBytes1,key,2);
byte[] ivBytes2 = new byte[8];
for(int i=0; i<8; i++)
ivBytes2 = blockSecondEncry[i+blockSecondEncry.length - 8];
byte[] finalBlockEncrypt = cbcDES(finalBlock,ivBytes2,key,3);
byte[] blockFirstEncryDecrypt = cbcDESDecry(blockFirstEncry, new byte[]{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },key,1);
byte[] ivBytes3 = new byte[8];
for(int i=0; i<8; i++)
ivBytes3 = blockFirstEncryDecrypt[i+blockFirstEncryDecrypt.length - 8];
byte[] blockSecondEncryDecrypt = cbcDESDecry(blockSecondEncry, ivBytes3, key,2);
byte[] ivBytes4 = new byte[8];
for(int i=0; i<8; i++)
ivBytes4 = blockSecondEncryDecrypt[i+blockSecondEncryDecrypt.length - 8];
byte[] finalBlockencryDecrypt = cbcDESDecry(finalBlockEncrypt, ivBytes4, key,3);
System.out.println(" First block " + toHexString(blockFirstEncryDecrypt));
System.out.println(" Second block " + toHexString(blockSecondEncryDecrypt));
System.out.println(" Final block " + toHexString(finalBlockencryDecrypt));
}
static byte[] cbcDES(byte[] data, byte[] ivBytes, byte[] key) throws Exception{
IvParameterSpec iv = new IvParameterSpec(ivBytes);
SecretKey keyForTDES1 = new SecretKeySpec(key,"DESede");
Cipher cipher1 = Cipher.getInstance("DESede/CBC/NoPadding", "SunJCE");
cipher1.init(Cipher.ENCRYPT_MODE,keyForTDES1,iv);
byte[] encryptBlock = null;
encryptBlock = cipher1.doFinal(data);
return encryptBlock;
}
static byte[] cbcDESDecry(byte[] data, byte[] ivBytes, byte[] key) throws Exception{
IvParameterSpec iv1 = new IvParameterSpec(ivBytes);
SecretKey keyForTDES1 = new SecretKeySpec(key,"DESede");
Cipher cipher1 = Cipher.getInstance("DESede/CBC/NoPadding", "SunJCE");
cipher1.init(Cipher.DECRYPT_MODE,keyForTDES1,iv1);
byte[] decryptBlock = null;
decryptBlock = cipher1.doFinal(data);
return decryptBlock;
}
public static String toHexString ( byte[] b )
{
StringBuffer sb = new StringBuffer( b.length * 2 );
for ( int i=0; i<b.length; i++ )
{
// look up high nibble char
sb.append( hexChar [( b & 0xf0 ) >>> 4] );
// look up low nibble char
sb.append( hexChar [b & 0x0f] );
}
return sb.toString();
}
// table to convert a nibble to a hex char.
static char[] hexChar = {
'0' , '1' , '2' , '3' ,
'4' , '5' , '6' , '7' ,
'8' , '9' , 'a' , 'b' ,
'c' , 'd' , 'e' , 'f'};
}
Decryption of First Block is done correctly, but the second block and final block is not decrypting correctly. please help me by pointing the problem.

