how to decrypt a file ?

i have encrypted file through following code now i want decrypt same file

p

FileInputStream fin,fin1;

String strUser;

FileOutputStream fout,fout1;

KeyGenerator kg;

Cipher c;

ObjectInputStream obji;

ObjectOutputStream objo;

FileOutputStream fos;

FileInputStream fis;

byte iv[];

/** Creates a new instance of encryptData */

public void encryptData(String filename,String Keyval) {

}

public String encryptFile(String filename,String Keyval) {

String FileName,keyval;

FileName=filename;

keyval=Keyval;

FileInputStream fin,fin1;

String strUser;

FileOutputStream fout,fout1;

KeyGenerator kg;

Cipher c;

ObjectInputStream obji;

ObjectOutputStream objo;

FileOutputStream fos;

FileInputStream fis;

String filepath="";

byte iv[];

System.out.println("FileName " +FileName);

try {

kg = KeyGenerator.getInstance("DES");

c= Cipher.getInstance("DES/ECB/PKCS5Padding");

File fileEn = new File(FileName);

System.out.println("FileName " +fileEn.getName());

Key key = kg.generateKey();

fos=new FileOutputStream("/keyval.stk");

objo=new ObjectOutputStream(fos);

objo.writeObject(key);

objo.close();

c.init(Cipher.ENCRYPT_MODE, key);

fin=new FileInputStream(fileEn);

byte input[] = new byte[fin.available()];

fin.read(input);

byte encrypted[] =c.doFinal(input);

//String str=new String(encrypted);

//iv = c.getIV();

fout1=new FileOutputStream("D:/EncryptedFiles/"+fileEn.getName());

filepath="D:/EncryptedFiles/"+fileEn.getName() +","+"/keyval.stk";

fout1.write(encrypted );

fout1.close();

System.out.println("keyval " +keyval);

} catch (Exception e) {

e.printStackTrace();

}

return filepath;

}

}

[1937 byte] By [kirtesh.lathkara] at [2007-11-27 10:24:14]
# 1

Use

c.init(Cipher.DECRYPT_MODE, key);

using the same key.

P.S. Your code for reading the file is flawed -

1) fin.available() does not guarantee to be the file length but File.length() does.

2) fin.read(input) does not guarantee to read the whole file but DataInputStream.readFully() does.

P.P.S. You should use CipherInputStream and CipherOutputStream and then you won't need to hold the whole file in memory.

sabre150a at 2007-7-28 17:26:42 > top of Java-index,Java Essentials,Java Programming...