Simple Steps to Encrypt and Decrypt PDF Files Using C#
Tags: dotnet, csharp, security, pdf
Published: 2020-02-13 13:13:47

---
title: Simple Steps to Encrypt and Decrypt PDF Files Using C#
published: true
date: 2020-02-12 12:41:55 UTC
tags: dotnet,csharp,security,pdf
canonical_url: https://www.syncfusion.com/blogs/post/simple-steps-to-encrypt-and-decrypt-pdf-files-using-c.aspx
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/1ay74126cr57lvcoekn5.jpg
---

The [Syncfusion PDF Library](https://www.syncfusion.com/pdf-framework/net/pdf-library) is a .NET PDF Library that allows users to encrypt and decrypt PDF documents in C# and VB.NET.

PDF encryption allows users to secure PDF documents with passwords. Two types of passwords are available:

- Document-open password: A document-open password, also known as a user password, is used to open a PDF document.
- Permission password: A permission password, also known as master or owner password, is used to restrict options such as printing, editing, and copying content in a PDF.

With the Syncfusion PDF Library, you can protect your PDF documents with either of these passwords, or both of them.

If the PDF is encrypted with both types of passwords, you can use either password to open it. But to change the restricted features, you need the permission password.

The complete details to encrypt and decrypt PDF files in C# are explained in the following topics in this post:

- [Encrypt PDF document](#encrypt-pdf-document)
- [Restrict PDF permissions](#restrict-pdf-permissions)
- [PDF encryption options](#pdf-encryption-options)
- [Decrypt PDF document/remove security from PDF](#decrypt-pdf-document)

## Encrypt PDF document

The Syncfusion PDF Library provides support for basic to advanced encryption standards. The following are the encryption standards supported:

- RC4 40-bit
- RC4 128-bit
- AES 128-bit
- AES 256-bit Revision 5
- AES 256-bit Revision 6

You can set the algorithm and key size using the properties **Algorithm** and **KeySize** available in the class **PdfSecurity**.

You can follow these three steps to encrypt an existing PDF document using Syncfusion PDF Library:

1. Load the existing PDF document.
2. Set a user password, encryption algorithm, and key size.
3. Save the PDF document.

The following code example shows how to encrypt a PDF document using a user password in C#.

```csharp
//Load existing PDF document.
PdfLoadedDocument document = new PdfLoadedDocument("PDF_Succinctly.pdf");
 
//Create a document security.
PdfSecurity security = document.Security;
 
//Set user password for the document.
security.UserPassword = "sample123";
//Set encryption algorithm.
security.Algorithm = PdfEncryptionAlgorithm.AES;
//Set key size.
security.KeySize = PdfEncryptionKeySize.Key256Bit;
 
//Save the PDF document.
document.Save("Output.pdf");
//Close the PDF document.
document.Close(true);
```

Execute this code example to get a PDF document like in the following screenshot. ![Encrypted PDF document](https://www.syncfusion.com/blogs/wp-content/uploads/2020/02/Encrypted-PDF-document.png)

## Restrict PDF permissions

You can customize the permission settings to allow or restrict certain actions such as:

- Copying content
- Printing
- Editing the document
- Filling form fields

The following table lists the permission flags supported by Syncfusion PDF Library.

<table>
<tbody>
<tr>
<td width="312"><strong>Permission flag</strong></td>
<td width="312"><strong>Description</strong></td>
</tr>
<tr>
<td width="312">FullQualityPrint</td>
<td width="312">Allows users to print a clear copy of the document.</td>
</tr>
<tr>
<td width="312">Print</td>
<td width="312">Allows users to print the document with low quality.</td>
</tr>
<tr>
<td width="312">FillFields</td>
<td width="312">Allows users to fill out and sign a form.</td>
</tr>
<tr>
<td width="312">CopyContent</td>
<td width="312">Allows users to copy content from the document.</td>
</tr>
<tr>
<td width="312">AccessibilityCopyContent</td>
<td width="312">Enables content screen reading for users with disabilities.</td>
</tr>
<tr>
<td width="312">EditAnnotations</td>
<td width="312">Allows users to get user input or feedback on a document.</td>
</tr>
<tr>
<td width="312">AssembleDocument</td>
<td width="312">Allows users to manipulate the pages in the document.</td>
</tr>
<tr>
<td width="312">EditContent</td>
<td width="312">Allows users to edit the content in the document.</td>
</tr>
</tbody>
</table>

To restrict PDF permissions, you must set the permission password (owner password) for the PDF document. You can set the owner password for the PDF document using the **OwnerPassword** property available in the class **PdfSecurity**.

You can set various permissions for the PDF document using the **Permissions** property available in the class **PdfSecurity.**

The following code example shows how to encrypt a PDF document using an owner password in C#.

```csharp
//Load existing PDF document.
PdfLoadedDocument document = new PdfLoadedDocument("PDF_Succinctly.pdf");
 
//Create document security.
PdfSecurity security = document.Security;
 
//Set owner password for the document.
security.OwnerPassword = "sample123";
//Set encryption algorithm.
security.Algorithm = PdfEncryptionAlgorithm.AES;
//Set key size.
security.KeySize = PdfEncryptionKeySize.Key256Bit;
 
//Set various permissions for the PDF document.
security.Permissions = PdfPermissionsFlags.Print |
PdfPermissionsFlags.FillFields |
PdfPermissionsFlags.CopyContent |
PdfPermissionsFlags.FullQualityPrint;
 
//Save the PDF document.
document.Save("Output.pdf");
//Close the PDF document.
document.Close(true);
```

Execute this code example to get a PDF document like in the following screenshot. ![Permission restricted PDF document](https://www.syncfusion.com/blogs/wp-content/uploads/2020/02/Permission-restricted-PDF-document.png)

## PDF encryption options

PDF encryption options are used to select the content needing to be encrypted in the resultant PDF document. It has following three options:

**Encrypt All Document Contents** : Encrypts both the document and its metadata. This makes the document undiscoverable by search engines, as its metadata is encrypted.

**Encrypt All Document Contents Except Metadata** : Encrypts only the contents of the document, leaving the metadata. This will allow search engines to read the metadata.

**Encrypt Only File Attachments** : Users can open the document without a password. But they need a password to open its attachments. Use this option to create security envelopes.

You can set these options in **PdfSecurity** using the property **EncryptionOptions.**

```csharp
//Load existing PDF document.
PdfLoadedDocument document = new PdfLoadedDocument("PDF_Succinctly.pdf");
 
//Create a document security.
PdfSecurity security = document.Security;
 
//Set owner password.
security.OwnerPassword = "sample123";
//Set user password.
security.UserPassword = "user";
 
//Set encryption algorithm.
security.Algorithm = PdfEncryptionAlgorithm.AES;
//Set key size
security.KeySize = PdfEncryptionKeySize.Key256Bit;
 
//Set various permissions for the PDF document.
security.Permissions = PdfPermissionsFlags.Print |
PdfPermissionsFlags.FillFields |
PdfPermissionsFlags.CopyContent |
PdfPermissionsFlags.FullQualityPrint;
 
//Encrypt all the content except metadata.
security.EncryptionOptions = PdfEncryptionOptions.EncryptAllContentsExceptMetadata;
 
//Save the PDF document.
document.Save("Output.pdf");
//Close the PDF document.
document.Close(true);
```

Execute this code example to get a PDF document like in the following screenshot. ![PDF encrypted with the metadata exposed to search engines](https://www.syncfusion.com/blogs/wp-content/uploads/2020/02/PDF-encrypted-with-the-metadata-exposed-to-search-engines.png)

## Decrypt a PDF document

Follow these steps to decrypt an encrypted PDF file:

1. Load the PDF document as PdfLoadedDocument using the **OwnerPassword** (since UserPassword cannot be used to alter the security permissions).
2. Set the security permission to default.
3. Set the owner password and user password to empty.
4. Save the resultant document.

The following code example shows how to decrypt a PDF document in C#.

```csharp
//Load existing PDF document.
PdfLoadedDocument document = new PdfLoadedDocument("PDF_Succinctly.pdf", "user");
 
//Create a document security.
PdfSecurity security = document.Security;
 
//Set permissions to default.
security.Permissions = PdfPermissionsFlags.Default;
//Set owner password.
security.OwnerPassword = String.Empty;
//Set user password.
security.UserPassword = String.Empty;
 
//Save the PDF document.
document.Save("Output.pdf");
//Close the PDF document.
document.Close(true);
```

By executing this code example, you will get a PDF document like in the following screenshot. ![Decrypted PDF document](https://www.syncfusion.com/blogs/wp-content/uploads/2020/02/Decrypted-PDF-document.png)

## Conclusion

In this blog post, we have walked through how to encrypt and decrypt a PDF document in C# using Syncfusion PDF Library.

So, now you can easily add password security to your PDF document with sensitive information and remove the password of an encrypted document if you want to open access to it to everyone.

Take a moment to peruse our [documentation](https://help.syncfusion.com/file-formats/pdf/working-with-security), where you’ll find other options and features, all with accompanying code examples.

If you have any questions about these features, please let us know in the comments below. You can also contact us through our [support forum](https://www.syncfusion.com/forums), [Direct-Trac](https://www.syncfusion.com/support/directtrac/), or [feedback portal](https://www.syncfusion.com/feedback/pdf). We are happy to assist you!

If you like this article, we think you would also like the following articles about our PDF Library:

- [How to Find Corrupted PDF Files in C# Easily](https://dev.to/syncfusion/how-to-find-corrupted-pdf-files-in-c-easily-16hi)
- [Create and Validate PDF Digital Signatures in C#](https://www.syncfusion.com/blogs/post/create-validate-pdf-digital-signatures-csharp.aspx)
- [How to Convert an Image to PDF in ASP.NET Core](https://www.syncfusion.com/blogs/post/how-to-convert-image-to-pdf-in-asp-net-core.aspx)
- [Easy Ways to Redact PDFs Using C#](https://www.syncfusion.com/blogs/post/easy-ways-to-redact-pdfs-using-c.aspx)
- [7 Ways to Compress PDF Files in C#, VB.NET](https://www.syncfusion.com/blogs/post/7-ways-to-compress-pdf-files.aspx)

The post [Simple Steps to Encrypt and Decrypt PDF Files Using C#](https://www.syncfusion.com/blogs/post/simple-steps-to-encrypt-and-decrypt-pdf-files-using-c.aspx) appeared first on [Syncfusion Blogs](https://www.syncfusion.com/blogs).