如何在 C# 中编写具有多个参数的扩展方法?
How to write an extension method in C# with multiple parameters?
我写了这个扩展方法,但我只得到一个参数。
我的 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 中进行了搜索,但找不到解决我的问题的方法。
谢谢大家!抱歉我的英语不好
我认为您正在尝试编写一个扩展方法来使用密钥加密密码。
所以你的函数头应该是:
public static string ToEncrypt(this string passWord, string key)
稍后您可以像下面这样使用此扩展程序:
string encrpted = password.ToEncrypt("your key here");
你的问题是你的扩展方法是为它所作用的字符串编写的,而不是“密码”
所以你写的代码应该是
var key = “some key”;
var encryptedpass = key.ToEncrypt(password);
您的代码甚至没有引用密钥,但您的扩展方法有。
我写了这个扩展方法,但我只得到一个参数。
我的 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 中进行了搜索,但找不到解决我的问题的方法。
谢谢大家!抱歉我的英语不好
我认为您正在尝试编写一个扩展方法来使用密钥加密密码。 所以你的函数头应该是:
public static string ToEncrypt(this string passWord, string key)
稍后您可以像下面这样使用此扩展程序:
string encrpted = password.ToEncrypt("your key here");
你的问题是你的扩展方法是为它所作用的字符串编写的,而不是“密码”
所以你写的代码应该是
var key = “some key”;
var encryptedpass = key.ToEncrypt(password);
您的代码甚至没有引用密钥,但您的扩展方法有。