【发布时间】:2019-01-10 23:29:26
【问题描述】:
我有一个 ASP.Net MVC 项目,在使用 HashAlgorithm 时可以正常工作,但我试图在 ASP.NET Core 2 中复制同一个项目,但出现以下错误:
System.PlatformNotSupportedException HResult=0x80131539 Message=此平台不支持操作。 Source=System.Security.Cryptography.Primitives 堆栈跟踪: 在 System.Security.Cryptography.HashAlgorithm.Create(String hashName) 在 Hash.Program.EncodePassword(String pass, String salt)
我的代码:
public static string GeneratePassword(int saltlength) //length of salt
{
const string chars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
var randNum = new Random();
var passwordSalt = new char[saltlength];
for (var i = 0; i <= saltlength - 1; i++) {
passwordSalt[i] = chars[Convert.ToInt32((chars.Length) * randNum.NextDouble())];
}
return new string(passwordSalt);
}
public static string EncodePassword(string pass, string salt) //encrypt password
{
byte[] bytes = Encoding.Unicode.GetBytes(pass);
byte[] src = Encoding.Unicode.GetBytes(salt);
byte[] dst = new byte[src.Length + bytes.Length];
Buffer.BlockCopy(src, 0, dst, 0, src.Length);
Buffer.BlockCopy(bytes, 0, dst, src.Length, bytes.Length);
HashAlgorithm algorithm = HashAlgorithm.Create("MD5");
if (algorithm != null) {
byte[] inArray = algorithm.ComputeHash(dst);
var encodedPassword = Convert.ToBase64String(inArray);
return encodedPassword;
}
return pass;
}
关于如何修复此错误的任何建议?
【问题讨论】:
-
HashAlgorithm.Create 在 .NET Core 上不受支持,请参阅 github.com/dotnet/corefx/wiki/…。我猜你应该直接实例化它,例如通过使用MD5 class。话虽这么说,您不应该在当今时代使用 MD5,因为它已经有好几年不安全了。也不要只是散列你的密码,加盐并使用像 Rfc2898DeriveBytes 类这样的密钥派生函数。
-
如果你知道你在使用 MD5,为什么不使用
MD5.Create()? -
不要使用 MD5 来散列密码。永远!
-
谢谢大家帮助我。这是一个特例,我只需要一个临时解决方案。该系统位于安全防火墙后面,并且位于具有单向流量的 Intranet 上。
标签: c# asp.net-mvc asp.net-core .net-core