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:

Полный код

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


Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу support@sautinsoft.ru или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже:



Вопросы и предложения всегда приветствуются!

Мы разрабатываем компоненты .Net с 2002 года. Мы знаем форматы PDF, DOCX, RTF, HTML, XLSX и Images. Если вам нужна помощь в создании, изменении или преобразовании документов в различных форматах, мы можем вам помочь. Мы напишем для вас любой пример кода абсолютно бесплатно.