Bouncy Castle issue and Triple DES

Hi I have a client using Triple DES. I know I know if AES I would not have a problem it is a older solution integration. I have code below that Takes in a file and writes to another. On the decrypt method I don’t understand the 2nd parameter for length of the file. Please help. The error I get is the following: Exception in thread “main” org.bouncycastle.crypto.DataLengthException: last block incomplete in decryption at org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher.doFinal(Unknown Source) at DESede_BC.decrypt(DESede_BC.java:102) at DESede_BC.main(DESede_BC.java:120)

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;



import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.ShortBufferException;
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.engines.DESedeEngine;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;

public class DESede_BC {

PaddedBufferedBlockCipher encryptCipher;
PaddedBufferedBlockCipher decryptCipher;

// Buffers used to transport the bytes from one stream to another
byte[] buf = new byte[8];       //input buffer - block size length
byte[] obuf = new byte[557];    //output buffer

byte[] key = null;              //the key

public DESede_BC(){
    //use a default 192 bit key
    key = "thekey".getBytes();
    InitCiphers();
}
public DESede_BC(byte[] keyBytes){
    key = new byte[keyBytes.length];
    System.arraycopy(keyBytes, 0 , key, 0, keyBytes.length);
    InitCiphers();
}

private void InitCiphers(){
    encryptCipher = new PaddedBufferedBlockCipher(new DESedeEngine());
    encryptCipher.init(true, new KeyParameter(key));
    decryptCipher =  new PaddedBufferedBlockCipher(new DESedeEngine());
    decryptCipher.init(false, new KeyParameter(key));
}

public void ResetCiphers() {
    if(encryptCipher!=null)
        encryptCipher.reset();
    if(decryptCipher!=null)
        decryptCipher.reset();
}

public void encrypt(InputStream in, long length, OutputStream out)
throws ShortBufferException, 
    IllegalBlockSizeException,
    BadPaddingException,
    DataLengthException,
    IllegalStateException,
    InvalidCipherTextException
{
    try {
    // Bytes written to out will be encrypted
    // Read in the cleartext bytes from in InputStream and
    //      write them encrypted to out OutputStream

    int noBytesRead = 0;        //number of bytes read from input
    int noBytesProcessed = 0;   //number of bytes processed

    while ((noBytesRead = in.read(buf)) >= 0) {
        noBytesProcessed =
                encryptCipher.processBytes(buf, 0, noBytesRead, obuf, 0);
        out.write(obuf, 0, noBytesProcessed);
    }
     noBytesProcessed =
             encryptCipher.doFinal(obuf, 0);

     out.write(obuf, 0, noBytesProcessed);

    out.flush();
}
catch (java.io.IOException e) {
    System.out.println(e.getMessage());
}
}
    public void decrypt(InputStream in,long length, OutputStream out)
   throws ShortBufferException, IllegalBlockSizeException,  BadPaddingException,
        DataLengthException, IllegalStateException, InvalidCipherTextException
    {
        try {
        // Bytes read from in will be decrypted
        // Read in the decrypted bytes from in InputStream and and
        //      write them in cleartext to out OutputStream

        int noBytesRead = 0;        //number of bytes read from input
        int noBytesProcessed = 0;   //number of bytes processed

        while ((noBytesRead = in.read(buf)) >= 0) {
                noBytesProcessed = decryptCipher.processBytes(buf, 0, noBytesRead, obuf, 0);
                out.write(obuf, 0, noBytesProcessed);
        }
        noBytesProcessed = decryptCipher.doFinal(obuf, 0);
        out.write(obuf, 0, noBytesProcessed);

        out.flush();
    }
    catch (java.io.IOException e) {
         System.out.println(e.getMessage());
    }
}

public static void main(String... args)
        throws Exception
      {

    DESede_BC d = new DESede_BC();
FileInputStream fis2 = new FileInputStream("c:\2.in");
FileOutputStream fos2 = new FileOutputStream("c:\decrypted.txt");

    d.decrypt(fis2, new Long("128"), fos2);



      }

}

Answer

I think you need to decode your input into Base64 before doFinal:

byte[] obuf = Base64.decode(obuf, Base64.NO_OPTIONS);
byte[] decValue = c.doFinal(obuf);

Leave a Reply

Your email address will not be published. Required fields are marked *