Excel Dateien mit C++ verschlüsseln
Microsoft Excel (97 - 365) ermöglicht es Ihnen, Ihre Tabellenkalkulationen zu verschlüsseln und mit einem Passwort zu schützen. Es verwendet Algorithmen, die von einem kryptografischen Dienstanbieter oder CSP bereitgestellt werden, ein Satz kryptografischer Algorithmen mit unterschiedlichen Eigenschaften. Der Standard-CSP ist ‘Office 97/2000 Kompatibel’ oder ‘Schwache Verschlüsselung (XOR)’. Es ist wichtig, die richtige Schlüssellänge zu wählen. Einige CSPs unterstützen nicht mehr als 40 oder 56 Bits. Das gilt als schwache Verschlüsselung. Für starke Verschlüsselung ist eine Mindestschlüssellänge von 128 Bits erforderlich. Microsoft Windows enthält ebenfalls CSPs, die starke Verschlüsselungstypen anbieten, wie z. B. den ‘Microsoft Strong Cryptographic Provider’. Um Ihnen eine Vorstellung zu geben, 128-Bit-Verschlüsselung wird von Banken verwendet, um die Verbindung mit ihren Internetbanking-Systemen zu verschlüsseln.
Mit Aspose.Cells können Sie Microsoft Excel-Dateien mit dem gewünschten Verschlüsselungstyp verschlüsseln und mit einem Passwort schützen.
Verwendung von Microsoft Excel
Um die Dateiverschlüsselungseinstellungen in Microsoft Excel festzulegen (hier Microsoft Excel 2003):
- Wählen Sie im Menü Extras die Option Optionen aus. Es wird ein Dialogfeld angezeigt.
- Wählen Sie den Tab Sicherheit aus.
- Geben Sie ein Passwort ein und klicken Sie auf Erweitert
- Wählen Sie den Verschlüsselungstyp aus und bestätigen Sie das Passwort.
Verschlüsselung mit Aspose.Cells
Das folgende Beispiel zeigt, wie man eine Excel-Datei mit Aspose.Cells API verschlüsselt und passwortgeschützt macht.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input excel file
U16String inputFilePath = srcDir + u"Book1.xls";
// Path of output excel file
U16String outputFilePath = outDir + u"encryptedBook1.out.xls";
// Instantiate a Workbook object and open the excel file
Workbook workbook(inputFilePath);
// 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.GetSettings().SetPassword(u"1234");
// Save the encrypted excel file
workbook.Save(outputFilePath);
std::cout << "File encrypted and saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
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.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input excel file
U16String inputFilePath = srcDir + u"Book1.xls";
// Path of output excel file
U16String outputFilePath = outDir + u"SpecifyPasswordToModifyOption.out.xls";
// Create workbook
Workbook workbook(inputFilePath);
// Set the password for modification
workbook.GetSettings().GetWriteProtection().SetPassword(u"1234");
// Save the excel file
workbook.Save(outputFilePath);
std::cout << "Password for modification set successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
Das Passwort der verschlüsselten Datei verifizieren
Um das Passwort der verschlüsselten Datei zu überprüfen, bietet Aspose.Cells for C++ die VerifyPassword Methode an. Diese Methode akzeptiert zwei Parameter, den Dateistream und das zu überprüfende Passwort. Der folgende Code-Schnipsel zeigt die Verwendung der Methode VerifyPassword zur Überprüfung, ob das angegebene Passwort gültig ist oder nicht.
#include <iostream>
#include <fstream>
#include <vector>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
U16String inputPath = srcDir + u"EncryptedBook1.xlsx";
std::vector<uint8_t> fileData;
std::ifstream file(inputPath.ToUtf8(), std::ios::binary);
if (file)
{
file.seekg(0, std::ios::end);
fileData.resize(file.tellg());
file.seekg(0, std::ios::beg);
file.read(reinterpret_cast<char*>(fileData.data()), fileData.size());
}
Vector<uint8_t> data(fileData.data(), static_cast<int32_t>(fileData.size()));
bool isPasswordValid = FileFormatUtil::VerifyPassword(data, u"123456");
std::cout << "Password is Valid: " << std::boolalpha << isPasswordValid << std::endl;
Aspose::Cells::Cleanup();
}
Verschlüsselung/Entschlüsselung von ODS-Dateien mit Aspose.Cells
Aspose.Cells ermöglicht es, ODS-Dateien zu verschlüsseln und zu entschlüsseln. Entschlüsselte ODS-Dateien können in Excel und OpenOffice geöffnet werden, während verschlüsselte ODS-Dateien nur in OpenOffice nach Eingabe des Passworts geöffnet werden können. Excel kann die verschlüsselte ODS-Datei nicht öffnen und zeigt möglicherweise eine Warnmeldung. Die Verschlüsselungsoptionen gelten nicht für ODS-Dateien, im Gegensatz zu anderen Dateitypen. Um eine ODS-Datei zu verschlüsseln, laden Sie die Datei, setzen Sie den WorkbookSettings.GetPassword() Wert auf das tatsächliche Passwort, bevor Sie sie speichern. Die verschlüsselte ODS-Ausgabedatei kann nur in OpenOffice geöffnet werden.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C++
// Source directory path
U16String sourceDir = u"..\\Data\\01_SourceDirectory\\";
// Output directory path
U16String outputDir = u"..\\Data\\02_OutputDirectory\\";
// Open an ODS file
Workbook workbook(sourceDir + u"sampleODSFile.ods");
// Password protect the file
workbook.GetSettings().SetPassword(u"1234");
// Save the ODS file
workbook.Save(outputDir + u"outputEncryptedODSFile.ods");
std::cout << "ODS file password protected and saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
return 0;
}
Für das Entschlüsseln einer ODS-Datei laden Sie die Datei, indem Sie ein Passwort in LoadOptions.GetPassword() angeben. Sobald die Datei geladen ist, setzen Sie den Wert WorkbookSettings.GetPassword() auf null.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Path to the source directory
U16String sourceDir = u"..\\Data\\01_SourceDirectory\\";
// Output directory
U16String outputDir = u"..\\Data\\02_OutputDirectory\\";
// Open an encrypted ODS file
LoadOptions loadOptions(LoadFormat::Ods);
// Set original password
loadOptions.SetPassword(u"1234");
// Load the encrypted ODS file with the appropriate load options
Workbook workbook(sourceDir + u"sampleEncryptedODSFile.ods", loadOptions);
// Set the password to null
workbook.GetSettings().SetPassword(nullptr);
// Save the decrypted ODS file
workbook.Save(outputDir + u"outputDecryptedODSFile.ods");
std::cout << "Decrypted ODS file saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
}