【问题标题】:How to write an extension method in C# with multiple parameters?如何在 C# 中编写具有多个参数的扩展方法?
【发布时间】:2019-06-06 00:15:32
【问题描述】:

我写了这个扩展方法,但我只得到一个参数。

我的 C# 代码:

public static string ToEncrypt(this string key, string passWord)
{
    // Salt and IV is randomly generated each time, but is prepended to encrypted cipher text
    // so that the same Salt and IV values can be used when decrypting.  
    var saltStringBytes = Generate256BitsOfRandomEntropy();
    var ivStringBytes = Generate256BitsOfRandomEntropy();
    var plainTextBytes = Encoding.UTF8.GetBytes(key);

    using (var password = new Rfc2898DeriveBytes(passWord, saltStringBytes, DerivationIterations))
    {
        var keyBytes = password.GetBytes(Keysize / 8);

        using (var symmetricKey = new RijndaelManaged())
        {
            symmetricKey.BlockSize = 256;
            symmetricKey.Mode = CipherMode.CBC;
            symmetricKey.Padding = PaddingMode.PKCS7;

            using (var encryptor = symmetricKey.CreateEncryptor(keyBytes, ivStringBytes))
            {
                using (var memoryStream = new MemoryStream())
                {
                    using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                    {
                        cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
                        cryptoStream.FlushFinalBlock();
                        // Create the final bytes as a concatenation of the random salt bytes, the random iv bytes and the cipher bytes.
                        var cipherTextBytes = saltStringBytes;
                        cipherTextBytes = cipherTextBytes.Concat(ivStringBytes).ToArray();
                        cipherTextBytes = cipherTextBytes.Concat(memoryStream.ToArray()).ToArray();
                        memoryStream.Close();
                        cryptoStream.Close();

                        return Convert.ToBase64String(cipherTextBytes);
                    }
                }
            }
        }
    }
}

我尝试使用这种扩展方法:

CallBack Miss Parameter

我在 google 中搜索过,但找不到解决问题的方法。

谢谢大家!对不起我的英语不好

【问题讨论】:

  • 我不明白问题出在哪里。扩展方法是一种单独接收至少一个参数的方法。第一个参数的类型是具有此方法的类型,就好像它在原始类中一样。因此,由于您的方法有两个参数,当作为扩展方法调用时,它将有一个:passWord。那么你的问题是什么?
  • 是的,正如@Andrew 所说,将第一个参数视为调用扩展方法的对象。您可能希望在方法声明中切换密钥和密码的顺序。
  • 我同意@Ben,我认为这是你的问题。顺便说一句,您的方法可能应该被称为Encrypt,或者可能是ToEncrypted。我强烈不鼓励使用两个名称相似的变量:passwordpassWord
  • 如果这是用于存储用户密码,您应该对它们进行散列,而不是对其进行加密。您永远不能将用户的密码反转为明文。您应该将用户输入的密码的哈希值与存储在数据库中的哈希值进行比较。如果这是用于用户密码,请研究存储密码的最佳方法。

标签: c# asp.net web extension-methods


【解决方案1】:

您的问题是您的扩展方法是为它所作用的字符串编写的,而不是“密码”

所以你写的代码应该是

var key = “some key”;
var encryptedpass = key.ToEncrypt(password);

您的代码甚至没有引用密钥,但您的扩展方法却引用了。

【讨论】:

    【解决方案2】:

    我认为您正在尝试编写扩展方法来使用密钥加密密码。 所以你的函数头应该是:

    public static string ToEncrypt(this string passWord, string key)
    

    以后你可以像下面这样使用这个扩展:

    string encrpted = password.ToEncrypt("your key here");
    

    【讨论】:

      猜你喜欢
      • 2019-09-15
      • 1970-01-01
      • 2021-03-22
      • 2011-06-23
      • 2012-03-10
      • 2015-09-01
      • 2018-02-23
      • 1970-01-01
      相关资源
      最近更新 更多