gpt4 book ai didi

c# - HashAlgorithm.Create 在 C# ASP.NET Core 2 中因 PlatformNotSupportedException 而失败

转载 作者:行者123 更新时间:2023-11-30 14:22:07 25 4
gpt4 key购买 nike

我有一个 ASP.Net MVC 项目,它在使用 HashAlgorithm 时运行良好,但我试图在 ASP.NET Core 2 中复制同一个项目,但出现以下错误:

System.PlatformNotSupportedException HResult=0x80131539Message=Operation is not supported on this platform.Source=System.Security.Cryptography.PrimitivesStackTrace:at System.Security.Cryptography.HashAlgorithm.Create(String hashName)at 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;
}

关于如何修复这个错误有什么建议吗?

最佳答案

有一个github issue对于这个问题,它提供了一种解决方法:

Workaround is to call (HashAlgorithm)CryptoConfig.CreateFromName(string), though calling CryptoConfig directly is generally discouraged.

关于c# - HashAlgorithm.Create 在 C# ASP.NET Core 2 中因 PlatformNotSupportedException 而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51666384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com