WCF初见之Salt+Hash加密

简介:

最近要对密码进行Salt加密,故查看了相关资料,其实就是把需要加密的值先和随机的Salt值连接在一起,再进行加密(可以哈希,也可以MD5加密等等)。

下面是具体步骤:

1.先创建相关数据库:

复制代码
--创建数据库表Salt_Encryption
CREATE TABLE Salt_Encryption
(
Name             VARCHAR(15) PRIMARY KEY NOT NULL,        --用户名
[Password]         VARCHAR(50) NOT NULL,                    --密码
Salt             VARCHAR(10) NOT NULL                    --Salt值
)
复制代码

2.新建一个Salt_Encryption_WCF的WCF应用程序,因为要用到数据库,所以先创建一个名为SaltModel.edmx的实体类:

然后跟着步骤来就可以了

3.创建服务契约和创建服务:
(1)IService1.cs (创建服务契约):

 

复制代码
using System;
using System.Collections.Generic;
using System.ServiceModel;

namespace Salt_Encryption_WCF
{
    
    [ServiceContract]
    public interface IService1
    {
         /***********************************密码加密*********************************************************/
        // 创建一个随机的Salt值
        [OperationContract]
        string CreateSalt();
        //对Salt后的密码进行哈希
        [OperationContract]
        string CreatePasswordHash(string pwd, string strSalt);
        /*************************************数据库操作********************************************/
        // 新增数据
        [OperationContract]
        void insertSql(string strName, string strPwd, string Salt);
        // 查询数据
        [OperationContract]
        IEnumerable<Salt_Encryption> selectSql();
    }
}
复制代码

(2)Service1.svc (创建服务)

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Web.Security;
using System.Security.Cryptography;
namespace Salt_Encryption_WCF
{
    
    public class Service1 : IService1
    {
        /***********************************密码加密*********************************************************/
        private const int saltLenght = 4;  //定义Salt值的长度

        /// <summary>
        /// 创建一个随机的Salt值
        /// </summary>
        /// <returns>随机数的字符串</returns>
        public string CreateSalt()
        {
            //生成一个加密的随机数
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            byte[] buff  = new byte[saltLenght];
            rng.GetBytes(buff);
            //返回一个Base64随机数的字符串
            return Convert.ToBase64String(buff);
        }

        /// <summary>
        /// 对Salt后的密码进行哈希
        /// </summary>
        /// <param name="pwd">密码</param>
        /// <param name="strSalt">Salt值</param>
        /// <returns>返回加密好的密码</returns>
        public string CreatePasswordHash(string pwd,string strSalt)
        {
            //把密码和Salt连起来
            string saltAndPwd = String.Concat(pwd,strSalt);
            //对密码进行哈希
            string hashenPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(saltAndPwd,"sha1");
            //返回哈希后的值
            return hashenPwd;
        }
        /*************************************数据库操作********************************************/
        SaltEnEntities db = new SaltEnEntities();
        /// <summary>
        /// 新增数据
        /// </summary>
        /// <param name="strName">用户名</param>
        /// <param name="strPwd">密码</param>
        /// <param name="Salt">Salt值</param>
        public void insertSql(string strName,string strPwd,string strSalt)
        {
            Salt_Encryption s = new Salt_Encryption();
            s.Name = strName;
            s.Password = strPwd;
            s.Salt = strSalt;
            //添加数据
            db.Salt_Encryption.AddObject(s);
            //保存数据的改变
            db.SaveChanges();
        }


        /// <summary>
        /// 查询数据
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Salt_Encryption> selectSql()
        {
            IEnumerable<Salt_Encryption> sql = from info in db.Salt_Encryption
                                               select info;
            return sql;
        }
    }
}
复制代码

4.然后新建一个名为Test2Salt的Web客户端(用于测试),先引用创建的WCF服务,具体过程见WCF初见之HelloWorld,然后进行Web端的代码编写:
(1)Test2SaltForm.aspx(Web界面代码)

复制代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test2SaltForm.aspx.cs" Inherits="Test2Salt.Test2SaltForm" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="Name" runat="server" Text="用户名:" Height="20px" Width="80px"></asp:Label>
    <asp:TextBox ID="toName" runat="server" Height="20px" Width="120px" ></asp:TextBox>
    </div>
    <div>
    <asp:Label ID="Password" runat="server" Text="密码:" Height="20px" Width="80px"></asp:Label>
    <asp:TextBox ID="toPassword" runat="server" Height="20px" Width="120px" 
            TextMode="Password"></asp:TextBox>
    </div>
    <asp:Button ID="InsertData" runat="server" Text="插入数据" 
        onclick="InsertData_Click"/>
    <asp:GridView ID="gv" runat="server">
    </asp:GridView>
    </form>
</body>
</html>
复制代码

(2)Test2SaltForm.aspx.cs(功能实现代码)

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Test2Salt
{
    public partial class Test2SaltForm : System.Web.UI.Page
    {
        host.Service1Client host = new host.Service1Client();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) { DataPageBind(); }
        }

        private void DataPageBind()
        {
            //绑定数据,并显示数据
            gv.DataSource = host.selectSql();
            gv.DataBind();
        }

        protected void InsertData_Click(object sender, EventArgs e)
        {
            string strName = toName.Text.Trim();
            string strPwd = toPassword.Text.Trim();
            //得到Salt值
            string Salt = host.CreateSalt();
            //得到加密后的密码
            string Pwd = host.CreatePasswordHash(strPwd,Salt);
            host.insertSql(strName,Pwd,Salt);
            DataPageBind();
        }


    }
}
复制代码

5.效果图如下:

 

PS:如果想验证用户名和密码的话,只要获取数据库中的Salt值,对你输入的密码进行加密,然后和数据库中的密码进行对比就可以了。






本文转自叶超Luka博客园博客,原文链接:http://www.cnblogs.com/yc-755909659/archive/2012/06/26/2563134.html,如需转载请自行联系原作者
目录
相关文章
|
存储 安全 算法
2021年你还不会Shiro?----4.使用MD5+盐+hash散列进行密码加密
上一篇文章里介绍了使用自定义的Realm来实现数据的获取,不过,数据的获取依然不是来源于真实的数据库或者是nosql,只是包装了一个方法,假装从这个方法里获取了数据库中的用户信息,然后我们返回了一个SimpleAccount,这个对象携带了用户名与密码,当时我们是明文返回的密码。这样做很显然是不安全的,一旦数据来源被攻破,所有的用户信息都会被泄露。所以这里我们介绍下,常用的密码加密策略。
148 0
2021年你还不会Shiro?----4.使用MD5+盐+hash散列进行密码加密
|
9月前
|
前端开发
WCF更新服务引用报错的原因之一
WCF更新服务引用报错的原因之一
|
8月前
|
C# 数据安全/隐私保护
c#如何创建WCF服务到发布(SqlServer版已经验证)
c#如何创建WCF服务到发布(SqlServer版已经验证)
38 0
|
8月前
|
安全 数据库连接 数据库
WCF服务创建到发布(SqlServer版)
在本示例开始之前,让我们先来了解一下什么是wcf? wcf有哪些特点? wcf是一个面向服务编程的综合分层架构。该架构的项层为服务模型层。 使用户用最少的时间和精力建立自己的软件产品和外界通信的模型。它使得开发者能够建立一个跨平台的安全、可信赖、事务性的解决方案。且能与已有系统兼容写作。 简单概括就是:一组数据通信的应用程序开发接口。
57 0
Visual Studio 2022 创建 WCF服务 找不到
Visual Studio 2022 创建 WCF服务 找不到