zoukankan      html  css  js  c++  java
  • 安全加密

    两个文本框(用户名密码),一个提交按扭,点击按扭时进行加密及注册处理!
    cs如下:

    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    using System.Security.Cryptography;
    using System.Security;
    using System.IO;
    using System.Text;
    namespace Security.Formsauth
    {
        
    /// <summary>
        
    /// Reg 的摘要说明。
        
    /// </summary>

        public class Reg : System.Web.UI.Page
        
    {
            
    protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2;
            
    protected System.Web.UI.WebControls.TextBox tbPass;
            
    protected System.Web.UI.WebControls.Label Label2;
            
    protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
            
    protected System.Web.UI.WebControls.TextBox tbName;
            
    protected System.Web.UI.WebControls.Label Label1;
            
    protected System.Web.UI.WebControls.Label Label3;
            
    protected System.Web.UI.WebControls.Button btnReg;
        
            
    private void Page_Load(object sender, System.EventArgs e)
            
    {
                
    // 在此处放置用户代码以初始化页面
            }


            
    Web Form Designer generated code

            
    private void btnReg_Click(object sender, System.EventArgs e)
            
    {
                SqlConnection con 
    = new SqlConnection();
                con.ConnectionString 
    = System.Configuration.ConfigurationSettings.AppSettings["DSN"];
                con.Open();
            
                
    //以下得到hash和salt加密串
                const int salSize = 16;
                
    // step 1: create some entropy for use as the salt
                RandomNumberGenerator rng = RandomNumberGenerator.Create();
                
    byte[] salt = new byte[ salSize ];
                rng.GetBytes(salt);


                
    // step 2: turn the password into bytes
                byte[] secret = Encoding.Unicode.GetBytes(tbPass.Text);


                
    // step 3: create the hash
                HashAlgorithm hashAlg = SHA1.Create();
                
    using(CryptoStream cs = new CryptoStream(Stream.Null, hashAlg, CryptoStreamMode.Write)) 
                
    {
                    cs.Write(secret, 
    0, secret.Length);
                    cs.Write(salt, 
    0, salt.Length);
                    cs.FlushFinalBlock();
                }

                
    string strHash = Convert.ToBase64String(hashAlg.Hash);
                
    string strSalt = Convert.ToBase64String(salt);
                
    //
                string strSql = "insert into formsUserInfo values(@username,@hashPass,@saltPass)";
                SqlParameter sqlpUser 
    = new SqlParameter("@username",SqlDbType.NVarChar,64);
                sqlpUser.Value 
    = tbName.Text;
                SqlParameter sqlpPassHash 
    = new SqlParameter("@hashPass",SqlDbType.NVarChar,50);
                SqlParameter sqlpPassSalt 
    = new SqlParameter("@saltPass",SqlDbType.NVarChar,50);
                sqlpPassHash.Value 
    = strHash;
                sqlpPassSalt.Value 
    = strSalt;
                SqlCommand com 
    = new SqlCommand(strSql,con);
                com.Parameters.Add(sqlpUser);
                com.Parameters.Add(sqlpPassHash);
                com.Parameters.Add(sqlpPassSalt);
                com.ExecuteNonQuery();
                con.Close();
                Response.Write(
    "<script language='javascript'>alert('注册成功!')</script>");

            }

        }

    }

  • 相关阅读:
    (转)使用 PyInstaller 把python程序 .py转为 .exe 可执行程序
    (转)使用 python Matplotlib 库绘图
    使用Matplotlib画图系列(一)
    Numpy常用金融计算(一)
    Windows中安装Linux子系统的详细步骤
    Centos7+Postfix+Dovecot实现内网邮件收发 风行天下
    centos7系统防火墙端口转发 风行天下
    Centos7+Postfix+Dovecot实现内网邮件收发
    centos7的路径含有空格Linux命令使用时路径存在空格、特殊符号(如、@等等) 风行天下
    zabbix无法使用Detect operating system [zabbix] 风行天下
  • 原文地址:https://www.cnblogs.com/fuchifeng/p/627243.html
Copyright © 2011-2022 走看看