系列(一):加解密字符串及配置文件(CSASPNETEncryptAndDecryptConfiguration)

All-In-One Code Framework相当于微软的一个半官方代码库,里面囊括了微软各种技术的示例代码。其中示例语言包括:C#C++F#VB等;技术类别包括:ASP.NETWinformWPFWCFWFSilverligterIEIISOfiiceWindows 7Windows PhoneAzure(云计算等)

这个系列中,主要是总结学习All-In-One Code Framework的心得,大多是使用原始代码,只是将自己对代码的理解和注释写道文章中。

 

这个示例主要是使用BCL中与安全相关的类库(System.Security)实现对字符串的加解密和配置文件的加解密

       1、 加解密字符串

     /// <summary>
        
/// 加密方法
        
/// </summary>
        private void RSAEncryption()
        {
            CspParameters param 
= new CspParameters();
            param.KeyContainerName 
= "MyKeyContainer";  //个人理解为【密钥】
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
            {
                
string plaintext = this.tbData.Text;  //获取需要加密的字符串
                byte[] plaindata = System.Text.Encoding.Default.GetBytes(plaintext); //将字符串转换为字节数组
                byte[] encryptdata = rsa.Encrypt(plaindata, false);    //加密内容和加密后的内容都必须是字节数组
                string encryptstring = Convert.ToBase64String(encryptdata);  //将加密后的字节数字转换为字符串
                this.tbEncryptData.Text = encryptstring;
            }
        }

        
/// <summary>
        
/// 解密方法
        
/// </summary>
        private void RSADecryption()
        {
            CspParameters param 
= new CspParameters();
            param.KeyContainerName 
= "MyKeyContainer";//个人理解为【密钥】
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
            {
                
byte[] encryptdata = Convert.FromBase64String(this.tbEncryptData.Text); //将需要解密的字符串转换为字节数组
                byte[] decryptdata = rsa.Decrypt(encryptdata, false);  //解密内容和解密后的内容都必须是字节数组
                string plaindata = System.Text.Encoding.Default.GetString(decryptdata); //将解密后字节数组转换为字符串
                this.tbDecryptData.Text = plaindata;
            }
        }

            2、加解密配置文件

         

 private const string provider = "RSAProtectedConfigurationProvider";  
//加密
 Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); //打开一个配置文件(Request.ApplicationPath为配置文件所在的目录)
            ConfigurationSection section = config.GetSection(sectionString);  //定位到配置文件的节(比如connectionStrings、appSettings等)
            if (section != null//如果该节存在
            {
                section.SectionInformation.ProtectSection(provider);   
//使用RSA Provider加密配置文件节
                config.Save(); //保存配置文件
                Response.Write("encrypt successed, please check the configuration file.");
            }
//解密
  Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); //打开一个配置文件(Request.ApplicationPath为配置文件所在的目录)
            ConfigurationSection section = config.GetSection(sectionString);//定位到配置文件的节(比如connectionStrings、appSettings等)
            if (section != null && section.SectionInformation.IsProtected)
            {
                section.SectionInformation.UnprotectSection();  
//解密
                config.Save();//保存配置文件
                Response.Write("decrypt success, please check the configuration file.");
            }

                  疑问:

                  一、在加解密配置文件的代码中并没有【密钥】,那解密时怎么对应匹配还原成原始配置文件呢?如果没有【密钥】,任何在一台机器上加密的配置文件,在另一台机器声都可以解密?

                      难道和机器码有关?请牛人解答,谢谢

posted @ 2011-03-12 14:50    阅读(833)  评论(0编辑  收藏  举报