Acquira

Encrypt Decrypt PS

Nov 18th, 2021
1,938
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Encrypt-File {
  2.     [CmdletBinding()]
  3.     param (
  4.       [Parameter(Mandatory = $false)]
  5.       [byte[]] $Key,
  6.       # Path to the file that will be encrypted
  7.       [Parameter(Mandatory = $true)]
  8.       [string] $Path,
  9.       # Path to the resulting encrypted file
  10.       [string] $Destination
  11.     )
  12.     if (-not $PSBoundParameters['Destination']) {
  13.       $Destination = (Resolve-Path -Path $Path).Path + '.encrypted'
  14.     }
  15.  
  16.     $AES = [System.Security.Cryptography.AesManaged]::new()
  17.     if ($PSBoundParameters['Key']) { $AES.Key = $Key }
  18.  
  19.     $Encryptor = $AES.CreateEncryptor()
  20.  
  21.     Write-Verbose -Message 'Reading source file'
  22.  
  23.     $FileStream = [System.IO.FileStream]::new($Path, [System.IO.FileMode]::OpenOrCreate)
  24.     $FileWriter = [System.IO.File]::OpenWrite($Destination)
  25.  
  26.     $CryptoStream = [System.Security.Cryptography.CryptoStream]::new($FileWriter, $Encryptor, [System.Security.Cryptography.CryptoStreamMode]::Write)
  27.     Write-Verbose -Message 'Created CryptoStream'
  28.  
  29.     $FileStream.CopyTo($CryptoStream)
  30.  
  31.     Write-Verbose -Message 'Finished writing bytes to CryptoStream'
  32.  
  33.     $AES.Key | Set-Content -Path ($Path + '.encrypted.key') -AsByteStream
  34.     $CryptoStream.Flush()
  35.     $CryptoStream.FlushFinalBlock()
  36.     $FileWriter.Flush()
  37.     $CryptoStream.Clear()
  38.     $FileWriter.Close()
  39.     $FileStream.Close()
  40. }
  41.  
  42. function Decrypt-File {
  43.   [CmdletBinding()]
  44.   param (
  45.     [Parameter(Mandatory = $false)]
  46.     [string] $KeyPath,
  47.     # Path to the file that will be decrypted
  48.     [Parameter(Mandatory = $true)]
  49.     [string] $Path,
  50.     # Path to the decrypted file
  51.     [string] $Destination
  52.   )
  53.   if (!$PSBoundParameters['KeyPath']) { $KeyPath = $Path + '.key' }
  54.   if (!$PSBoundParameters['Destination']) { $Destination = $Path + '.decrypted' }
  55.  
  56.   $AES = [System.Security.Cryptography.AesManaged]::new()
  57.   $AES.Key = Get-Content -AsByteStream -Path $KeyPath
  58.  
  59.   $Decryptor = $AES.CreateDecryptor()
  60.  
  61.   $EncryptedFile = [System.IO.File]::OpenRead($Path)
  62.   $DecryptedFile = [System.IO.File]::OpenWrite($Destination)
  63.  
  64.   $CryptoStream = [System.Security.Cryptography.CryptoStream]::new($DecryptedFile, $Decryptor, [System.Security.Cryptography.CryptoStreamMode]::Write)
  65.  
  66.   $EncryptedFile.CopyTo($CryptoStream)
  67.  
  68.   $CryptoStream.Flush()
  69.   $DecryptedFile.Flush()
  70.   $EncryptedFile.Flush()
  71.   $CryptoStream.Clear()
  72.   $EncryptedFile.Close()
  73.   $DecryptedFile.Close()
  74. }
Advertisement
Add Comment
Please, Sign In to add comment