import java.io.*;
import java.security.*;
import javax.crypto.*;
class UseCipher{
public static void main(String args[])
{
try{
//Create keys and key generators
KeyGenerator KG = KeyGenerator.getInstance("DES");
Key sigKey = KG.generateKey();
//Create Cipher objects for encryption and decryption
Cipher c1 = Cipher.getInstance("DES");
Cipher c2 = Cipher.getInstance("DES");
//Initialize Cipher objects
c1.init(Cipher.ENCRYPT_MODE, sigKey);
c2.init(Cipher.DECRYPT_MODE, sigKey);
//Create the encrypting cipher stream
FileInputStream fis = new FileInputStream("C:/encryptdata.txt");
CipherInputStream cis1 = new CipherInputStream(fis, c1);
//Create the decrypting cipher stream
CipherInputStream cis2 = new CipherInputStream(cis1, c2);
//Write the decrypted data to an output file
FileOutputStream fos = new FileOutputStream("C:/decryptdata.txt");
byte[] bytearray = new byte[1024];
int i = cis2.read(bytearray);
while(i != -1)
{
fos.write(bytearray, 0, i);
i = cis2.read(bytearray);
}
fos.close();
cis1.close();
cis2.close();
}
catch(Exception e)
{
System.out.println("Exception: " + e);
}
}
}