package org.bouncycastle.crypto.engines;

import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.params.KeyParameter;

/**
 * a class that provides a basic DESede (or Triple DES) engine.
 */
public class DESedeEngine
    extends DESEngine
{
    protected static final int  BLOCK_SIZE = 8;

    private int[]               workingKey1 = null;
    private int[]               workingKey2 = null;
    private int[]               workingKey3 = null;

    private boolean             forEncryption;

    /**
     * standard constructor.
     */
    public DESedeEngine()
    {
    }

    /**
     * initialise a DESede cipher.
     *
     * @param encrypting whether or not we are for encryption.
     * @param params the parameters required to set up the cipher.
     * @exception IllegalArgumentException if the params argument is
     * inappropriate.
     */
    public void init(
        boolean           encrypting,
        CipherParameters  params)
    {
        if (!(params instanceof KeyParameter))
        {
            throw new IllegalArgumentException("invalid parameter passed to DESede init - " + params.getClass().getName());
        }

        byte[]      keyMaster = ((KeyParameter)params).getKey();
        byte[]      key1 = new byte[8], key2 = new byte[8], key3 = new byte[8];

        if (keyMaster.length > 24)
        {
            throw new IllegalArgumentException("key size greater than 24 bytes");
        }
        
        this.forEncryption = encrypting;

        if (keyMaster.length == 24)
        {
            System.arraycopy(keyMaster, 0, key1, 0, key1.length);
            System.arraycopy(keyMaster, 8, key2, 0, key2.length);
            System.arraycopy(keyMaster, 16, key3, 0, key3.length);

            workingKey1 = generateWorkingKey(encrypting, key1);
            workingKey2 = generateWorkingKey(!encrypting, key2);
            workingKey3 = generateWorkingKey(encrypting, key3);
        }
        else    // 16 byte key
        {
            System.arraycopy(keyMaster, 0, key1, 0, key1.length);
            System.arraycopy(keyMaster, 8, key2, 0, key2.length);

            workingKey1 = generateWorkingKey(encrypting, key1);
            workingKey2 = generateWorkingKey(!encrypting, key2);
            workingKey3 = workingKey1;
        }
    }

    public String getAlgorithmName()
    {
        return "DESede";
    }

    public int getBlockSize()
    {
        return BLOCK_SIZE;
    }

    public int processBlock(
        byte[] in,
        int inOff,
        byte[] out,
        int outOff)
    {
        if (workingKey1 == null)
        {
            throw new IllegalStateException("DESede engine not initialised");
        }

        if ((inOff + BLOCK_SIZE) > in.length)
        {
            throw new DataLengthException("input buffer too short");
        }

        if ((outOff + BLOCK_SIZE) > out.length)
        {
            throw new DataLengthException("output buffer too short");
        }

        if (forEncryption)
        {
            desFunc(workingKey1, in, inOff, out, outOff);
            desFunc(workingKey2, out, outOff, out, outOff);
            desFunc(workingKey3, out, outOff, out, outOff);
        }
        else
        {
            desFunc(workingKey3, in, inOff, out, outOff);
            desFunc(workingKey2, out, outOff, out, outOff);
            desFunc(workingKey1, out, outOff, out, outOff);
        }

        return BLOCK_SIZE;
    }

    public void reset()
    {
    }
}

@@ -1230,29 +1230,24 @@
       byte%5B%5D
-     
  keyMaster =
@@ -1283,88 +1283,8 @@
 y();
-%0A        byte%5B%5D      key1 = new byte%5B8%5D, key2 = new byte%5B8%5D, key3 = new byte%5B8%5D;
 %0A%0A  
@@ -1418,24 +1418,16 @@
       %7D%0A
-        
 %0A       
@@ -1473,50 +1473,35 @@
     
-if (keyMaster.length == 24)%0A        %7B%0A    
+byte%5B%5D key1 = new byte%5B8%5D;%0A
     
@@ -1570,70 +1570,96 @@
     
-    System.arraycopy(keyMaster, 8, key2, 0, key2.length);%0A    
+workingKey1 = generateWorkingKey(encrypting, key1);%0A%0A        byte%5B%5D key2 = new byte%5B8%5D;%0A
     
@@ -1694,24 +1694,23 @@
 er, 
-16
+8
 , key
-3
+2
 , 0, key
 3.le
@@ -1705,17 +1705,17 @@
 , 0, key
-3
+2
 .length)
@@ -1720,77 +1720,8 @@
 h);%0A
-%0A
-            workingKey1 = generateWorkingKey(encrypting, key1);%0A    
     
@@ -1773,35 +1773,33 @@
 ing, key2);%0A
-   
+%0A
         
  workingKey3
@@ -1790,60 +1790,35 @@
     
- workingKey3 = generateWorkingKey(encrypting, key3);
+if (keyMaster.length == 24)
 %0A   
@@ -1814,33 +1814,33 @@
  == 24)%0A        
-%7D
+%7B
 %0A        else   
@@ -1836,105 +1836,37 @@
     
-else    // 16
+   
  byte
+%5B%5D
  key
-%0A        %7B%0A            System.arraycopy(keyMaster, 0, key1, 0, key1.length)
+3 = new byte%5B8%5D
 ;%0A  
@@ -1899,39 +1899,40 @@
 (keyMaster, 
-8
+16
 , key
-2
+3
 , 0, key
 2.length);%0A%0A
@@ -1911,33 +1911,33 @@
 16, key3, 0, key
-2
+3
 .length);%0A%0A     
@@ -1930,17 +1930,16 @@
 ength);%0A
-%0A
         
@@ -1944,33 +1944,33 @@
       workingKey
-1
+3
  = generateWorki
@@ -1990,17 +1990,17 @@
 ing, key
-1
+3
 );%0A     
@@ -2006,64 +2006,50 @@
     
+%7D%0A
     
-workingKey2 = generateWorkingKey(!encrypting, key2);
+    else    // 16 byte key%0A        %7B
 %0A   

Refactor init method