zoukankan      html  css  js  c++  java
  • web.config信息及RSA加密方式!

         
        我们都知道web.config可以保存连接字符串,我们在程序中也都是这么做的,web.config是XML,所以它有清晰的结构,是我们很容易可以读懂它,但是这也出现一个问题,我们数据库完全暴露给浏览该文件的人,这是我们所不希望的。我们可以使用一个简单有效的加密算法来加密这段连接字符,使直接浏览该文件的人不能清楚地看到这些信息。
         我们一般以下面的形式保存连接字符串:
         <appSettings>
         <add key="ConnectionString" value="server=localhost;database=mydb;pwd=sa;uid=sa;" />
         </appSettings>
         为了我们自己可以看得明白这些加密的字符我们需要一个额外的程序专门生成这些加密数据(这个额外的程序你可以写得很复杂但是为了说明问题,我们使用简单的base64编码数据,这其实不是加密数据,但是原理是一样的)。下面用来生成明文到密文的(base64)转换,如果使用其它的加密算法可以替换这个加密过程
        
         byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes(this.textBox1.Text);
         string encrystr = Convert.ToBase64String(data);
        
         encrystr就是变码以后的字符。我们可以将我们web.config里面的连接字符("server=localhost;database=mydb;pwd=sa;uid=sa;")取出来放在这个程序里面执行生成一个新的字符串(c2VydmVyPWxvY2FsaG9zdDtkYXRhYmFzZT10ZXN0O3B3ZD1zYTt1aWQ9c2E71)。
        之后我们用这个字符替换未编码的字符串。如下所示:
         <appSettings>
         <add key="ConnectionString" value="c2VydmVyPWxvY2FsaG9zdDtkYXRhYmFzZT10ZXN0O3B3ZD1zYTt1aWQ9c2E7" />
         </appSettings>
         同样在我们的程序里面也需要解密,最后就是在程序中如何使用了,我们的程序需要理解这个字符串的意义,我们在数据访问层里面添加如下的解密方法
         string strconn = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
         byte[] data = Convert.FromBase64String(strconn);
         string strReal = System.Text.ASCIIEncoding.ASCII.GetString(data);
        
        这样就可以得到真实的连接字符串了。
         上文一个简单的加密(伪加密,其实本文实现的是一个编码而非加密)的方法,也许可以骗过一些人的眼睛,但是对于了解内幕的人还是起不到什么保护的作用,所以你可以扩展这个算法,使用对称或者非对称的加密算法替换该案例里面的编码算法,这样基本上就万无一失了。
        
        
        下面介绍如何使用非对称RSA加密的方法:
        
        加密:
        
        <%@ Page language="c#" AutoEventWireup="false"%>
        <%@ Import Namespace= "System.Security.Cryptography" %>
        <%@ Import Namespace= "System.IO" %>
        <%@ Import Namespace= "System.Text" %>
        
        <%
        string word = Request.Params["word"];
        if(word == null){
         Response.Write("没有输入密码啦!!!");
         return;
        }
        
        StreamReader sr = new StreamReader(@"f:\a.txt",UTF8Encoding.UTF8);
        string readpublickey = sr.ReadToEnd();
        sr.Close();
        
        RSACryptoServiceProvider crypt=new RSACryptoServiceProvider();
        UTF8Encoding enc=new UTF8Encoding();
        byte[] bytes=enc.GetBytes(word);
        crypt.FromXmlString( readpublickey );
        bytes = crypt.Encrypt( bytes,false );
        string encryttext=Convert.ToBase64String(bytes);
        string abb = Server.UrlEncode(encryttext);
        Response.Write(abb);
        
        Response.Write("<a href='take.aspx?word=" + encryttext + "'>" + encryttext + @"</a>");
        %>
        
        解密:
        
        <%@ Page language="c#" AutoEventWireup="false"%>
        <%@ Import Namespace= "System.Security.Cryptography" %>
        <%@ Import Namespace= "System.IO" %>
        <%@ Import Namespace= "System.Text" %>
        
        <%
        string word = Request.QueryString["word"];
        if(word == null){
         Response.Write("没有传来密码啦!!!");
         return;
        }
        
        StreamReader sr = new StreamReader(@"f:\b.txt",UTF8Encoding.UTF8);
        string readprivatekey = sr.ReadToEnd();
        sr.Close();
        
        Response.Write("<br>" + word);
        
        RSACryptoServiceProvider crypt=new RSACryptoServiceProvider();
        UTF8Encoding enc=new UTF8Encoding();
        byte[] bytes = Convert.FromBase64String(@word);
        crypt.FromXmlString ( readprivatekey ) ;
        byte[] decryptbyte = crypt.Decrypt( bytes,false );
        string decrypttext=enc.GetString( decryptbyte );
        
        Response.Write(decrypttext);
        
        %>
        
        公匙在a.txt文件中,私匙在b.txt文件中.
        
        制造公匙和私匙的方法如下:
        
        crypt=new RSACryptoServiceProvider();
        publickey=crypt.ToXmlString(false);//公匙
        privatekey=crypt.ToXmlString(true);//私匙
        crypt.Clear();
        
        //写入文本文件中
        StreamWriter one=new StreamWriter(@"f:\a.txt",true,UTF8Encoding.UTF8);
        one.Write(publickey);
        StreamWriter two=new StreamWriter(@"f:\b.txt",true,UTF8Encoding.UTF8);
        two.Write(privatekey);
        one.Flush();
        two.Flush();
        one.Close();
        two.Close();
        MessageBox.Show("成功保存公匙和密匙!");
  • 相关阅读:
    Get distinct count of rows in the DataSet
    单引号双引号的html转义符
    PETS Public English Test System
    Code 39 basics (39条形码原理)
    Index was outside the bounds of the array ,LocalReport.Render
    Thread was being aborted Errors
    Reportviewer Error: ASP.NET session has expired
    ReportDataSource 值不在预期的范围内
    .NET/FCL 2.0在Serialization方面的增强
    Perl像C一样强大,像awk、sed等脚本描述语言一样方便。
  • 原文地址:https://www.cnblogs.com/EasonWu/p/881382.html
Copyright © 2011-2022 走看看