У меня вопрос о защите паролем файла Excel.Как защитить пароль зашифрованный файл Excel на Java?
Ситуация в том, что у меня есть zip-файл, в котором есть файл Excel. Мне нужно написать программу Java, чтобы защитить файл Excel. Следовательно, пользователь должен иметь возможность разархивировать файл (zip-файл не должен защищаться паролем). Но Excel должен быть защищен паролем. Когда пользователь пытается распаковать файл, он должен это сделать. И когда он пытается открыть файл Excel (который находится в распакованной папке), он должен запросить пароль. Вопрос аналогичен Protect excel file with java, с дополнительной сложностью, что файл Excel застегнут.
У меня есть код, который защищает только почтовый файл, но это не то, что я хочу.
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();
}
}
Возможно, есть способ добавить защиту паролем в сжатый файл без его распаковки , но единственный способ, которым я знаю, - это распаковать его, а затем повторно сжать его с помощью защиты паролем. Вам, возможно, придется вникнуть в спецификацию zip, чтобы узнать, как обрабатывается защита паролем, если ему необходимо следовать стандарту zip. –
Зачем вы закрепили файл .xlsx? .xlsx уже является ziped-файлом, вы не получите много! – AxFab
Я не знаю вашего прецедента, но просто считаю, что пароль, защищающий почтовый индекс, безопасен, так как он не просто добавит к нему «пароль», а зашифрует все это. Также подумайте, что некоторые версии excel действительно небезопасны: http://www.excelforum.com/excel-programming/503874-how-safe-is-the-excel-password-functionality.html. Если для вас проблема безопасности, вам лучше остаться с зашифрованной почтой. –