[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Google
  Web www.spinics.net

[GNU Crypto] Odd behavior from PKCS7



Hello,

I'm using GNU-Crypto 1.1.0 on a project that I am working on and I'm
getting some weird things from PKCS7.  I'm encrypting data using the
Blowfish algorithm (with CBC) and padding the remaining data with PKCS7.

My problem comes about when I attempt to pad remaining bytes in a
block.  For whatever reason, PKCS7 is returning a byte array of length
252 instead of 5.  I am thinking there is probably something I am
overlooking in my code, but I can't figure out what it is.  I have tried
varying lengths of information to see maybe if there was just a problem
with return 5 bytes of padding, but I received the same errors with
similarly large returned arrays from PKCS7.


Attached is the class that's having problems.  It's specifically the
encryptStream() class.  

The output it gives is:

Current Block Size 8
Bytes available 3
leftOver length is 3
paddedTemp length is 252
inData length is 8
java.lang.ArrayIndexOutOfBoundsException
        at java.lang.System.arraycopy(Native Method)
        at GCISS_BE.encryptStream(GCISS_BE.java:99)


Everything looked normal until the paddedTemp length is 252.

Also, another question I have is that when I don't call pad.reset(), I
get a java.lang.IllegalStateException however the pad has NOT been
initialized yet.  I didn't think calling pad.reset() would hurt
anything, so I went ahead and did that and it fixed my problem, however
I was still wondering why I would get that if I just instantiated the
PKCS7 class.

Thanks for the help,

Ryan
import gnu.crypto.cipher.*;
import java.io.*;
import java.util.*;
import gnu.crypto.pad.*;
import gnu.crypto.mode.*;
import java.security.*;

class GCISS_BE{
	
	private InputStream in;
	private OutputStream out;
	private IMode mode;
    	private int blockSize;
	private Map attr;
        private IBlockCipher forTesting;

	public GCISS_BE(int alg, byte  pass[], int kSize, boolean encrypt){
			
		
		
		switch(alg){
		

		case 1:
				
			try{
			        
				attr = new HashMap();
				mode = ModeFactory.getInstance("CBC", "Blowfish", 8);
				attr.put(IMode.KEY_MATERIAL, pass);
				attr.put(IMode.CIPHER_BLOCK_SIZE, new Integer(8));
								
				//if encrypt == true then we are encrypting a file
				if(encrypt){
					attr.put(IMode.STATE, new Integer(IMode.ENCRYPTION));
					
					
				}
				else{
					attr.put(IMode.STATE, new Integer(IMode.DECRYPTION));
					
				}
				
				
					mode.init(attr);
				}catch(InvalidKeyException e){System.out.println(e.toString());}	

				blockSize = mode.currentBlockSize();
				break;
			default:
		}
		
	}

	public boolean encryptStream( BufferedInputStream in, String out, Object k, int s ){
                byte []inData = new byte[blockSize];
                byte []outData = new byte[blockSize];
                BufferedOutputStream z;
		IPad pad = PadFactory.getInstance("PKCS7");
		
		pad.reset();
		pad.init(mode.currentBlockSize());
		pad.selfTest();
		System.out.println("Current Block Size " + mode.currentBlockSize());
		
		try{
                z = new BufferedOutputStream(new FileOutputStream(out));

                while(in.available() >= blockSize){
                        

                        in.read(inData, 0, blockSize);
                        mode.update(inData, 0, outData, 0 );
                        z.write(outData, 0, blockSize);

                }
		if((in.available()%blockSize)!= 0){

		    System.out.println("Bytes available " + in.available());			

		        byte leftOver[] = new byte[in.available()];
			
			in.read(leftOver,0,in.available());
			
			byte paddedTemp[] = pad.pad(leftOver, 0, leftOver.length);
			System.arraycopy(leftOver, 0, inData, 0, leftOver.length);
			System.out.println("leftOver length is " + leftOver.length);
			System.out.println("paddedTemp length is " + paddedTemp.length);
			System.out.println("inData length is " + inData.length);
			System.arraycopy(paddedTemp, 0, inData, leftOver.length, inData.length);
			mode.update(inData, 0, outData, 0 );
			z.write(outData, 0, blockSize);
		}
			

                z.flush();
                }catch(IOException e){System.out.println(e.toString());}

                System.out.println("File should now be encrypted here: " + out);
                return true;

        }



	
}	
_______________________________________________
gnu-crypto-discuss mailing list
gnu-crypto-discuss@xxxxxxxxxx
http://mail.nongnu.org/mailman/listinfo/gnu-crypto-discuss

[Home]     [Gnu Classpath]     [Linux Kernel]     [Linux Cryptography]     [Fedora]     [Fedora Directory]     [Red Hat Development]     [Red Hat 9 Bible]     [Fedora Bible]     [Red Hat 9]     [Network Security Reading]

  Powered by Linux