---
title: "Create a password-protected 7z file in C#"
url: https://recursiveai.org/onearchive/cookbook/create-password-protected-7z-archive/
description: "Create a password-protected 7z file in C# with AES-256, open it with the same password, and choose whether file names stay visible."
version: "1.2.0"
---

# Create a password-protected 7z file in C#

**OneArchive** is a free, open-source archive and compression library for .NET from Recursive AI: zip, tar, 7z, RAR and more in RecursiveAI.OneArchive, with compression codecs in RecursiveAI.OneCompress. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneArchive) · [GitHub](https://github.com/recursive-ai-oss/onearchive-csharp) · [Docs](https://recursiveai.org/onearchive/docs/) · [Download example (.zip)](https://recursiveai.org/onearchive/cookbook/create-password-protected-7z-archive/create-password-protected-7z-archive.zip)

Set `Password` when creating a 7z. By default it encrypts both contents and names.

```shell
dotnet add package RecursiveAI.OneArchive
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneArchive;
File.WriteAllText("note.txt", "Draft only.");
var options = new ArchiveCreateOptions
    { Password = "demo-password" };
Archive.Create("locked.7z", options)
    .Add("note.txt").Save();
Console.WriteLine("Created locked.7z");
```

Unzip [the example](https://recursiveai.org/onearchive/cookbook/create-password-protected-7z-archive/create-password-protected-7z-archive.zip), then `dotnet run`.

Writes `locked.7z` with AES-256 and an encrypted header. It prints:

```text
Created locked.7z
```

## The full program

The program creates `note.txt` containing `Draft only.` It tries to list the archive without a password, then reopens it with the password and reads the note.

```csharp
using RecursiveAI.OneArchive;

const string password = "demo-password";
File.WriteAllText("note.txt", "Draft only.");
Archive.Create("locked.7z", new ArchiveCreateOptions
{
    Password = password
}).Add("note.txt").Save();
Console.WriteLine("Created locked.7z");

try
{
    using var locked = Archive.Open("locked.7z");
    _ = locked.Entries.ToArray();
}
catch (ArchivePasswordException error)
{
    Console.WriteLine($"Listing without password: {error.Problem}");
}

using var archive = Archive.Open("locked.7z", new ArchiveOpenOptions
{
    Password = password
});
var entry = archive.Entries.Single();
using var reader = new StreamReader(archive.OpenEntry(entry));
Console.WriteLine($"{entry.Name}: {reader.ReadToEnd()}");
```

```text
Created locked.7z
Listing without password: Required
note.txt: Draft only.
```

Trying to list an archive with an encrypted header and no password raises `ArchivePasswordException`. With header encryption off, the [7z writer docs](https://recursiveai.org/onearchive/docs/7z-writer/#encryption) say “the header stays readable and only file content is encrypted.”

## Variations

**Encrypt contents but leave names visible.** This writes a second archive from the same note and lists its name without the password. `EncryptHeader` is only available in `SevenZipWriterOptions`, so this example sets the password there too.

```csharp
using RecursiveAI.OneArchive;
using RecursiveAI.OneArchive.SevenZip;
File.WriteAllText("note.txt", "Draft only.");
var options = new ArchiveCreateOptions
{
    SevenZip = new SevenZipWriterOptions
    {
        Password = "demo-password",
        EncryptHeader = false
    }
};
Archive.Create("visible.7z", options)
    .Add("note.txt").Save();
using var archive = Archive.Open("visible.7z");
Console.WriteLine(archive.Entries.Single().Name);
```

```text
note.txt
```

## Related recipes

- [Create a 7z archive](https://recursiveai.org/onearchive/cookbook/create-7z/)
- [Extract a 7z archive](https://recursiveai.org/onearchive/cookbook/extract-7z/)
- [Create a ZIP file with a password](https://recursiveai.org/onearchive/cookbook/zip-encryption/)

## The short answer

Create a `.7z` archive with `ArchiveCreateOptions.Password`, add the files, and call `Save`. OneArchive encrypts the contents with 7z AES-256 and encrypts the header by default. Give `Archive.Open` the same password in `ArchiveOpenOptions` to list and read the entries. Use a password supplied at runtime for real files rather than the literal string in the example.

## Can a 7z file be password protected?

Yes. The `.7z` extension selects the 7z writer; no separate command-line program is needed. The example creates a new archive from a text file. For your own input, replace `note.txt` in `Add` with the source file path and keep `Save` after the last entry. This writes a new archive, not an in-place password change to an existing 7z.

OneArchive decrypts the header when reading the index. Pass the password to `Archive.Open`, not just at the point where you read an entry. AES-encrypted 7z reading and writing aren’t supported on browser WebAssembly; see the [platform notes](https://recursiveai.org/onearchive/docs/zip-encryption/).

## How do I encrypt file names in a 7z archive?

Leave `EncryptHeader` at its default of `true` when you set a password. The header is the index containing file names, sizes, and timestamps. Encrypting it makes it possible to share the archive without revealing that index to someone who doesn’t know the password. Name the outer `.7z` file appropriately too: header encryption doesn’t hide the archive’s own name.

If someone needs to see what files an archive contains before they have the password, use `EncryptHeader = false` as shown above. They can list the entries, but still need the password to read the contents. Unlike the 7z default, names stay visible, the same as in AES-encrypted ZIP files.
