Password-Protect Documents
- 5 minutes to read
Encrypt a workbook to require a password to open the workbook file. Spreadsheet applications (for example, Microsoft Excel) and the Spreadsheet Document API cannot open an encrypted file without the password.
Encryption and workbook protection are independent. You can apply both to the same workbook.
Use the following methods to require a password for a workbook:
| Protection Type | Users Need the Password To | Method |
|---|---|---|
| Encryption | Open the file. | Workbook.Encrypt(options) |
| Workbook protection | Modify the workbook and unlock workbook structure. | Workbook.Protect(options) |
Encrypt a Workbook
Call the Workbook.Encrypt method to encrypt a workbook. Pass an EncryptionOptions object to the Encrypt method to specify the password and the encryption type (EncryptionType):
| Encryption Type | Description |
|---|---|
| Strong | Agile encryption for Open XML files that uses an XML EncryptionInfo structure. The default encryption algorithm is AES-256, and the hashing algorithm is SHA-512. Binary files, such as XLS files, use RC4 CryptoAPI encryption. |
| Compatible | Standard encryption for Open XML files that uses a binary EncryptionInfo structure. The default encryption algorithm is AES-128, and the hashing algorithm is SHA-1. |
Important
The Compatible encryption type uses outdated algorithms for compatibility purposes (for earlier versions of Microsoft Office). To protect sensitive data, use the Strong encryption type instead.
The XLS format supports only RC4 encryption, regardless of the encryption type. To protect sensitive data, save the workbook in XLSX format and use the Strong encryption type.
The password must contain from 1 to 255 characters.
The API encrypts the document when you save the workbook.
XLSX, XLSM, XLSB, and XLS files support encryption. If you save the workbook to a text format (for example, CSV), the API does not encrypt the output.
The following code snippet saves an encrypted workbook, passes the password to the Workbook constructor to open the file, and displays the decrypted cell A1 value in the console:
using System;
using System.IO;
using DevExpress.Docs.Office;
using DevExpress.Docs.Spreadsheet;
Directory.CreateDirectory("output");
string filePath = Path.Combine("output", "Document.xlsx");
// Encrypt the workbook and save it to a file.
using (Workbook workbook = new Workbook()) {
workbook.Worksheets[0].Cells["A1"].Value = "Confidential";
workbook.Encrypt(new EncryptionOptions("password"));
using FileStream outputStream = File.Create(filePath);
workbook.Save(outputStream);
}
// Open the encrypted file.
LoadOptions loadOptions = new LoadOptions() { Password = "password" };
using FileStream inputStream = File.OpenRead(filePath);
using Workbook encryptedWorkbook = new Workbook(inputStream, loadOptions);
Console.WriteLine(encryptedWorkbook.Worksheets[0].Cells["A1"].Value.TextValue);
Open an Encrypted Workbook
Set the LoadOptions.Password property to the file password and pass the LoadOptions object to the Workbook(stream, options) or Workbook(buffer, options) constructor.
The API cannot detect the format of an encrypted XLSX, XLSM, XLSB, XLTX, or XLTM document from document content. To load such a document from a byte array or from a stream other than a FileStream, set the LoadOptions.DocumentFormat property. Otherwise, Workbook(stream, options) and Workbook(buffer, options) constructors cannot load the document, even if the password is correct.
If the password is missing or incorrect, Workbook(stream, options) and Workbook(buffer, options) constructors throw an Exception with the “Cannot load the document” message. The InnerException property contains an InvalidOperationException that describes the reason (for example, “The specified password is incorrect”). If the API cannot detect the document format, the inner exception message is “Cannot determine document format.”
The API resets the LoadOptions.Password property after each load operation, even if the operation fails. To load another encrypted workbook with the same LoadOptions object, set the Password property again.
using System;
using System.Collections.Generic;
using System.IO;
using DevExpress.Docs.Spreadsheet;
// Specify encrypted files and passwords.
Dictionary<string, string> files = new Dictionary<string, string>() {
{ "Document1.xlsx", "password1" },
{ "Document2.xlsx", "password2" }
};
LoadOptions loadOptions = new LoadOptions();
foreach (KeyValuePair<string, string> entry in files) {
// Set the password before each load operation.
loadOptions.Password = entry.Value;
using FileStream inputStream = File.OpenRead(entry.Key);
using Workbook workbook = new Workbook(inputStream, loadOptions);
Console.WriteLine($"{entry.Key}: {workbook.Worksheets[0].Cells["A1"].Value.TextValue}");
}
Modify or Remove Encryption
If you load an encrypted workbook and save it again, the API encrypts the saved file with the same password and encryption type. To modify the password or the encryption type, call the Workbook.Encrypt method. The new settings replace the current settings.
Call the Workbook.RemoveEncryption() method to save the workbook without encryption. Workbook and sheet protection settings remain.
The following code snippet opens an encrypted file, removes encryption, and saves the workbook:
using System.IO;
using DevExpress.Docs.Spreadsheet;
Directory.CreateDirectory("output");
// Open the encrypted file.
LoadOptions loadOptions = new LoadOptions() { Password = "password" };
using FileStream inputStream = File.OpenRead("Document.xlsx");
using Workbook workbook = new Workbook(inputStream, loadOptions);
// Remove encryption and save the workbook.
workbook.RemoveEncryption();
using FileStream outputStream = File.Create(Path.Combine("output", "Document.xlsx"));
workbook.Save(outputStream);