问题描述
我正在编写一个具有注册和所有功能的身份验证系统。密码用盐散列。我遇到了一个问题,其中我正在使用的实现使用的是salt,而最后的哈希是字节数组。我无法将其转换为可以在sql中存储的常规人类可读字符串,并且在打印到控制台时,我已经尝试了Encoding类中的几乎所有Encodings。
任何帮助将不胜感激。我只是想念一些很明显的东西吗?
下面的代码示例:
class Program
{
static void Main(string[] args)
{
//The salt and hash sizes are 16 bytes (128 bits)
int saltSize = 16;
int hashSize = 16;
int iterations = 100000;
RNGCryptoServiceProvider randomness = new RNGCryptoServiceProvider();
byte[] salt = new byte[saltSize];
randomness.GetBytes(salt);
string password = "Mypassissecure7";
byte[] passBytes = Encoding.Default.GetBytes(password);
Rfc2898DeriveBytes pbkdf2 = new Rfc2898DeriveBytes(passBytes,salt,iterations,HashAlgorithmName.SHA512);
byte[] hash = pbkdf2.GetBytes(hashSize);
}
}
解决方法
为方便起见:
var base64String = System.Convert.ToBase64String(hash);
或
var hexString = BitConverter.ToString(hash);