Tôi có câu hỏi về mật khẩu bảo vệ tệp Excel.Làm thế nào để bảo vệ mật khẩu một tệp Excel đã nén trong Java?
Tình huống là, tôi có tệp zip, có tệp Excel trong đó. Tôi cần viết một chương trình Java, để bảo vệ mật khẩu tệp Excel. Do đó, người dùng có thể giải nén tệp (tệp zip không cần phải được bảo vệ bằng mật khẩu). Tuy nhiên, Excel cần phải được bảo vệ bằng mật khẩu. Khi người dùng cố gắng giải nén tệp, anh ấy có thể làm như vậy. Và khi anh ta cố mở tệp Excel (nằm trong thư mục đã giải nén), nó phải yêu cầu mật khẩu. Câu hỏi tương tự như Protect excel file with java, với độ phức tạp thêm vào đó, tệp Excel được nén.
Tôi có mã, mật khẩu đó chỉ bảo vệ tệp zip, nhưng đây không phải là những gì tôi muốn.
import java.io.File;
import java.util.ArrayList;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
/**
* Demonstrates adding files to zip file with standard Zip Encryption
*/
public class AddFilesWithStandardZipEncryption
{
public AddFilesWithStandardZipEncryption()
{
try {
// Initiate ZipFile object with the path/name of the zip file.
//ZipFile zipFile = new ZipFile("c:\\ZipTest\\AddFilesWithStandardZipEncryption.zip");
ZipFile zipFile = new ZipFile("C:\\homepage\\workspace\\PasswordProtectedFiles\\new.zip");
// Build the list of files to be added in the array list
// Objects of type File have to be added to the ArrayList
ArrayList filesToAdd = new ArrayList();
//filesToAdd.add(new File("C:\\homepage\\workspace\\passwordprotectedzipfile\\profile\\profile.txt"));
filesToAdd.add(new File("C:\\homepage\\workspace\\PasswordProtectedFiles\\new.xlsx"));
//filesToAdd.add(new File("c:\\ZipTest\\myvideo.avi"));
//filesToAdd.add(new File("c:\\ZipTest\\mysong.mp3"));
// Initiate Zip Parameters which define various properties such
// as compression method, etc.
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // set compression method to store compression
// Set the compression level
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
// Set the encryption flag to true
// If this is set to false, then the rest of encryption properties are ignored
parameters.setEncryptFiles(true);
// Set the encryption method to Standard Zip Encryption
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
// Set password
parameters.setPassword("test123!");
// Now add files to the zip file
// Note: To add a single file, the method addFile can be used
// Note: If the zip file already exists and if this zip file is a split file
// then this method throws an exception as Zip Format Specification does not
// allow updating split zip files
zipFile.addFiles(filesToAdd, parameters);
}
catch (ZipException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new AddFilesWithStandardZipEncryption();
}
}
Có thể có cách thêm bảo vệ mật khẩu vào tệp nén mà không giải nén , nhưng cách duy nhất tôi biết để xử lý nó là giải nén nó, sau đó recompress nó với mật khẩu bảo vệ. Bạn có thể phải thâm nhập vào thông số zip để tìm hiểu cách bảo vệ mật khẩu được xử lý nếu nó cần tuân thủ tiêu chuẩn zip. –
Tại sao bạn nén tệp .xlsx? .xlsx đã là một tệp nén, bạn sẽ không đạt được nhiều thứ! – AxFab
Tôi không biết trường hợp sử dụng của bạn, nhưng chỉ xem xét rằng mật khẩu bảo vệ một zip là an toàn, vì nó sẽ không chỉ thêm một "mật khẩu" vào nó, nhưng mã hóa toàn bộ điều. Cũng nên xem xét rằng một số phiên bản của excel thực sự không an toàn: http://www.excelforum.com/excel-programming/503874-how-safe-is-the-excel-password-functionality.html. Nếu bảo mật là một vấn đề cho bạn, bạn nên ở lại với mã hóa zip. –