Encrypt Excel file

Password Protect Excel(XLS, XLSX) using C# .NET

Excel is a widely used tool for organizing and analyzing data. However, sometimes the data contained within an Excel file can be sensitive or confidential, and it is crucial to protect it from unauthorized access. By password protecting and encrypting Excel files, you can secure your data and prevent it from falling into the wrong hands. This feature is particularly important for businesses and organizations that deal with confidential information. In this article, we will show you how to password protect and encrypt Excel files using C# .NET, so that you can keep your data secure and protect your privacy.

API to Password Protect Excel Files

Aspose.Cells Cloud SDK for .NET is an amazing tool that allows developers to easily add password protection to their Excel files. With this feature, you can protect sensitive data and prevent unauthorized access to your files. Password protection is essential for businesses and individual users, who are quite concerned about the privacy of their information. This API makes it easy to add password protection to your Excel files, without the need for complex coding or additional software.

Now search Aspose.Cells-Cloud in “NuGet packages manager” and click the “Add Package” button. Secondly, if you do not have an account over Cloud Dashboard, please create a free account by using a valid email address and obtain your personalized credentials.

Encrypt Excel File using C#

Please try using the following code snippet to add password to Excel Workbook.

We have used XOR as an encryption type and one of the following values can be assigned to EncryptionType property

  • XOR
  • Compatible
  • EnhancedCryptographicProviderV1
  • StrongCryptographicProvider
password protect excel file

Encrypt Excel file preview.

Now let’s develop our understanding about the code snippet:

CellsApi cellsInstance = new CellsApi(clientID, clientSecret);

Create an object of CellsApi while passing client credentials as arguments.

var file = System.IO.File.OpenRead(input_ODS);

Read the content of input Excel from local system drive.

cellsInstance.UploadFile(input_Excel, File.OpenRead(input_Excel));

Upload the input Excel to cloud storage.

WorkbookEncryptionRequest encryption = new WorkbookEncryptionRequest();
encryption.Password = "123456";
encryption.KeyLength = 128;
encryption.EncryptionType = "XOR";

Create Workbook encryption request where we define XOR as encryption type.

var response = cellsInstance.CellsWorkbookPostEncryptDocument(input_Excel, encryption, null);

Finally, call this method to password protect an Excel file. The encrypted file will be saved in cloud storage.

The sample Excel file used in the above example can be downloaded from myDocument.xlsx.

Add Password to Excel using cURL Commands

cURL is a command-line tool that allows you to transfer data to and from a server using various protocols, including HTTP, HTTPS, FTP, and more. The cURL commands can also be useful for scripting and automation purposes. This can be especially useful if you have a large number of Excel files that need to be encrypted, or if you need to perform this operation regularly. Therefore, instead of manually performing these operations using a web browser or a graphical user interface, you can use cURL commands to interact with the Aspose.Cells Cloud API directly from the command line.

To get started, you will need to have cURL installed on your system and then generate an accessToken based on client credentials:

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=bb959721-5780-4be6-be35-ff5c3a6aa4a2&client_secret=4d84d5f6584160cbd91dba1fe145db14" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Use the following command to upload input Excel to cloud storage:

curl -v "https://api.aspose.cloud/v3.0/cells/storage/file/{filePath}" \
-X PUT \
-F file=@{localFilePath} \
-H "Content-Type: multipart/form-data" \
-H "Authorization: Bearer {accessToken}"

Replace {filePath} with the path where you want to store the file in the cloud storage, {localFilePath} with the path of the Excel file on your local system, and {accessToken} with your Aspose Cloud access token.

Finally, execute the following command to password protect an Excel file:

curl -v "https://api.aspose.cloud/v3.0/cells/{excelName}]/encryption" \
-X POST \
-H  "accept: application/json" \
-H  "authorization: Bearer {accessToken}" \
-H  "Content-Type: application/json" \
-d "{  \"EncryptionType\": \"XOR\",  \"KeyLength\": 128,  \"Password\": \"123456\"}"

Replace {excelName} with the name of the Excel file you uploaded to the cloud storage, and {accessToken} with the access token generated above. After the execution, the resultant Excel will be stored in same cloud storage.

Concluding Remarks

We have learned that the Aspose.Cells Cloud SDK for .NET provides a simple and secure way to password protect and encrypt Excel files using cURL commands and API methods. Overall, the Aspose.Cells Cloud SDK for .NET is an excellent tool for developers who need to implement security measures for Excel files. By using this API, developers can ensure that sensitive and confidential information stored in Excel files is protected from unauthorized access, and comply with regulatory requirements. Lastly, the API has proven to be a useful tool in fulfilling the need for password protection and encryption of Excel files.

Please visit the following links to learn more about: