EPPlus supports both setting protection and reading and writing encrypted packages.

## Protection

Protection can be set both on the workbook and the worksheet level.  
This example locks the window and the structure and sets a password.

```csharp
package.workbook.Protection.LockWindows = true;
package.workbook.Protection.LockStructure = true;

//Set a password for the workbook protection
package.workbook.Protection.SetPassword("EPPlus");
```

```vbnet
package.Workbook.Protection.LockWindows = True
package.Workbook.Protection.LockStructure = True

'Set a password for the workbook protection
package.Workbook.Protection.SetPassword("EPPlus")
```

Note that these settings don't encrypt the workbook. To do that you can set the `IsEncrypted` property:

```csharp
    //Encrypts the package with the default password. The package will be encrypted but can be opened by Excel without a password.
    //The file can be opened by EPPlus by providing an empty string as password ("")
    package.Encryption.IsEncrypted = true;
```

```vbnet
'Encrypts the package with the default password. The package will be encrypted but can be opened by Excel without a password.
'The file can be opened by EPPlus by providing an empty string as password ("")
package.Encryption.IsEncrypted = True
```

Worksheets can also be protected by using the worksheets `Protection` property.  
Here is an example of how you can lock cells in a worksheet:

```csharp
sheet.Protection.AllowSelectLockedCells = false;
sheet.Protection.SetPassword("EPPlus");

sheet.Cells["A:XFD"].Style.Locked = true;          //Set Locked to true on all cells (this is the default state, so this is not needed on a newly add worksheet).
sheet.Cells["B3,C7,C9,C11"].Style.Locked = false;  //Unlock these cells
```

```vbnet
sheet.Protection.AllowSelectLockedCells = False
sheet.Protection.SetPassword("EPPlus")

sheet.Cells("A:XFD").Style.Locked = True          'Set Locked to true on all cells (this is the default state, so this is not needed on a newly add worksheet).
sheet.Cells("B3,C7,C9,C11").Style.Locked = False  'Unlock these cells
```

## Encryption

To encrypt a workbook simply save the workbook with your password using the `Save` or `SaveAs` methods.

Using the `SaveAs` method:

```csharp
// Using the SaveAs method
using (ExcelPackage package = new ExcelPackage())
{
   var ws = package.Workbook.Worksheets.Add("MyWorksheet");
   ws.Cells["A1"].Value = "Secret Value";

   package.SaveAs(@"c:\MyEncryptedFile.xlsx", "MyP@ssw0rd!");
}
```

```vbnet
' Using the SaveAs method
Using package As New ExcelPackage()
    Dim ws = package.Workbook.Worksheets.Add("MyWorksheet")
    ws.Cells("A1").Value = "Secret Value"

    package.SaveAs("c:\MyEncryptedFile.xlsx", "MyP@ssw0rd!")
End Using
```

Using the `Save` method:

```csharp
// Using the Save method
using (ExcelPackage package = new ExcelPackage(@"c:\MyEncryptedFile.xlsx"))
{
   var ws = package.Workbook.Worksheets.Add("MyWorksheet");
   ws.Cells["A1"].Value = "Secret Value";
   package.Save("MyP@ssw0rd!");
}
```

```vbnet
' Using the Save method
Using package As New ExcelPackage("c:\MyEncryptedFile.xlsx")
    Dim ws = package.Workbook.Worksheets.Add("MyWorksheet")
    ws.Cells("A1").Value = "Secret Value"
    package.Save("MyP@ssw0rd!")
End Using
```

To read an encrypted package you should supply the password in the constructor:

```csharp
using (ExcelPackage package = new ExcelPackage(@"c:\MyEncryptedFile.xlsx", "MyP@ssw0rd!"))
{
    Console.WriteLine(package.Workbook.Worksheets["MyWorksheet"].Cells["A1"]);
}
```

```vbnet
Using package As New ExcelPackage("c:\MyEncryptedFile.xlsx", "MyP@ssw0rd!")
    Console.WriteLine(package.Workbook.Worksheets("MyWorksheet").Cells("A1"))
End Using
```

## See also

Encryption and protection of your workbook is shown in [Sample 8.1-C\#](<https://github.com/EPPlusSoftware/EPPlus.Samples.CSharp/blob/master/08-Encryption protection and VBA/01-Encryption and protection/EncryptionAndProtectionSample.cs>) or [Sample 8.1-VB](<https://github.com/EPPlusSoftware/EPPlus.Samples.VB/blob/master/08-Encryption protection and VBA/01-Encryption and protection/EncryptionAndProtectionSample.vb>)
