> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/uwp/get-started/samples/enctest.md).

# Encryption

Learn how to encrypt and decrypt PDF files in Apryse SDK.  It includes samples to encrypt and decrypt PDFs using C#

Sample C# code for using Apryse SDK to read encrypted (password protected) documents, secure a document with encryption, or remove encryption. Learn more about our [UWP SDK](/core/get-started/frameworks/dotnet.md).

{% tabs %}
{% tab title="C#" %}
{% code lineNumbers="true" %}

```csharp
//
// Copyright (c) 2001-2020 by PDFTron Systems Inc. All Rights Reserved.
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Windows.Foundation;

using pdftron.PDF;
using pdftron.SDF;

using PDFNetUniversalSamples.ViewModels;

namespace PDFNetSamples
{
    public sealed class EncTestCS : Sample
    {
        public EncTestCS() :
            base("Encryption Test", "Illustrates how to add password protection to a document and how to open it again.")
        {
        }

        public override IAsyncAction RunAsync()
        {
            return Task.Run(new System.Action(async () =>
            {
                WriteLine("--------------------------------");
                WriteLine("Starting Encryption Test...");
                WriteLine("--------------------------------\n");

                // Example 1: Securing a document with password protection and adjusting permissions 
                // on the document.
                try
                {
                    // Open the test file
                    WriteLine("-------------------------------------------------");
                    WriteLine("Securing an existing document...");

                    String input_file_path = Path.Combine(InputPath, "fish.pdf");
                    PDFDoc doc = new PDFDoc(input_file_path);

                    if (!doc.InitSecurityHandler())
                    {
                        WriteLine("Document authentication error...");
                        return;
                    }


                    SecurityHandler new_handler = new SecurityHandler();

                    string my_password = "test";
                    new_handler.ChangeUserPassword(my_password);

                    new_handler.SetPermission(SecurityHandlerPermission.e_print, true);
                    new_handler.SetPermission(SecurityHandlerPermission.e_extract_content, false);

                    doc.SetSecurityHandler(new_handler);

                    WriteLine(string.Format("\nTo open document use password: {0}\n", my_password));

                    String output_file_path = Path.Combine(OutputPath, "secured.pdf");
                    await doc.SaveAsync(output_file_path, 0);

                    WriteLine("Done. Result saved in " + output_file_path);
                    await AddFileToOutputList(output_file_path).ConfigureAwait(false);

                }
                catch (Exception e)
                {
                    WriteLine(GetExceptionMessage(e));
                }


                // Example 2: Reading password protected document without user feedback.
                try
                {
                    WriteLine("-------------------------------------------------");
                    WriteLine("Open the password protected document from the first example...");

                    String input_file_path = Path.Combine(OutputPath, "secured.pdf");
                    PDFDoc doc = new PDFDoc(input_file_path);   // Open the encrypted document that we saved in the first example. 

                    if (!doc.InitStdSecurityHandler("test"))
                    {
                        WriteLine("Document authentication error...");
                        WriteLine("The password is not valid.");
                        return;
                    }
                    else
                    {
                        WriteLine("The password is correct! Document can now be used for reading and editing");

                        // Remove the password security and save the changes to a new file.
                        doc.SetSecurityHandler(null);

                        String output_file_path = Path.Combine(OutputPath, "secured_nomore1.pdf");
                        await doc.SaveAsync(output_file_path, 0);

                        WriteLine("Done. Result saved in " + output_file_path);
                        await AddFileToOutputList(output_file_path).ConfigureAwait(false);
                        
                        WriteLine("Done. Result saved in secured_nomore1.pdf");

                        
                    }
                }
                catch (Exception e)
                {
                    WriteLine(GetExceptionMessage(e));
                }

                WriteLine("\n--------------------------------");
                WriteLine("To see how to open a document with user feedback, see the PDFViewCtrlDemo and the Open and HandlePassword functions.");
                WriteLine("--------------------------------\n");

                WriteLine("\n--------------------------------");
                WriteLine("Done Encryption Test.");
                WriteLine("--------------------------------\n");
            })).AsAsyncAction();
        }
    }
}
```

{% endcode %}
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.apryse.com/uwp/get-started/samples/enctest.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
