欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#中Web.Config加密与解密的方法

程序员文章站 2023-12-18 09:09:28
web.config,其中一部分配置如下:复制代码 代码如下:     

web.config,其中一部分配置如下:

复制代码 代码如下:

  <appsettings>
    <add key="erictest" value="erictest"/>
    <add key="encrypt" value="encrypt value"/>
  <appsettings>

  <connectionstrings >
    <add name="encryptconnection" connectionstring="data source=.\sql2000;initial catalog=northwind;user id=sa;password=test"/>
    <add name="sqlexpress" connectionstring="data source=.\sqlexpress;initial catalog=mydb;user id=sa;password=test"
     providername="system.data.sqlclient" />
  <connectionstrings>
 

在加密前,先做一些准备工作。

首先引用使用空间

复制代码 代码如下:

using system.configuration;
using system.web.configuration;
//将加密方式定义一下。主要是为了使用方便。

        ///
        /// 加密方式
        ///
        public enum encrypttype
        {
            dataprotectionconfigurationprovider,
            rsaprotectedconfigurationprovider
        }
 


使用dpapi加密
复制代码 代码如下:

        ///
        /// 以dpapi方式加密config
        ///
        private void encryptwebconfigbydpapi()
        {
            configuration configuration = null;
            configurationsection connectionsection = null;

            //打开request所在路径网站的web.config文件
            configuration = webconfigurationmanager.openwebconfiguration(request.applicationpath);
            //取得web.config中connectionstrings设置区块
            connectionsection = configuration.getsection("connectionstrings");
            //未加密时
            if (!connectionsection.sectioninformation.isprotected)
            {
                connectionsection.sectioninformation.protectsection(encrypttype.dataprotectionconfigurationprovider.tostring());
                configuration.save();
            }
        }


加密前后的数据对比
复制代码 代码如下:

  <connectionstrings >
    <add name="encryptconnection" connectionstring="data source=.\sql2000;initial catalog=northwind;user id=sa;password=test"/>
    <add name="sqlexpress" connectionstring="data source=.\sqlexpress;initial catalog=mydb;user id=sa;password=test"
     providername="system.data.sqlclient" />
  <connectionstrings>

复制代码 代码如下:

  <connectionstrings configprotectionprovider="dataprotectionconfigurationprovider">
    <encrypteddata>
      <cipherdata>
        <ciphervalue>aqaaancmnd8bfderjhoawe/cl+sbaaaapcenenbvhu6c+bx4e8qcpaqaaaacaaaaaaaqzgaaaaeaacaaaadie56y0pgcokepovxmvmmyo3tmqi/2w89huiq0lejaegaaaaaogaaaaaiaacaaaacyufojntk1iprbv91mmp8aciullzvrhapwbavohvtxpkacaabp0/yot/b8ig/elnaxrdvcxpq6l8mcvovpl0hv4507vepjb6fyrom9c5ti6iif6jz8gqfnfqif4p27rlyvvvu/r9kpuwdsz0ikjpe47nt/q/qollqx6vhqve8yajj64drujh6wjs2xdzsc03co4u9og/ydjx9zkpjvmnw8cx5ffklymizhxwx+b1zftzmu0ca8lzu4slktbfme3jmma4kqc6egeddxd3z53qkbt3kiswt1lm5ulpeq8rfr7qrzuewqsgagluntjvcdwlpjwbzvzqaoho71/epqrphgvmnakk1/hrqwxr0umf9k6hnkcr0ndlfeclhcjcc4zr6qhhwdwt8fhpm2zg2yucekehqsrbiwtjzqg/ddyvplwqmedj82t1gm9u9xhdhunlpot1fxy7bgjjok9tw1mxbwixq7bbih0muwmvesd8azgdxoh0xefyy3fly2hjwszg4opg3qmz1/s0x6sbz1vjjl7rk7ftdg3pwmdfvcvklmmdzqtkm3sqplazwrjyi4ijbnial/ubxwmdxo515lws55ddkdnx5r8htgoecn+cw5qfw8xxrprsqkg6sgti1gf2kzezz5wjegn0hqus18xoepzcuualbzhqrnbswwn0/gfdadxfwdnxothdj+cqc3vslk5f02ptw9cfzwdn30afjipmtarnltpplvwap1yxtkmtyzjmv7iiiosmthfictjazo7fetc+ytoifu/wddpeszqb2mlrefnuk+cbkoszausfhtquwfhblv6jneq5a/pdoheksu0dn2pc6aeqog/yngb6bjzprfxssdfikdh6lfxdo4s5wjxjx7vqnquo4mmtkoucp6dgmoogzqbhodl3mbgkfqyjdvxv9+4aa9qolhbckdl5taaaaachj0uapao59pmmz7gj67ho1mxjg9ntuah/lg5xi+phdrzwcnrmjv2zruhz8ewigcmoig7nvibnbmcet4k8pxuw==ciphervalue>
      <cipherdata>
    <encrypteddata>
  <connectionstrings>
 

 

对使用dpapi加密的数据解密

复制代码 代码如下:

        ///
        /// 解密dpapi
        ///
        private void decryptwebconfigbydpapi()
        {
            configuration configuration = null;
            configurationsection connectionsection = null;

            //打开request所在路径网站的web.config文件
            configuration = webconfigurationmanager.openwebconfiguration(request.applicationpath);
            //取得web.config中connectionstrings设置区块
            connectionsection = configuration.getsection("connectionstrings");
            if (connectionsection.sectioninformation.isprotected)
            {
                connectionsection.sectioninformation.unprotectsection();
                configuration.save();
            }
        }
 


调用dpapi加密数据(无需解密)
复制代码 代码如下:

        ///
        /// 取得加密后的数据
        ///
        private void getencryptwebconfigbydpapi()
        {
            string cncryptconnection = webconfigurationmanager.connectionstrings["encryptconnection"].connectionstring;
            string sqlexpressconnection = webconfigurationmanager.connectionstrings["sqlexpress"].connectionstring;
        }

使用rsa加密
复制代码 代码如下:

        ///
        /// 以rsa方式加密config
        ///
        private void encryptwebconfigbyrsa()
        {
            configuration configuration = null;
            configurationsection connectionsection = null;

            //打开request所在路径网站的web.config文件
            configuration = webconfigurationmanager.openwebconfiguration(request.applicationpath);
            //取得web.config中connectionstrings设置区块
            connectionsection = configuration.getsection("appsettings");
            //未加密时
            if (!connectionsection.sectioninformation.isprotected)
            {
                connectionsection.sectioninformation.protectsection(encrypttype.rsaprotectedconfigurationprovider.tostring());
                configuration.save();
            }
        }


加密前后数据对比:
复制代码 代码如下:

    <appsettings>
    <add key="erictest" value="erictest"/>
    <add key="encrypt" value="encrypt value"/>
  <appsettings>

复制代码 代码如下:

  <appsettings configprotectionprovider="rsaprotectedconfigurationprovider">
    <encrypteddata type="http://www.w3.org/2001/04/xmlenc#element"
      xmlns="http://www.w3.org/2001/04/xmlenc#">
      <encryptionmethod algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
      <keyinfo xmlns="http://www.w3.org/2000/09/xmldsig#">
        <encryptedkey xmlns="http://www.w3.org/2001/04/xmlenc#">
          <encryptionmethod algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
          <keyinfo xmlns="http://www.w3.org/2000/09/xmldsig#">
            <keyname>rsa <keykeyname>
          <keyinfo>
          <cipherdata>
            <ciphervalue>cjikulw6qbtley5mj9bs1ropf1l3f4ulrzknd6zxn6xyg9o+b6hr52ijk1al9/+nsbseapfdkdgax/skljywgzhhhi9sbrdbj10djcsnuguwpi5zslc+qhdpv0z4ijtw83jmrdb9efcx7ag60qwl52ofeqli/ps1hsojlkpsv8m=ciphervalue>
          <cipherdata>
        <encryptedkey>
      <keyinfo>
      <cipherdata>
        <ciphervalue>y1aem/brwcwzxweule9mbaku8aui7cpelrjojgqefzaozxq7uejspqaxjydiymcf4egjkhe7py6wbrajrabbodxxeqhgj8i1+t554h8zosz2ino43h5x0zjcmvawxnbeq1rp9dnutcheyqrw70nnshf79w6e2fmuf1dovpwynwmlehjcp7zkzg==ciphervalue>
      <cipherdata>
    <encrypteddata>
  <appsettings>

解密rsa加密数据

复制代码 代码如下:

        ///
        /// 解密rsa
        ///
        private void decryptwebconfigbyrsa()
        {
            configuration configuration = null;
            configurationsection connectionsection = null;

            //打开request所在路径网站的web.config文件
            configuration = webconfigurationmanager.openwebconfiguration(request.applicationpath);
            //取得web.config中connectionstrings设置区块
            connectionsection = configuration.getsection("appsettings");
            if (connectionsection.sectioninformation.isprotected)
            {
                connectionsection.sectioninformation.unprotectsection();
                configuration.save();
            }
        }

调用使用rsa加密数据(无需解密)

复制代码 代码如下:

        ///
        /// 取得加密后的数据
        ///
        private void getencryptwebconfigbyrsa()
        {
            string cncryptconnection = webconfigurationmanager.appsettings["erictest"];
            string sqlexpressconnection = webconfigurationmanager.appsettings["encrypt"];
        }

上一篇:

下一篇: