Skip to content

Creating Encrypted Archives

Learn how to create archives protected with AES-256 encryption.

Encryption Types

zesven supports two types of encryption that can be used independently or together:

TypeMethodWhat it protects
Content encryptionencrypt_data(true)File contents
Header encryptionencrypt_header(true)File names and metadata

Content Encryption

Encrypt file contents with encrypt_data():

rust
use zesven::{Writer, WriteOptions, Password, ArchivePath, Result};

fn main() -> Result<()> {
    let options = WriteOptions::new()
        .password(Password::new("secret_password"))
        .encrypt_data(true);  // Encrypt file contents

    let mut writer = Writer::create_path("encrypted.7z")?
        .options(options);

    writer.add_bytes(ArchivePath::new("secret.txt")?, b"Secret data")?;
    writer.finish()?;
    Ok(())
}

With content encryption:

  • File data is encrypted with AES-256
  • File names remain visible (use header encryption to hide them)
  • Password required to extract files

Header Encryption

Hide file names by encrypting the header:

rust
use zesven::{Writer, WriteOptions, Password, ArchivePath, Result};

fn main() -> Result<()> {
    let options = WriteOptions::new()
        .password(Password::new("secret"))
        .encrypt_header(true);  // Hide file names

    let mut writer = Writer::create_path("fully_encrypted.7z")?
        .options(options);

    writer.add_bytes(ArchivePath::new("classified.txt")?, b"Top secret")?;
    writer.finish()?;
    Ok(())
}

With header encryption:

  • File names are hidden until password is entered
  • Archive structure is not visible
  • Provides better privacy

Full Encryption

For maximum security, enable both content and header encryption:

rust
use zesven::{Writer, WriteOptions, Password, ArchivePath, Result};

fn main() -> Result<()> {
    let options = WriteOptions::new()
        .password(Password::new("secret"))
        .encrypt_data(true)    // Encrypt file contents
        .encrypt_header(true); // Hide file names

    let mut writer = Writer::create_path("fully_encrypted.7z")?
        .options(options);

    writer.add_bytes(ArchivePath::new("classified.txt")?, b"Top secret")?;
    writer.finish()?;
    Ok(())
}

This provides:

  • Content protection: File data encrypted with AES-256
  • Metadata protection: File names and sizes hidden
  • Privacy: No information visible without the password

Key Derivation Strength

The 7z format uses SHA-256 with configurable iteration counts for key derivation. The default iteration count provides a good balance between security and performance. Higher iteration counts make brute-force attacks slower but also slow down legitimate access.

Iterations (2^n)ValueSecuritySpeed
1665,536MinimumFast
19524,288DefaultMedium
224,194,304HighSlow
2416,777,216MaximumVery slow

Combining with Compression

Encryption works with all compression settings:

rust
use zesven::{Writer, WriteOptions, Password, ArchivePath, Result};

fn main() -> Result<()> {
    let options = WriteOptions::new()
        .password(Password::new("secret"))
        .encrypt_header(true)
        .level(9)?
        .solid();

    let mut writer = Writer::create_path("secure_backup.7z")?
        .options(options);

    writer.add_path("documents", ArchivePath::new("documents")?)?;
    writer.finish()?;
    Ok(())
}

Password Security

Best practices for passwords:

rust
use zesven::{Writer, WriteOptions, Password, ArchivePath, Result};
use zeroize::Zeroizing;

fn main() -> Result<()> {
    // Use Zeroizing to clear password from memory
    let password = Zeroizing::new(String::from("my_secure_password_123!"));

    let options = WriteOptions::new()
        .password(Password::new(&*password))
        .encrypt_header(true);

    let mut writer = Writer::create_path("secure.7z")?
        .options(options);

    writer.add_bytes(ArchivePath::new("data.txt")?, b"Sensitive data")?;
    writer.finish()?;

    // password is automatically zeroed when dropped
    Ok(())
}

Password Guidelines

For secure archives:

  • Use 12+ characters
  • Mix uppercase, lowercase, numbers, and symbols
  • Avoid dictionary words
  • Use a password manager to generate and store passwords
  • Enable header encryption for maximum privacy
  • Use high iteration counts for sensitive data

See Also

Released under MIT OR Apache-2.0 License