我有一个使用加盐哈希值加密密码的类。
但是如果我想将 null 传递给类,我会收到以下错误:Cannot implicitly convert type string to byte[]
这是类代码:
public class MyHash
{
public static string ComputeHash(string plainText,
string hashAlgorithm, byte[] saltBytes)
{
Hash Code
}
}
当我使用该类时出现错误:“无法将类型字符串隐式转换为 byte[]”
//Encrypt Password
byte[] NoHash = null;
byte[] encds = MyHash.ComputeHash(Password, "SHA256", NoHash);
最佳答案
这是因为您的“ComputeHash”方法返回一个字符串,而您正试图将此返回值分配给一个字节数组;
byte[] encds = MyHash.ComputeHash(Password, "SHA256", NoHash);
string 到 byte[] 没有隐式转换,因为存在许多不同的编码来将字符串表示为字节,例如 ASCII 或 UTF8。
您需要像这样使用适当的编码类显式转换字节;
string x = "somestring";
byte[] y = System.Text.Encoding.UTF8.GetBytes(x);
关于c# - 无法将类型字符串隐式转换为 byte[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10792315/