我创建了一个基本程序,它将输入的内容输入两个文本字段并将它们导出到文件中。我现在想加密那个文件,并且已经有了加密器。问题是我不能调用它。这是我的加密器代码:
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.
}
}
这是我的文件创建者中未能调用它的部分:
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.
好吧,我想我已经猜到了。感谢所有做出贡献的人。