我正在编写一个小工具来加密和解密文件。加密文件将被添加到 zip 存档中。到目前为止,以下代码正在运行。我可以加密除文本文件以外的任何文件。如果我选择一个文本文件,它写得不完整。如果有以下文字:“一二三测试检查检查测试”它会像“一二三测试车”一样被剪切。
没有抛出异常,只是丢失了几个字节。任何其他文件(pdf、mp3、mp4、exe 等)都是完整编写的。
package encryptme.crypto;
import encryptme.fileoperations.FileIn;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
public class Encrypt {
private String algorithm;
private int keylength;
private Key key;
private KeyPair keypair;
private File[] source;
private String destination;
public Encrypt(String a, int kl, Key k, File[] s, String dest) {
this.algorithm = a;
this.keylength = kl;
this.key = k;
this.source = s;
this.destination = dest;
this.output = o;
}
public void encOut() {
CipherOutputStream cout = null;
ZipOutputStream zout = null;
OutputStream out = null;
Cipher cipher;
try {
out = new FileOutputStream(this.destination);
System.out.println(this.destination);
zout = new ZipOutputStream(out);
cipher = Cipher.getInstance(this.algorithm);
cipher.init(Cipher.ENCRYPT_MODE, this.key);
for (int i = 0; i < this.source.length; i++) {
FileInputStream in = new FileInputStream(this.source[i].getAbsolutePath());
ZipEntry ze = new ZipEntry(this.source[i].getName());
zout.putNextEntry(ze);
cout = new CipherOutputStream(zout, cipher);
byte[] buffer = new byte[1024];
long bytesRead = 0;
for (bytesRead = 0; (bytesRead = in.read(buffer)) != -1;) {
cout.write(buffer, 0, (int) bytesRead);
}
zout.closeEntry();
in.close();
}
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(Encrypt.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchPaddingException ex) {
Logger.getLogger(Encrypt.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidKeyException ex) {
Logger.getLogger(Encrypt.class.getName()).log(Level.SEVERE, null, ex);
} catch (FileNotFoundException ex) {
Logger.getLogger(Encrypt.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Encrypt.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
if (cout != null) {
cout.close();
}
if (zout != null) {
zout.close();
}
if (out != null) {
out.close();
}
} catch (IOException ex) {
Logger.getLogger(Encrypt.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
使用 jFileChooser 选择要加密的文件并添加到一个数组中,该数组作为参数传递给上面的类。我考虑过使用 PrintWriter 来处理那些通过结尾来识别它们的文本文件,例如“textfile.txt”或“textfile.odt”。但是如果文件没有像'textfile'这样的结尾,我该怎么办?
我对Java和一般编码都很陌生,所以我希望只是一个导致问题的严重错误。