0

有人可以帮我解决以下问题吗?我有以下功能可以正常工作

function Decrypt-String($Encrypted, $Passphrase, $salt, $init) 
{ 
    if($Encrypted -is [string]){ 
        $Encrypted = [Convert]::FromBase64String($Encrypted) 
       } 

    $r = new-Object System.Security.Cryptography.RijndaelManaged 
    $pass = [Text.Encoding]::UTF8.GetBytes($Passphrase) 
    $salt = [Text.Encoding]::UTF8.GetBytes($salt) 
    $r.Key = (new-Object Security.Cryptography.PasswordDeriveBytes $pass, $salt, "SHA1", 5).GetBytes(32) #256/8 
    $r.IV = (new-Object Security.Cryptography.SHA1Managed).ComputeHash( [Text.Encoding]::UTF8.GetBytes($init) )[0..15] 

    $d = $r.CreateDecryptor() 
    $ms = new-Object IO.MemoryStream @(,$Encrypted) 
    $cs = new-Object Security.Cryptography.CryptoStream $ms,$d,"Read" 
    $sr = new-Object IO.StreamReader $cs 
    Write-Output $sr.ReadToEnd() 
    $sr.Close() 
    $cs.Close() 
    $ms.Close() 
    $r.Clear() 
}

代码中有两个地方引用了 SHA1

我想切换到 SHA256,尽管上面分类的 .NET 都支持 SHA256,但它会引发以下错误是我将粗体文本从 SHA1 单独更改为 SHA256

Exception calling "ReadToEnd" with "0" argument(s): "Padding is invalid and cannot be removed."
At J:\UTemp\063146ee-175d-4a33-b485-7c6dd0e309f6.ps1:28 char:5
+     Write-Output $sr.ReadToEnd()
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : CryptographicException

Exception calling "Close" with "0" argument(s): "Padding is invalid and cannot be removed."
At J:\UTemp\063146ee-175d-4a33-b485-7c6dd0e309f6.ps1:29 char:5
+     $sr.Close()
+     ~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : CryptographicException

任何帮助都欢迎厄尼

4

0 回答 0