Загрузка данных



$dir = Join-Path ([Environment]::GetFolderPath("Desktop")) "LR3"
New-Item -ItemType Directory -Force -Path $dir | Out-Null
New-Item -ItemType Directory -Force -Path (Join-Path $dir "screenshots") | Out-Null

$source = Join-Path $dir "test.txt"
if (-not (Test-Path $source)) {
    "Laboratory work 3 - password protection test file." |
        Set-Content -Path $source -Encoding UTF8
}

# Find 7-Zip
$sevenZip = @(
    "$env:ProgramFiles\7-Zip\7z.exe",
    "${env:ProgramFiles(x86)}\7-Zip\7z.exe"
) | Where-Object { Test-Path $_ } | Select-Object -First 1

if (-not $sevenZip) {
    $cmd = Get-Command 7z.exe -ErrorAction SilentlyContinue
    if ($cmd) { $sevenZip = $cmd.Source }
}
if (-not $sevenZip) { throw "7-Zip not found!" }

# Passwords for archives 1-12
$archives = @(
    [pscustomobject]@{Number=1;  Name="01_digits.zip";        Password="4837"}
    [pscustomobject]@{Number=2;  Name="02_upper.zip";         Password="ABCD"}
    [pscustomobject]@{Number=3;  Name="03_lower.zip";         Password="abcd"}
    [pscustomobject]@{Number=4;  Name="04_special.zip";       Password='!@#$'}
    [pscustomobject]@{Number=5;  Name="05_digit_upper.zip";   Password="1A2B"}
    [pscustomobject]@{Number=6;  Name="06_digit_lower.zip";   Password="1a2b"}
    [pscustomobject]@{Number=7;  Name="07_digit_letters.zip"; Password="1Aa2"}
    [pscustomobject]@{Number=8;  Name="08_digit_upper_special.zip"; Password="1A@2"}
    [pscustomobject]@{Number=9;  Name="09_digit_lower_special.zip"; Password="1a@2"}
    [pscustomobject]@{Number=10; Name="10_letters_special.zip";     Password="Aa@B"}
    [pscustomobject]@{Number=11; Name="11_all_groups.zip";          Password="1Aa@"}
    [pscustomobject]@{Number=12; Name="12_english.zip";      Password="algorithm"}
)

foreach ($a in $archives) {
    $target = Join-Path $dir $a.Name

    if (Test-Path $target) {
        Write-Host "EXISTS: $($a.Name)" -ForegroundColor Yellow
        continue
    }

    & $sevenZip a -tzip -mem=AES256 "-p$($a.Password)" -y $target $source | Out-Null

    if ($LASTEXITCODE -ne 0) {
        Write-Warning "Failed: $($a.Name)"
    } else {
        Write-Host "CREATED: $($a.Name)" -ForegroundColor Green
    }
}

# Archive 13: RAR4 / AES-128
$rar = @(
    "$env:ProgramFiles\WinRAR\Rar.exe",
    "$env:ProgramFiles\WinRAR\WinRAR.exe",
    "${env:ProgramFiles(x86)}\WinRAR\Rar.exe",
    "${env:ProgramFiles(x86)}\WinRAR\WinRAR.exe"
) | Where-Object { Test-Path $_ } | Select-Object -First 1

$rarName = "13_russian.rar"
$rarPath = Join-Path $dir $rarName
$rarPassword = [string]([char[]]@(0x0430,0x043A,0x0446,0x0438,0x043E,0x043D,0x0435,0x0440) -join '')

if (Test-Path $rarPath) {
    Write-Host "EXISTS: $rarName" -ForegroundColor Yellow
} elseif ($rar) {
    & $rar a -ma4 "-hp$rarPassword" -y $rarPath $source

    if (($LASTEXITCODE -eq 0) -and (Test-Path $rarPath)) {
        Write-Host "CREATED: $rarName" -ForegroundColor Green
    } else {
        Write-Warning "RAR4 creation unavailable in this WinRAR version."
    }
} else {
    Write-Warning "WinRAR not found. Archive 13 not created."
}

# Save password reference table
$all = @($archives) + @(
    [pscustomobject]@{
        Number=13
        Name=$rarName
        Password=$rarPassword
    }
)

$all | Select-Object Number,Name,Password |
    Export-Csv -Path (Join-Path $dir "passwords.csv") `
        -NoTypeInformation -Delimiter ";" -Encoding UTF8

Write-Host ""
Write-Host "DONE. Folder: $dir" -ForegroundColor Cyan
Write-Host "Password table: passwords.csv"