Advertisement
Guest User

Untitled

a guest
May 30th, 2024
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Add-Type -AssemblyName System.Windows.Forms
  2. Add-Type -AssemblyName System.Security
  3.  
  4. function Get-SecureStringFromConsole {
  5.     $password = Read-Host -Prompt "Enter Password" -AsSecureString
  6.     return $password
  7. }
  8.  
  9. # Create OpenFileDialog for selecting the file to encrypt
  10. $openFileDialog = New-Object System.Windows.Forms.OpenFileDialog
  11. $openFileDialog.Title = "Select the file to encrypt"
  12. $openFileDialog.Filter = "All files (*.*)|*.*"
  13.  
  14. if ($openFileDialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
  15.     $inputFile = $openFileDialog.FileName
  16. } else {
  17.     Write-Output "Operation cancelled."
  18.     exit
  19. }
  20.  
  21. $secureKey = Get-SecureStringFromConsole
  22.  
  23. # Convert SecureString to a byte array
  24. $keyBytes = [System.Text.Encoding]::UTF8.GetBytes((ConvertFrom-SecureString -SecureString $secureKey))
  25.  
  26. # Create AES encryption object
  27. $aes = [System.Security.Cryptography.Aes]::Create()
  28. $aes.Key = $keyBytes[0..31]  # AES-256 requires a 32-byte key
  29. $aes.IV = $keyBytes[0..15]   # AES requires a 16-byte IV
  30.  
  31. try {
  32.     $inputStream = [System.IO.File]::OpenRead($inputFile)
  33.     $outputFile = "$inputFile.movencr"
  34.     $outputStream = [System.IO.File]::Create($outputFile)
  35.  
  36.     # Create a CryptoStream for encryption
  37.     $encryptor = $aes.CreateEncryptor()
  38.     $cryptoStream = New-Object System.Security.Cryptography.CryptoStream ($outputStream, $encryptor, [System.Security.Cryptography.CryptoStreamMode]::Write)
  39.  
  40.     $buffer = New-Object byte[] 4096
  41.     $totalBytesRead = 0
  42.     $inputFileLength = $inputStream.Length
  43.  
  44.     Write-Host "Encrypting file..."
  45.     while (($bytesRead = $inputStream.Read($buffer, 0, $buffer.Length)) -gt 0) {
  46.         $totalBytesRead += $bytesRead
  47.         $cryptoStream.Write($buffer, 0, $bytesRead)
  48.  
  49.         # Update progress
  50.         $percentComplete = [math]::Round(($totalBytesRead / $inputFileLength) * 100)
  51.         Write-Progress -Activity "Encrypting" -Status "$percentComplete% Complete" -PercentComplete $percentComplete
  52.     }
  53.  
  54.     $cryptoStream.Close()
  55.     $outputStream.Close()
  56.     $inputStream.Close()
  57.  
  58.     Write-Host "File encrypted successfully. Encrypted file: $outputFile"
  59. } catch {
  60.     Write-Host "Error during encryption: $_"
  61. } finally {
  62.     $aes.Dispose()
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement