Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Add-Type -AssemblyName System.Windows.Forms
- Add-Type -AssemblyName System.Security
- function Get-SecureStringFromConsole {
- $password = Read-Host -Prompt "Enter Password" -AsSecureString
- return $password
- }
- # Create OpenFileDialog for selecting the file to encrypt
- $openFileDialog = New-Object System.Windows.Forms.OpenFileDialog
- $openFileDialog.Title = "Select the file to encrypt"
- $openFileDialog.Filter = "All files (*.*)|*.*"
- if ($openFileDialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
- $inputFile = $openFileDialog.FileName
- } else {
- Write-Output "Operation cancelled."
- exit
- }
- $secureKey = Get-SecureStringFromConsole
- # Convert SecureString to a byte array
- $keyBytes = [System.Text.Encoding]::UTF8.GetBytes((ConvertFrom-SecureString -SecureString $secureKey))
- # Create AES encryption object
- $aes = [System.Security.Cryptography.Aes]::Create()
- $aes.Key = $keyBytes[0..31] # AES-256 requires a 32-byte key
- $aes.IV = $keyBytes[0..15] # AES requires a 16-byte IV
- try {
- $inputStream = [System.IO.File]::OpenRead($inputFile)
- $outputFile = "$inputFile.movencr"
- $outputStream = [System.IO.File]::Create($outputFile)
- # Create a CryptoStream for encryption
- $encryptor = $aes.CreateEncryptor()
- $cryptoStream = New-Object System.Security.Cryptography.CryptoStream ($outputStream, $encryptor, [System.Security.Cryptography.CryptoStreamMode]::Write)
- $buffer = New-Object byte[] 4096
- $totalBytesRead = 0
- $inputFileLength = $inputStream.Length
- Write-Host "Encrypting file..."
- while (($bytesRead = $inputStream.Read($buffer, 0, $buffer.Length)) -gt 0) {
- $totalBytesRead += $bytesRead
- $cryptoStream.Write($buffer, 0, $bytesRead)
- # Update progress
- $percentComplete = [math]::Round(($totalBytesRead / $inputFileLength) * 100)
- Write-Progress -Activity "Encrypting" -Status "$percentComplete% Complete" -PercentComplete $percentComplete
- }
- $cryptoStream.Close()
- $outputStream.Close()
- $inputStream.Close()
- Write-Host "File encrypted successfully. Encrypted file: $outputFile"
- } catch {
- Write-Host "Error during encryption: $_"
- } finally {
- $aes.Dispose()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement