当前位置: 移动技术网 > IT编程>开发语言>.net > 利用DPAPI技术,实现浏览器地址栏字符串的加密

利用DPAPI技术,实现浏览器地址栏字符串的加密

2019年04月19日  | 移动技术网IT编程  | 我要评论

新手卡365之家,林怀部,黑棒佳丽

前言:
   dpapi特别有用,因为它能够消除使用密码的应用程序所带来的密钥管理问题。虽然能确保数据安全,但您必须采取额外的步骤来确保密钥的安全。 dpapi 使用与 dpapi 函数的调用代码关联的用户帐户的密码,以便派生加密密钥。因此,是操作(而非应用程序)管理着密钥。
   dpapi能够与计算机存储或用户存储(需要一个已加载的用户配置文件)配合使用。dpapi 默认情况下用于用户存储,但您可以通过将 cryptprotect_local_machine 标志传递给 dpapi 函数来指定使用计算机存储。
这种用户配置文件方式提供了一个额外的安全层,因为它限制了哪些用户能访问机密内容。只有加密该数据的用户才能解密该数据。但是,当通过 asp.net web 应用程序使用 dpapi 时,使用用户配置文件需要您执行额外的开发工作,因为您需要采取明确的步骤来加载和卸载用户配置文件(asp.net 不会自动加载用户配置文件)。
   计算机存储方式更容易开发,因为它不需要管理用户配置文件。但是,除非使用一个附加的熵参数,否则并不安全,因为该计算机的任何用户都可以解密数据。(熵是一个设计用来使解密机密内容更为困难的随机值)。使用附加的熵参数出现的问题在于它必须由应用程序安全地存储起来,这带来了另一个密钥管理问题。
注意:
   如果您将 dpapi 和计算机存储一起使用,那么加密字符串仅适用于给定的计算机,因此您必须在每台计算机上生成加密数据。不要在场或群集中将加密数据从一台计算机复制到另一台计算机。
   如果将 dpapi 和用户存储一起使用,则可以用一个漫游的用户配置文件在任何一台计算机上解密数据。
   dpapi只在2k以上的系统上才有,win9x系列就不要想了.
________________________________________
以下利用dpapi技术来加密查询字符串。
第一步:站点添加对system.security.dll程序集的引用。
第二步:建立类,这里利用dpapi和十六进制数结合的方式对数据进行加密。
hexencoding.cs
 
using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.text;
using system.security.cryptography;

/// <summary>
/// hexencoding 的摘要说明
/// </summary>
public class hexencoding
{
    public  hexencoding()
    {
        //
        // todo: 在此处添加构造函数逻辑
        //
    }
    /// <summary>
    /// 将十六进行字节数组转换成字符串
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public static string getstring(byte[] data)
    {
        stringbuilder results = new stringbuilder();
        foreach (byte b in data)
        {
            results.append(b.tostring("x2"));
        }
        return results.tostring();
    }
    /// <summary>
    /// 将字符串转换为十六进行字节数组
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public static byte[] getbytes(string data)
    {
        byte[] results = new byte[data.length / 2];
        for (int i = 0; i < data.length; i += 2)
        {
            results[i / 2] = convert.tobyte(data.substring(i, 2), 16);
        }
        return results;
    }
}
 
encryptedquerystring.cs
 
using system;
using system.text;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.security.cryptography;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;

public class encryptedquerystring :
    system.collections.specialized.stringdictionary
{
    public encryptedquerystring()
    {
        // todo: 在此处添加构造函数逻辑
    }
    /// <summary>
    ///
   /// </summary>
    /// <param name="encrypteddata"></param>
    public encryptedquerystring(string encrypteddata)
    {
      
        byte[] rawdata = hexencoding.getbytes(encrypteddata);
        byte[] clearrawdata = protecteddata.unprotect(
                    rawdata, null, dataprotectionscope.localmachine);
        string stringdata = encoding.utf8.getstring(clearrawdata);
        int index;
        string[] splitteddata = stringdata.split(new char[] { '&' });
        foreach (string singledata in splitteddata)
        {
            index = singledata.indexof('=');
            base.add(
                httputility.urldecode(singledata.substring(0, index)),
                httputility.urldecode(singledata.substring(index + 1))
            );
        }
    }
    /// <summary>
    ///
   /// </summary>
    /// <returns></returns>
    public override string tostring()
    {
        stringbuilder content = new stringbuilder();
        foreach (string key in base.keys)
        {
            content.append(httputility.urlencode(key));
            content.append("=");
            content.append(httputility.urlencode(base[key]));
            content.append("&");
        }
        content.remove(content.length-1, 1);
        byte[] encrypteddata = protecteddata.protect(
                    encoding.utf8.getbytes(content.tostring()),
                    null, dataprotectionscope.localmachine);
        return hexencoding.getstring(encrypteddata);
    }
}
 
第三步:程序中使用类加密查询字符串
首先建立一个发送页面send.x,代码如下:
 
<%@ page language="c#" autoeventwireup="true"  codefile="send.aspx.cs" inherits="send" %>

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "">

<html xmlns="" >
<head runat="server">
    <title>发送页面</title>
</head>
<body>
    <form id="form1" runat="server">
    <p>
        <asp:label id="label1" runat="server" text="mydata:" width="85px"></asp:label>
        <asp:textbox id="mydata" runat="server"></asp:textbox><br />
        <asp:label id="label2" runat="server" text="mydatatwo:" width="85px"></asp:label>
        <asp:textbox id="mydatatwo" runat="server"></asp:textbox><br />
        <asp:button id="sendcommand" runat="server" onclick="mydata_click" text="send info" /></p>
    </form>
</body>
</html>
 
send.aspx.cs
 
using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;

public partial class send : system.web.ui.page
{
    protected void page_load(object sender, eventargs e)
    {

    }
    protected void mydata_click(object sender, eventargs e)
    {
        string strmydata = this.mydata.text;
        string strmydatatwo = this.mydatatwo.text;
        encryptedquerystring querystring = new encryptedquerystring();
        querystring.add("mydata", strmydata);
        querystring.add("mydatatwo", strmydatatwo);
        response.redirect("receive.aspx?data="+querystring.tostring());
    }
}
 
再建立一个接收页面代码receive.aspx
 
<%@ page language="c#" autoeventwireup="true" codefile="receive.aspx.cs" inherits="receive" %>

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "">

<html xmlns="" >
<head runat="server">
    <title>接收页面</title>
</head>
<body>
    <form id="form1" runat="server">
    <p>
        <asp:literal id="litflag" runat="server"></asp:literal></p>
    </form>
</body>
</html>
 
receive.aspx.cs
 
using system;
using system.data;
using system.configuration;
using system.collections;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;

public partial class receive : system.web.ui.page
{
    protected void page_load(object sender, eventargs e)
    {
        if (!page.ispostback)
        {
            string strmydata = "";
            string strmydatatwo = "";
            encryptedquerystring querystring = new encryptedquerystring(request.querystring["data"]);
            foreach (string key in querystring.keys)
            {
                strmydata = querystring["mydata"].tostring();
                strmydatatwo = querystring["mydatatwo"].tostring();
            }
            this.litflag.text = "mydata:" + strmydata + "---->>>mydatatwo:" + strmydatatwo;
        }

    }
}
 
 
________________________________________
效果图如下:(注意看地址栏的变化,由于比较懒所以只在本地测试了。)

  
------>>>>



 

 

摘自 爱智旮旯

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网