1

一直在尝试冒险学习一些 C# 和 powershell,给自己一些小项目来尝试和学习。最近,我一直在尝试将一些代码从 powershell 转换为 C#,我相信我可以正常工作,但是在为 RijndaelManaged 创建 IV 时遇到了一些错误。

这是从互联网上提取的运行良好的 powershell 代码

function Decrypt-String($Encrypted, $Passphrase, $salt, $init="Yet another key")
{
   if($Encrypted -is [string]){
      $Encrypted = [Convert]::FromBase64String($Encrypted)
   }
   $r = new-Object System.Security.Cryptography.RijndaelManaged
   $pass = [System.Text.Encoding]::UTF8.GetBytes($Passphrase)
   $salt = [System.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()
}

这是我把它移到的 C# 代码

public static string Decrypt_String(string cipherText, string passPhrase, string Salt)
{
    string hashAlgorithm = "SHA1";
    int passwordIterations = 5;
    initName = "Yet another key";
    using (RijndaelManaged r = new RijndaelManaged())
    {
        byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
        byte[] PassPhraseBytes = Encoding.UTF8.GetBytes(passPhrase);
        byte[] SaltBytes = Encoding.UTF8.GetBytes(Salt);
        byte[] initVectorBytes = Encoding.UTF8.GetBytes(initName);

        PasswordDeriveBytes password = new PasswordDeriveBytes(PassPhraseBytes,SaltBytes,hashAlgorithm,passwordIterations);
        byte[] keyBytes = password.GetBytes(32); //(256 / 32)
        r.Key = keyBytes;

        SHA1Managed cHash = new SHA1Managed();
        r.IV = cHash.ComputeHash(Encoding.UTF8.GetBytes(initName),0,16);

        ICryptoTransform decryptor = r.CreateDecryptor();
        MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
        CryptoStream cryptoStream = new CryptoStream(memoryStream,
                                                  decryptor,
                                                  CryptoStreamMode.Read);
        StreamReader streamReader = new StreamReader(cryptoStream);
        string output = streamReader.ReadToEnd();
        return output;
    }


}

目前 ComputeHash 正在吐出一个错误,告诉我该值无效。这是我从工作加密函数中使用的值

密文 = "s6ZqNpJq05jsMh2+1BxZzJQDDiJGRQPqIYzBjYQHsgw="

saltValue = "}=[BJ8%)vjJDnQfmvC))))3Q"

密码 = "S@lt3d"

关于为什么 IV 无法正确设置的任何想法?

编辑:对不起,例外是

Line 38:             r.IV = cHash.ComputeHash(initVectorBytes, 0, 16);

Exception Details: System.ArgumentException: Value was invalid.

一种通用的

4

1 回答 1

2

@Nate 是正确的,您正在使用该ComputeHash方法的不同重载,并且您没有正确处理它:

Encoding.UTF8.GetBytes(initName)

这将返回一个与您的字符串长度相同的字节数组 - 15。但是通过传递0and 16,您要求ComputeHash使用数组的前16 个元素。

cHash.ComputeHash(Encoding.UTF8.GetBytes(initName),0,16);

因此,第一个解决方法是通过0and 15(或者可能是0and initName.Length),或者更好的是,返回到您在 powershell 脚本中使用的重载,它会自动计算出数组长度:

cHash.ComputeHash(Encoding.UTF8.GetBytes(initName));

但是您需要缩短结果数组(它返回长度 20,但您只需要 16):

using System.Linq;
...
cHash.ComputeHash(Encoding.UTF8.GetBytes(initName)).Take(16).ToArray();
于 2012-08-29T17:17:47.417 回答