Cannot run public class in one .java from another

Posted by DIOS on Stack Overflow See other posts from Stack Overflow or by DIOS
Published on 2013-11-06T21:38:42Z Indexed on 2013/11/06 21:53 UTC
Read the original article Hit count: 299

Filed under:

I have created a basic program that takes whatever is input into two textfields and exports them to a file. I would now like to encrypt that file, and alredy have the encryptor. The problem is that I cannot call it. Here is my code for the encryptor:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.*;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.spec.SecretKeySpec;

public class FileEncryptor {

private String algo;
private File file;

public FileEncryptor(String algo,String path) {
this.algo=algo; //setting algo
this.file=new File(path); //settong file
}

 public void encrypt() throws Exception{
    //opening streams
     FileInputStream fis =new FileInputStream(file);
     file=new File(file.getAbsolutePath());
     FileOutputStream fos =new FileOutputStream(file);
     //generating key
     byte k[] = "HignDlPs".getBytes();   
     SecretKeySpec key = new SecretKeySpec(k,algo.split("/")[0]);  
     //creating and initialising cipher and cipher streams
     Cipher encrypt =  Cipher.getInstance(algo);  
     encrypt.init(Cipher.ENCRYPT_MODE, key);  
     CipherOutputStream cout=new CipherOutputStream(fos, encrypt);

     byte[] buf = new byte[1024];
     int read;
     while((read=fis.read(buf))!=-1)  //reading data
         cout.write(buf,0,read);  //writing encrypted data
     //closing streams
     fis.close();
     cout.flush();
     cout.close();
 }

 public static void main (String[] args)throws Exception {
     new FileEncryptor("DES/ECB/PKCS5Padding","C:\\Users\\*******\\Desktop\\newtext").encrypt();//encrypts the current file.
  }
}

Here is the section of my file creator that is failing to call this:

FileWriter fWriter = null;
BufferedWriter writer = null;
try{
fWriter = new FileWriter("C:\\Users\\*******\\Desktop\\newtext");
writer = new BufferedWriter(fWriter);
writer.write(Data);
writer.close();
f.dispose();
FileEncryptor encr = new FileEncryptor(); //problem lies here.
encr.encrypt //public void that does the encryption.
new complete(); //different .java that is working fine.

© Stack Overflow or respective owner

Related posts about java