Security Developer Guide

The Security class in Aspose.Pdf.LowCode provides a streamlined API for encrypting and decrypting PDF documents.

Installation and Setup

Install via NuGet:

dotnet add package Aspose.PDF

Apply metered licensing before use:

using Aspose.Pdf;

Metered metered = new Metered();
metered.SetMeteredKey("publicKey", "privateKey");

Features and Functionalities

File Format Support

  • Encrypt: produces a password-protected PDF using EncryptionOptions
  • Decrypt: removes password protection using DecryptionOptions

Simple Conversion

Encrypt a PDF with a user password, an owner password, a permission set, and an encryption algorithm:

using Aspose.Pdf.LowCode;
using Aspose.Pdf.Facades;

var security = new Security();
var encryptOptions = new EncryptionOptions(
    "userPassword123",
    "ownerPassword456",
    DocumentPrivilege.ForbidAll,
    CryptoAlgorithm.AESx256);

encryptOptions.AddInput(new FileDataSource("input.pdf"));
encryptOptions.AddOutput(new FileDataSource("encrypted.pdf"));
security.Process(encryptOptions);

Advanced Conversion with Options

For fine-grained control:

using Aspose.Pdf.LowCode;
using Aspose.Pdf.Facades;

// AllowAll/ForbidAll each return a fresh instance -- customizing one
// does not affect a later call to the same preset.
var permissions = DocumentPrivilege.AllowAll;
permissions.AllowPrint = false;
permissions.AllowCopy = false;

var encryptOptions = new EncryptionOptions(
    "userPassword123",
    "ownerPassword456",
    permissions,
    CryptoAlgorithm.AESx256);

encryptOptions.AddInput(new FileDataSource("input.pdf"));
encryptOptions.AddOutput(new FileDataSource("encrypted.pdf"));

var security = new Security();
security.Process(encryptOptions);

Stream-Based Processing

For in-memory or web application scenarios:

using System.IO;
using Aspose.Pdf.LowCode;
using Aspose.Pdf.Facades;

using (var inputStream = File.OpenRead("input.pdf"))
using (var outputStream = new MemoryStream())
{
    var encryptOptions = new EncryptionOptions(
        "userPassword123", "ownerPassword456",
        DocumentPrivilege.ForbidAll, CryptoAlgorithm.AESx256);

    // CloseOutputStreams defaults to true -- set it to false to keep
    // reading the output stream after Process() returns.
    encryptOptions.CloseOutputStreams = false;
    encryptOptions.AddInput(new StreamDataSource(inputStream));
    encryptOptions.AddOutput(new StreamDataSource(outputStream));

    var security = new Security();
    security.Process(encryptOptions);

    // outputStream now holds the encrypted PDF bytes
}

Usage Examples

Basic File Conversion

using Aspose.Pdf.LowCode;
using Aspose.Pdf.Facades;

var security = new Security();
var decryptOptions = new DecryptionOptions("ownerPassword456");
decryptOptions.AddInput(new FileDataSource("encrypted.pdf"));
decryptOptions.AddOutput(new FileDataSource("decrypted.pdf"));
security.Process(decryptOptions);

Batch Processing

using System.IO;
using Aspose.Pdf.LowCode;
using Aspose.Pdf.Facades;

string inputDir = "input";
string outputDir = "output";
Directory.CreateDirectory(outputDir);

foreach (var file in Directory.GetFiles(inputDir, "*.pdf"))
{
    string output = Path.Combine(outputDir, Path.GetFileName(file));
    var encryptOptions = new EncryptionOptions(
        "userPassword123", "ownerPassword456",
        DocumentPrivilege.ForbidAll, CryptoAlgorithm.AESx256);
    encryptOptions.AddInput(new FileDataSource(file));
    encryptOptions.AddOutput(new FileDataSource(output));

    var security = new Security();
    security.Process(encryptOptions);
}

Tips and Best Practices

  • Choose the right algorithm: AESx256 is the strongest of the four directly supported values (RC4x40, RC4x128, AESx128, AESx256); all four were individually confirmed to work.
  • Permission presets: start from DocumentPrivilege.AllowAll or ForbidAll and set only the switches you need to change – each access returns a fresh instance, so customizing one never affects another.
  • Decrypting needs only the owner password: DecryptionOptions takes a single owner-password argument.
  • Streams: set CloseOutputStreams = false on EncryptionOptions if you need to read the output stream after Process() returns.

Common Issues and Resolutions

IssueResolution
File not foundVerify path is correct; use absolute paths in production
Unsupported formatCheck supported formats list; ensure file extension matches content
Evaluation watermarkApply a valid license before processing
Memory errors on large filesSwitch to stream-based processing

API Reference

  • Security — Main plugin class; Process() encrypts or decrypts depending on the options object passed to it
  • EncryptionOptions — Encryption configuration: user/owner passwords, permission set, algorithm
  • DecryptionOptions — Decryption configuration: owner password only

See Also