我有一个 ASP.Net MVC 项目,它在使用时运行良好HashAlgorithm,但我试图在 ASP.NET Core 2 中复制这个相同的项目,但出现以下错误:
System.PlatformNotSupportedException HResult=0x80131539 消息=此平台不支持操作。Source=System.Security.Cryptography.Primitives StackTrace:在 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;
}
有关如何修复此错误的任何建议?
慕哥9229398
哈士奇WWW
回首忆惘然
相关分类