Verschlüsselung von Excel Dateien

Verwendung von Microsoft Excel

Um die Dateiverschlüsselungseinstellungen in Microsoft Excel festzulegen (hier Microsoft Excel 2003):

  1. Wählen Sie im Menü Extras die Option Optionen aus. Es wird ein Dialogfeld angezeigt.
  2. Wählen Sie den Tab Sicherheit aus.
  3. Geben Sie ein Passwort ein und klicken Sie auf Erweitert
  4. Wählen Sie den Verschlüsselungstyp aus und bestätigen Sie das Passwort.

Verschlüsselung mit Aspose.Cells

Das folgende Beispiel zeigt, wie Sie mit dem Aspose.Cells-API eine Excel-Datei verschlüsseln und kennwortgeschützt machen können.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiate a Workbook object.
// Open an excel file.
Workbook workbook = new Workbook(dataDir + "Book1.xls");
// Specify XOR encryption type.
workbook.SetEncryptionOptions(EncryptionType.XOR, 40);
// Specify Strong Encryption type (RC4,Microsoft Strong Cryptographic Provider).
workbook.SetEncryptionOptions(EncryptionType.StrongCryptographicProvider, 128);
// Password protect the file.
workbook.Settings.Password = "1234";
// Save the excel file.
workbook.Save(dataDir + "encryptedBook1.out.xls");

Passwort zum Ändern der Option festlegen

Das folgende Beispiel zeigt, wie Sie die Passwort zum Ändern Microsoft Excel-Option für eine vorhandene Datei mithilfe der Aspose.Cells-API festlegen.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiate a Workbook object.
// Open an excel file.
Workbook workbook = new Workbook(dataDir + "Book1.xls");
// Set the password for modification.
workbook.Settings.WriteProtection.Password = "1234";
// Save the excel file.
workbook.Save(dataDir + "SpecifyPasswordToModifyOption.out.xls");

Das Passwort der verschlüsselten Datei verifizieren

Um das Passwort der verschlüsselten Datei zu verifizieren, bietet Aspose.Cells for .NET die Methode VerifyPassword. Diese Methoden akzeptieren zwei Parameter: den Dateistream und das zu verifizierende Passwort. Der folgende Code-Schnipsel zeigt die Verwendung der Methode VerifyPassword zur Überprüfung, ob das angegebene Passwort gültig ist oder nicht.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create a Stream object
FileStream fstream = new FileStream(dataDir + "EncryptedBook1.xlsx", FileMode.Open);
bool isPasswordValid = FileFormatUtil.VerifyPassword(fstream, "1234");
Console.WriteLine("Password is Valid: " + isPasswordValid);

Verschlüsselung/Entschlüsselung von ODS-Dateien mit Aspose.Cells

Aspose.Cells ermöglicht die Verschlüsselung und Entschlüsselung von ODS-Dateien. Die entschlüsselte ODS-Datei kann sowohl in Excel als auch in OpenOffice geöffnet werden. Die verschlüsselte ODS-Datei kann jedoch nur von OpenOffice geöffnet werden, nachdem das Passwort eingegeben wurde. Excel kann die verschlüsselte ODS-Datei nicht öffnen und möglicherweise eine Warnmeldung anzeigen. Die Verschlüsselungsoptionen sind im Gegensatz zu anderen Dateitypen nicht für ODS-Dateien anwendbar. Um eine ODS-Datei zu verschlüsseln, laden Sie die Datei und setzen Sie den WorkbookSettings.Password-Wert auf das tatsächliche Passwort, bevor Sie sie speichern. Die Ausgabeverschlüsselte ODS-Datei kann nur in OpenOffice geöffnet werden.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string sourceDir = RunExamples.Get_SourceDirectory();
//Output directory
string outputDir = RunExamples.Get_OutputDirectory();
// Open an ODS file
Workbook workbook = new Workbook(sourceDir + "sampleODSFile.ods");
// Password protect the file
workbook.Settings.Password = "1234";
// Save the ODS file
workbook.Save(outputDir + "outputEncryptedODSFile.ods");

Für das Entschlüsseln einer ODS-Datei laden Sie die Datei, indem Sie ein Passwort in LoadOptions.Password angeben. Sobald die Datei geladen ist, setzen Sie den Wert WorkbookSettings.Password auf null.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string sourceDir = RunExamples.Get_SourceDirectory();
//Output directory
string outputDir = RunExamples.Get_OutputDirectory();
// Open an encrypted ODS file
Aspose.Cells.LoadOptions loadOptions = new Aspose.Cells.LoadOptions(Aspose.Cells.LoadFormat.Ods);
// Set original password
loadOptions.Password = "1234";
// Load the encrypted ODS file with the appropriate load options
Workbook workbook = new Workbook(sourceDir + "sampleEncryptedODSFile.ods", loadOptions);
// Set the password to null
workbook.Settings.Password = null;
// Save the decrypted ODS file
workbook.Save(outputDir + "outputDecryptedODSFile.ods");