Differences
This shows you the differences between the selected revisions of the page.
file_encryption_decrypt_script 2021-06-14 | 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?]]// | //For C# version of the ''AesCtrTransform'' function, see [[https://stackoverflow.com/q/6374437/850848#51188472|Can I use AES in CTR mode in .NET?]]// | ||
Line 24: | 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 66: | 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) |