READING FROM FILE DECRYPTION ERROR

Hi,

I can successfully encode and write to a file using DESede and tested my code using a standard String encode/decode test. However when reading from the file again my message always is a blank String

cipher.init(Cipher.ENCRYPT_MODE, key);

CipherInputStream cis = new CipherInputStream(fis, cipher);

byte data[] = new byte[cis.available()];

cis.read(data);

String msg = decrypt(data);

System.out.println("msg: "+msg);

System.out.println("msgbte: "+msg.hashCode());

cis.close();

My decrypt method is here

cipher.init(Cipher.DECRYPT_MODE, key);

byte[] recoveredBytes = cipher.doFinal(encryptionBytes);

String recovered = new String(recoveredBytes);

return recovered;

Note: This code doesnt return any Exceptions however the hexCode is always 0. What can i do?

[878 byte] By [sezertiia] at [2007-10-3 4:01:54]
# 1
For a start you should get rid of the available() call, which has practically no defined semantics at all, and none that are relevant to this task. Use File.length().
ejpa at 2007-7-14 22:01:13 > top of Java-index,Security,Cryptography...
# 2

Also,

cis.read(data);

does not guarantee to read ALL the bytes of the file even if the buffer length is the length of the file. You need to keep reading from the file until all your file has been read. For example

for (int totalRead = 0; totalRead < data.length;)

{

totalRead += cis.read(data, totalRead, data.length - totalRead);

}

sabre150a at 2007-7-14 22:01:13 > top of Java-index,Security,Cryptography...
# 3

I'm sorry, I can't make any sense of this. You have one fragment which appears to be encrypting an InputStream; usually you decrypt InputStream and encrypt an OutputStream. Then you have another fragment which appears unrelated to the first fragment; and finally, the text of your post does not relate to any of it.

ghstarka at 2007-7-14 22:01:13 > top of Java-index,Security,Cryptography...