Kryptera och dekryptera Excel filer med C++
Microsoft Excel (97 - 365) gör det möjligt för dig att kryptera och lösenordsskydda dina kalkylblad. Det använder algoritmer som tillhandahålls av en kryptografisk tjänsteleverantör, eller CSP, en uppsättning krypteringsalgoritmer med olika egenskaper. Standard-CSP är ‘Kontors 97/2000-kompatibel’ eller ‘Svag kryptering (XOR)’. Det är viktigt att välja rätt krypteringsnyckellängd. Vissa CSP:er stöder inte mer än 40 eller 56 bitar. Det anses vara svag kryptering. För stark kryptering krävs en minsta nyckellängd på 128 bitar. Microsoft Windows innehåller också CSP:er som erbjuder starka krypteringstyper, till exempel ‘Microsoft Strong Cryptographic Provider’. För att ge dig en uppfattning, 128 bitar kryptering är vad banker använder för att kryptera anslutningen med sina internetbankssystem.
Aspose.Cells gör det möjligt för dig att kryptera och lösenordsskydda Microsoft Excel-filer med önskad krypteringstyp.
Använda Microsoft Excel
För att ställa in filkrypteringsinställningar i Microsoft Excel (här Microsoft Excel 2003):
- Från menyn Verktyg, välj Alternativ. En dialogruta kommer att visas.
- Välj fliken Säkerhet.
- Ange ett lösenord och klicka på Avancerat
- Välj krypteringstyp och bekräfta lösenordet.
Kryptera Excel-fil med Aspose.Cells
Följande exempel visar hur man krypterar och lösenordsskyddar en Excel-fil med Aspose.Cells API.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// 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";
// Create workbook
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 excel file
workbook.Save(outputFilePath);
std::cout << "Workbook encrypted successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
Ange lösenord för att ändra alternativ
Följande exempel visar hur man ställer in alternativet Lösenord för att ändra i Microsoft Excel för en befintlig fil med hjälp av Aspose.Cells API.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// 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();
}
Avkryptering av Excelfil med Aspose.Cells
Det är mycket enkelt att öppna en lösenordsskyddad Excel-fil och dekryptera den med Aspose.Cells API som visas i följande kod:
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Create load options and set password
LoadOptions loadOptions;
loadOptions.SetPassword(u"password");
// Open encrypted Excel file
Workbook workbook(u"Book1.xlsx", loadOptions);
// Remove password protection
workbook.GetSettings().SetPassword(nullptr);
// Save the modified workbook
workbook.Save(u"Book1.xlsx");
std::cout << "Password removed and file saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
}