// File for encryption,decryption,generating secret key

package extractor;

import java.io.File;
import java.io.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.spec.AlgorithmParameterSpec;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.CipherInputStream;


public class UploadEncrypt
	{
		Cipher ecipher;
		Cipher dcipher;
		byte[] buf = new byte[1024];
		private SecretKey key = null;

		public UploadEncrypt()
			{
			   try 
				   {
					 key = KeyGenerator.getInstance("DES").generateKey();

// Create an 8-byte initialization vector

		  		    byte[] iv = new byte[]{(byte)0x8E, 0x12, 0x39, (byte)0x9C, 0x07, 0x72, 0x6F, 0x5A};
		            AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);

				   ecipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
				   dcipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

// CBC requires an initialization vector

 			       ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
			       dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);

		         }
			 catch (Exception e)
				 {
				    e.printStackTrace();
		         }

			} // UploadEncrypt


	   public boolean encrypt(InputStream in, OutputStream out)
		   {
		     try 
				 {

 // Bytes written to out will be encrypted
  
	  			    out = new CipherOutputStream(out, ecipher);

// Read in the cleartext bytes and write to out to encrypt
				   int numRead = 0;
				   while ((numRead = in.read(buf)) >= 0)
					   {
						  out.write(buf, 0, numRead);
			           }
				   out.close();
		        }
			catch (java.io.IOException e)
				{
			   	   e.printStackTrace();
				   return false;
		        }

		   return true;

	      } // encrypt

	  public  static boolean decrypt(InputStream in, OutputStream out, SecretKey key)
		  {
	        try 
				{
					byte[] iv = new byte[]{(byte) 0x8E, 0x12, 0x39, (byte) 0x9C, 0x07, 0x72, 0x6F, 0x5A};
		            AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
				    Cipher dcipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
		            dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
		            byte[] buf = new byte[1024];

// Bytes read from in will be decrypted

					in = new CipherInputStream(in, dcipher);

// Read in the decrypted bytes and write the cleartext to out

		            int numRead = 0;
				    while ((numRead = in.read(buf)) >= 0)
						{
		                  out.write(buf, 0, numRead);
						}
		            out.close();
				    in.close();

				}
			catch (Exception e)
				{
					e.printStackTrace();
				    return false;
				}
			return true;

        }


	  public SecretKey getKey()
		{
	    	 return this.key;
		} // getKey

	  public Cipher getDcipher()
		{
	  	   return this.dcipher;
		} // getDcipher


	  public boolean deleteEnr(String filename)
		  {
			try
			{
				
				File file = new File(filename);
				file.delete();
			}
		   catch(Exception e)
			   {
			      e.printStackTrace();
				  return false;
			   }

		return true;

	} // deleteEnr


	public boolean checkDelimiter(String record, String delimiter)
		{
			int index = record.indexOf(delimiter);
			if (index <= 0)
				return false;

			return true;
		}//    checkDelimiter


    public boolean readFile(String filePath, String delimiter)
		{
			boolean status = false;
			try
			{
				
			   if (filePath != null)
				   {

					FileReader fileReader = new FileReader(filePath);
					BufferedReader bufferedReader = new BufferedReader(fileReader);
					String strLine = null;
					while ((strLine = bufferedReader.readLine()) != null)
						{
							if (!strLine.equals(""))
						    status = checkDelimiter(strLine.trim(), delimiter);
						}
					fileReader.close();
				   }

             }
			 catch (Exception e)
				 {
		            e.printStackTrace();
				    return status;
		         }

			return status;

		}//    readFile


 } // end of class
