下载类库 Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.dll
创新互联公司专注于曲水网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供曲水营销型网站建设,曲水网站制作、曲水网页设计、曲水网站官网定制、小程序制作服务,打造曲水网络公司原创品牌,更为您提供曲水网站排名全网营销落地服务。
Microsoft.Practices.EnterpriseLibrary.Security.dll
Microsoft.Practices.EnterpriseLibrary.Common
2. web.config 配置
3.添加引用 类库
4.类 方法
///
/// 哈希加密算法枚举
///
public enum EnumHashProvider
{
///
/// MD5
///
MD5CryptoServiceProvider = 1,
///
/// RIPEMD160Managed
///
RIPEMD160Managed,
///
/// SHA1
///
SHA1CryptoServiceProvider,
///
/// SHA1Managed
///
SHA1Managed,
///
/// SHA256Managed
///
SHA256Managed,
///
/// SHA384Managed
///
SHA384Managed,
///
/// SHA512Managed
///
SHA512Managed
}
///
/// 加密服务工具类
///
public class CryptographyUtil
{
///
/// 根据随机抽取的加密算法和随机产生的散列字符串对源字符串进行加密
///
/// 要加密的字符串
/// 散列字符串
/// 哈希加密算法提供器
///
public static string GetEncryptedHashText(string plainText, out string saltText, out int hashProvider)
{
Random radom = new Random();
int hashProviderType = radom.Next(1, 8);
hashProvider = hashProviderType;
saltText = CreateSaltText();
plainText += saltText;
string encryptedText = Cryptographer.CreateHash(((EnumHashProvider)hashProvider).ToString(), plainText);
return encryptedText;
}
///
/// 比较某一字符串是否是某一加密字符串的源字符串
///
/// 源字符串
/// 加密后的字符串
/// 加密用的散列值
/// 哈希加密算法提供器
///
public static bool CompareHashText(string plainText, string encryptedText, string saltText, int hashProviderType)
{
EnumHashProvider hashProvider = new EnumHashProvider();
try
{
hashProvider = (EnumHashProvider)hashProviderType;
}
catch
{
return false;
}
bool matched = Cryptographer.CompareHash(hashProvider.ToString(), plainText + saltText, encryptedText);
return matched;
}
///
/// 产生一个散列值
///
///
private static string CreateSaltText()
{
byte[] bytSalt = new byte[32];
RNGCryptoServiceProvider rng;
rng = new RNGCryptoServiceProvider();
rng.GetBytes(bytSalt);
return Convert.ToBase64String(bytSalt);
}
}
///
/// 用户名登录
///
/// 用户名
/// 用户密码
///
public tb_User UserLogin(string userLoginname, string userLogPassword)
{
tb_User dbclass = this.repository.UserLogin(userLoginname);
if (dbclass == null)
{
return null;
}
if (!CryptographyUtil.CompareHashText(userLogPassword, dbclass.User_Password, dbclass.User_PasswordSalt, dbclass.User_PasswordFormat.Value))
{
return null;
}
return dbclass;
}
string user_Password;
string user_PasswordSalt;
int user_PasswordFormat;
//密码加密
user_Password = CryptographyUtil.GetEncryptedHashText(password, out user_PasswordSalt, out user_PasswordFormat);
item.User_Password = user_Password;
item.User_PasswordFormat = user_PasswordFormat;
item.User_PasswordSalt = user_PasswordSalt;