Differences

This shows you the differences between the selected revisions of the page.

file_encryption_decrypt_script 2018-07-05 file_encryption_decrypt_script 2024-01-02 (current)
Line 2: Line 2:
The following PowerShell script can be used to decrypt files [[file_encryption|encrypted by WinSCP]], when you do not have WinSCP available. The following PowerShell script can be used to decrypt files [[file_encryption|encrypted by WinSCP]], when you do not have WinSCP available.
 +
 +//The script works both in legacy Windows PowerShell and modern PowerShell [Core], though it runs slower in the latter.//
 +
 +//For C# version of the ''AesCtrTransform'' function, see [[https://stackoverflow.com/q/6374437/850848#51188472|Can I use AES in CTR mode in .NET?]]//
<code powershell> <code powershell>
Line 22: Line 26:
    $counter = $salt.Clone()     $counter = $salt.Clone()
-    $xorMask = New-Object System.Collections.Queue<byte>+    # Particularly in pwsh, it runs much faster with non-generic Queue 
 +····$xorMask = New-Object System.Collections.Queue
    $zeroIv = New-Object byte[] $blockSize     $zeroIv = New-Object byte[] $blockSize
Line 64: Line 69:
    $aes = New-Object System.Security.Cryptography.AesManaged     $aes = New-Object System.Security.Cryptography.AesManaged
-    $key = [System.Runtime.Remoting.Metadata.W3cXsd2001.SoapHexBinary]::Parse($key).Value+    try 
 +    { 
 +        # .NET >= 5 
 +        $key = [System.Convert]::FromHexString($key) 
 +    } 
 +    catch [System.Management.Automation.RuntimeException] 
 +    { 
 +        # .NET Framework 
 +········$key = [System.Runtime.Remoting.Metadata.W3cXsd2001.SoapHexBinary]::Parse($key).Value 
 +    }
    $keySize = $aes.KeySize / 8     $keySize = $aes.KeySize / 8
    if ($key.Length -ne $keySize)     if ($key.Length -ne $keySize)
Line 76: Line 90:
    if (-not ($name -like "*$aesCtrExt"))     if (-not ($name -like "*$aesCtrExt"))
    {     {
-        throw "$name has not a know encrypted file extension [$aesCtrExt]"+        throw "$name has not a known encrypted file extension [$aesCtrExt]"
    }     }
Line 83: Line 97:
    if (($padding -gt 0) -and ($padding -lt 4))     if (($padding -gt 0) -and ($padding -lt 4))
    {     {
-        $base64 = "=" * $padding+        $base64 += "=" * $padding
    }     }
Line 158: Line 172:
} }
</code> </code>
 +

Last modified: by martin