PowerShell ile AES Dosya Şifreleme ve Şifre Çözme

Hassas verileri şifrelemek kritik bir güvenlik gereksinimidir. PowerShell’in .NET altyapısını kullanarak AES şifreleme ile dosyalarınızı güvenli şekilde koruyabilirsiniz.

Dosya Şifreleme

PowerShell
function Protect-File {
    param([string]$InputFile, [string]$OutputFile, [string]$Password)
    $salt    = New-Object byte[] 32
    [System.Security.Cryptography.RandomNumberGenerator]::Create().GetBytes($salt)
    $derive  = New-Object System.Security.Cryptography.Rfc2898DeriveBytes($Password, $salt, 100000)
    $aes     = [System.Security.Cryptography.Aes]::Create()
    $aes.Key = $derive.GetBytes(32); $aes.IV = $derive.GetBytes(16)
    $inBytes = [System.IO.File]::ReadAllBytes($InputFile)
    $enc     = $aes.CreateEncryptor().TransformFinalBlock($inBytes, 0, $inBytes.Length)
    [System.IO.File]::WriteAllBytes($OutputFile, $salt + $enc)
    Write-Host "Sifrelendi: $OutputFile" -ForegroundColor Green
}
 

Şifre Çözme

PowerShell
function Unprotect-File {
    param([string]$InputFile, [string]$OutputFile, [string]$Password)
    $all    = [System.IO.File]::ReadAllBytes($InputFile)
    $salt   = $all[0..31]; $enc = $all[32..($all.Length-1)]
    $derive = New-Object System.Security.Cryptography.Rfc2898DeriveBytes($Password, $salt, 100000)
    $aes    = [System.Security.Cryptography.Aes]::Create()
    $aes.Key = $derive.GetBytes(32); $aes.IV = $derive.GetBytes(16)
    $dec    = $aes.CreateDecryptor().TransformFinalBlock($enc, 0, $enc.Length)
    [System.IO.File]::WriteAllBytes($OutputFile, $dec)
    Write-Host "Sifre cozuldu: $OutputFile" -ForegroundColor Green
}
 
Protect-File   -InputFile "gizli.txt" -OutputFile "gizli.enc" -Password "G1zl1P@ss!"
Unprotect-File -InputFile "gizli.enc"  -OutputFile "gizli_cozulmus.txt" -Password "G1zl1P@ss!"