Created
August 8, 2023 08:41
-
-
Save Bungeetaco/445d7315d68b31413ece50dd9fab22a1 to your computer and use it in GitHub Desktop.
A powershell script to bulk decrypt files encrypted with SharpAESCrypt (Duplicati)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Function to update the progress bar | |
function Update-ProgressBar { | |
param ( | |
[int]$Completed, | |
[int]$Total, | |
[int]$RunningJobs, | |
[int]$FailedJobs | |
) | |
$RemainingFiles = $Total - $Completed - $FailedJobs # Subtract failed jobs from remaining files | |
$Progress = [math]::Round(($Completed / $Total) * 100, 3) # Calculate progress percentage rounded to 3 decimals | |
$ProgressBar = "[{0}{1}] {2,6:F3}% - Comp: {3}/{4} - Rem: {5}/{4} - Run: {6} - Fail: {7}" -f ('#' * ($Progress / 10)), (' ' * (10 - $Progress / 10)), $Progress, $Completed, $Total, $RemainingFiles, $RunningJobs, $FailedJobs | |
Write-Progress -Activity "Processing Files" -Status $ProgressBar -Id 1 | |
} | |
# Set the paths and parameters - change the SourceDirectory and OutputDirectory paths to suit your needs | |
$ScriptDirectory = Split-Path -Parent $MyInvocation.MyCommand.Path | |
$SharpAESCryptPath = Join-Path $ScriptDirectory "SharpAESCrypt.exe" | |
$Password = "password" | |
$SharpTask = "d" | |
$SourceDirectory = "F:\backblaze-duplicati\SourceAES" | |
$OutputDirectory = "F:\backblaze-duplicati\DestZIP" | |
# Get a list of files to decrypt | |
$FilesToDecrypt = Get-ChildItem -Path $SourceDirectory -Filter "*.zip.aes" -File | |
$TotalFiles = $FilesToDecrypt.Count | |
$CompletedFiles = 0 | |
$FailedJobs = 0 | |
$MaxConcurrentJobs = 5 | |
$JobResults = @{} | |
# Script block to decrypt a single file | |
$DecryptFileScript = { | |
param ( | |
$SharpAESCryptPath, | |
$SharpTask, | |
$Password, | |
$SourceFilePath, | |
$OutputFilePath | |
) | |
# Execute the decryption command | |
& $SharpAESCryptPath $SharpTask $Password $SourceFilePath $OutputFilePath | |
if ($LASTEXITCODE -eq 0) { | |
Write-Host "Successfully decrypted $SourceFilePath." | |
return $true | |
} else { | |
if ($result -match "Invalid password or corrupted data") { | |
Write-Host "Error: Invalid password or corrupted data occurred while decrypting $SourceFilePath." | |
} else { | |
Write-Host "An error occurred while decrypting $SourceFilePath." | |
} | |
return $false | |
} | |
} | |
$JobQueue = New-Object System.Collections.Queue | |
$RunningJobs = 0 | |
# Start processing each file in the source directory | |
foreach ($File in $FilesToDecrypt) { | |
$OutputFile = Join-Path $OutputDirectory ($File.BaseName + ".zip") | |
# Start a new job to decrypt the file | |
$Job = Start-Job -ScriptBlock $DecryptFileScript -ArgumentList $SharpAESCryptPath, $SharpTask, $Password, $File.FullName, $OutputFile | |
$JobQueue.Enqueue($Job) | |
$RunningJobs++ | |
# Update the progress bar | |
Update-ProgressBar -Completed $CompletedFiles -Total $TotalFiles -RunningJobs $RunningJobs -FailedJobs $FailedJobs | |
# Limit the number of concurrent jobs to $MaxConcurrentJobs | |
while ($RunningJobs -ge $MaxConcurrentJobs) { | |
Start-Sleep -Milliseconds 500 | |
$CompletedJob = $JobQueue.Dequeue() | |
$Result = Receive-Job $CompletedJob | |
if ($Result -eq $false) { | |
$FailedJobs++ | |
} else { | |
$CompletedFiles++ | |
} | |
Stop-Job $CompletedJob | |
Remove-Job $CompletedJob | |
$RunningJobs-- | |
Update-ProgressBar -Completed $CompletedFiles -Total $TotalFiles -RunningJobs $RunningJobs -FailedJobs $FailedJobs | |
} | |
} | |
# Wait for any remaining jobs to complete | |
while ($JobQueue.Count -gt 0) { | |
Start-Sleep -Milliseconds 500 | |
$CompletedJob = $JobQueue.Dequeue() | |
$Result = Receive-Job $CompletedJob | |
if ($Result -eq $false) { | |
$FailedJobs++ | |
} else { | |
$CompletedFiles++ | |
} | |
Stop-Job $CompletedJob | |
Remove-Job $CompletedJob | |
$RunningJobs-- | |
Update-ProgressBar -Completed $CompletedFiles -Total $TotalFiles -RunningJobs $RunningJobs -FailedJobs $FailedJobs | |
} | |
# Export summary to results.txt file | |
$Summary = @" | |
Decryption process completed. | |
Total Files: $TotalFiles | |
Successfully Decrypted: $CompletedFiles | |
Failed Jobs: $FailedJobs | |
Remaining Files: $($TotalFiles - $CompletedFiles - $FailedJobs) | |
"@ | |
$ResultsFilePath = Join-Path $ScriptDirectory "results.txt" | |
$Summary | Out-File -FilePath $ResultsFilePath | |
# Display completion message | |
Write-Host "" # New line after processing is complete | |
Write-Host "Decryption process completed." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment