惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
爱范儿
爱范儿
T
The Exploit Database - CXSecurity.com
量子位
T
Tailwind CSS Blog
T
Threatpost
The GitHub Blog
The GitHub Blog
AWS News Blog
AWS News Blog
云风的 BLOG
云风的 BLOG
K
Kaspersky official blog
P
Proofpoint News Feed
博客园 - 司徒正美
L
LangChain Blog
T
Threat Research - Cisco Blogs
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
S
Secure Thoughts
The Last Watchdog
The Last Watchdog
Spread Privacy
Spread Privacy
H
Hacker News: Front Page
T
Troy Hunt's Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
W
WeLiveSecurity
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Proofpoint News Feed
T
Tor Project blog
T
The Blog of Author Tim Ferriss
I
Intezer
P
Privacy & Cybersecurity Law Blog
美团技术团队
N
Netflix TechBlog - Medium
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost
Application and Cybersecurity Blog
Application and Cybersecurity Blog
G
Google Developers Blog
Attack and Defense Labs
Attack and Defense Labs
T
Tenable Blog
月光博客
月光博客
Stack Overflow Blog
Stack Overflow Blog
J
Java Code Geeks
腾讯CDC
Microsoft Security Blog
Microsoft Security Blog
A
About on SuperTechFans
Last Week in AI
Last Week in AI

博客园 - Kevin

换工作了,记录一下面试情况 打工的就是打工的! 超强的希捷硬盘 如何一次性取出每个类别下的前n条记录 在一个表里添加了一记录,怎样取得这条记录的id 发布程序到远程服务器 DNN所有表结构 vs2005调用js脚本方法总结 Using JavaScript to show the current time for the end user HtmlGenericControl操作meta标记 window2k3集成sp1光盘启动工具版 CodeSmith资源 华硕网站ajax应用分析-js脚本 Ajax程序设计入门 SQL Server端口更改后的数据库连接方式 windows2003server自带防火墙导致sql server2k无法访问,谁有好办法? 局域网访问access数据库 dreamweaver+asp+access+vss不该犯的错误 windows2k3server远程桌面连接,”终端服务器超出了最大允许连接数“的解决办法
web.config信息及RSA加密方式!
Kevin · 2006-01-06 · via 博客园 - Kevin

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("成功保存公匙和密匙!");