根据输入的password而生成salt和pwdHash
/// <summary> /// 根据pwd分别生成Salt和Pwd的值(新增插入时使用) /// </summary> /// <param name="pwd">明文的密码</param> /// <param name="salt">密文后的salt</param> /// <param name="pwdhash">密文后的pwd</param> public static void GetPwdhashAndSalt(string pwd, out string salt, out string pwdHash) { Rfc2898DeriveBytes db = new Rfc2898DeriveBytes(pwd, 32, 1000); salt = Convert.ToBase64String(db.Salt); pwdHash = Convert.ToBase64String(db.GetBytes(32)); }
根据salt和pwdHash来判断密码的正确性
// <summary> /// 根据salt和pwdHash来判断密码的正确性(验证时使用) /// </summary> /// <param name="pwd">明文的密码</param> /// <param name="salt">密文的salt</param> /// <returns>密文的salt</returns> public static string GetPwdhash(string pwd, string salt) { string pwdHash = ""; System.Security.Cryptography.Rfc2898DeriveBytes db; db = new System.Security.Cryptography.Rfc2898DeriveBytes(pwd, System.Convert.FromBase64String(salt), 1000); return pwdHash = System.Convert.ToBase64String(db.GetBytes(32)); }