Strengthening Password Security in C# and .NET

In the modern world of information technology, data security is one of the key aspects of software development. Data encryption allows you to ensure confidentiality and protect information from unauthorized access.

In this article, we will look at how you can protect data using encryption in C# and .NET using the SautinSoft.Pdf component.

Below is an example of password encryption of an existing PDF file:

  1. Add SautinSoft.PDF from NuGet.
  2. Load the PDF document.
  3. Set password-based encryption using the password required to open the PDF document.
  4. Save the PDF document in an encrypted PDF file.

Output result:

Complete code

using System;
using SautinSoft.Pdf;
using System.IO;

class Program
{
    /// <summary>
    /// Password Protection.
    /// </summary>
    /// <remarks>
    /// Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/password-protection.php
    /// </remarks>
    static void Main()
    {
            // Before starting this example, please get a free 100-day trial key:
            // https://sautinsoft.com/start-for-free/

            // Apply the key here:
            // PdfDocument.SetLicense("...");

        using (var document = PdfDocument.Load(Path.GetFullPath(@"..\..\..\simple text.pdf")))
        {
            // Set password-based encryption with password required to open a PDF document.
            document.SaveOptions.SetPasswordEncryption().DocumentOpenPassword = "user1234";

            // Save PDF document to an encrypted PDF file.
            document.Save("SautinSoft.pdf");
        }

        System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("SautinSoft.pdf") { UseShellExecute = true });
    }
}

Download

Option Infer On

Imports System
Imports SautinSoft.Pdf
Imports System.IO

Friend Class Program
	''' <summary>
	''' Password Protection.
	''' </summary>
	''' <remarks>
	''' Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/password-protection.php
	''' </remarks>
	Shared Sub Main()
			' Before starting this example, please get a free 100-day trial key:
			' https://sautinsoft.com/start-for-free/

			' Apply the key here:
			' PdfDocument.SetLicense("...");

		Using document = PdfDocument.Load(Path.GetFullPath("..\..\..\simple text.pdf"))
			' Set password-based encryption with password required to open a PDF document.
			document.SaveOptions.SetPasswordEncryption().DocumentOpenPassword = "user1234"

			' Save PDF document to an encrypted PDF file.
			document.Save("SautinSoft.pdf")
		End Using

		System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo("SautinSoft.pdf") With {.UseShellExecute = True})
	End Sub
End Class

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.