原文: http://blog.csdn.net/xuyue1987/article/details/6706600

在上一篇文章当中,介绍到了通过Silverlight获取web.config中的值,最后提到了加密的问题,因此首先对该安全问题做一个简单的描述。

问题描述

1. 下方是我的web.config文件,当中配置这一个媒体文件服务器的IP地址

  1. <?xml version="1.0"?>
  2. <!--
  3. For more information on how to configure your ASP.NET application, please visit
  4. http://go.microsoft.com/fwlink/?LinkId=169433
  5. -->
  6. <configuration>
  7. <connectionStrings>
  8. <add name="VideoFiles_ConnectionString" connectionString="127.0.0.1"/>
  9. </connectionStrings>
  10. <system.web>
  11. <compilation debug="true" targetFramework="4.0" />
  12. </system.web>
  13. </configuration>

2. 当在Silverlight程序中获取到该config中的值时,我们可以发现,通过查看页面的Source Code,是可以看到这个IP地址的,它是以明文的方式显示出来的,这样会引发安全性问题,我相信没有人愿意将自己的服务器IP地址暴露在外面

解决方法

在Page页面获取到web.config后,对该文件当中的内容进行加密,从而有效地解决了明文显示的问题,这样,别人就不能通过查看源代码的方式从前台获取你的服务器IP地址或者其他机密信息了,其实现方法如下:

1. 在Page页面后台的.cs文件中,加入机密算法,使用Rfc2898DeriveBytes 获取密码、salt 值和迭代次数,然后通过调用 GetBytes 方法生成密钥:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Collections.Specialized;
  8. using System.Text;
  9. using System.Configuration;
  10. using System.Security.Cryptography;
  11. using System.IO;
  12. namespace GetWebConfig.Web
  13. {
  14. public partial class HostPage : System.Web.UI.Page
  15. {
  16. protected void Page_Load(object sender, EventArgs e)
  17. {
  18. //Clear the Cache
  19. Response.Cache.SetCacheability(HttpCacheability.NoCache);
  20. WriteInitParams();
  21. }
  22. private void WriteInitParams()
  23. {
  24. string strConn = ConfigurationManager.ConnectionStrings["VideoFiles_ConnectionString"].ToString();
  25. StringBuilder stringBuilder = new StringBuilder();
  26. stringBuilder.Append("<param name=\"InitParams\" value=\"");
  27. stringBuilder.Append(Encrypt(strConn));
  28. stringBuilder.Append("\"/>");
  29. this.litInitParams.Text = stringBuilder.ToString();
  30. }
  31. /**/
  32. /// <summary>
  33. /// 加密数据
  34. /// </summary>
  35. /// <param name="input">加密前的字符串</param>
  36. /// <returns>加密后的字符串</returns>
  37. public static string Encrypt(string input)
  38. {
  39. // salt值
  40. string saltValue = "saltValue";
  41. // 密码值
  42. string pwdValue = "xuyue";
  43. byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(input);
  44. byte[] salt = System.Text.UTF8Encoding.UTF8.GetBytes(saltValue);
  45. // AesManaged - 高级加密标准(AES) 对称算法的管理类
  46. System.Security.Cryptography.AesManaged aes = new System.Security.Cryptography.AesManaged();
  47. // Rfc2898DeriveBytes - 通过使用基于 HMACSHA1 的伪随机数生成器,实现基于密码的密钥派生功能 (PBKDF2 - 一种基于密码的密钥派生函数)
  48. // 通过 密码 和 salt 派生密钥
  49. System.Security.Cryptography.Rfc2898DeriveBytes rfc = new System.Security.Cryptography.Rfc2898DeriveBytes(pwdValue, salt);
  50. /**/
  51. /*
  52. * AesManaged.BlockSize - 加密操作的块大小(单位:bit)
  53. * AesManaged.LegalBlockSizes - 对称算法支持的块大小(单位:bit)
  54. * AesManaged.KeySize - 对称算法的密钥大小(单位:bit)
  55. * AesManaged.LegalKeySizes - 对称算法支持的密钥大小(单位:bit)
  56. * AesManaged.Key - 对称算法的密钥
  57. * AesManaged.IV - 对称算法的密钥大小
  58. * Rfc2898DeriveBytes.GetBytes(int 需要生成的伪随机密钥字节数) - 生成密钥
  59. */
  60. aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
  61. aes.KeySize = aes.LegalKeySizes[0].MaxSize;
  62. aes.Key = rfc.GetBytes(aes.KeySize / 8);
  63. aes.IV = rfc.GetBytes(aes.BlockSize / 8);
  64. // 用当前的 Key 属性和初始化向量 IV 创建对称加密器对象
  65. System.Security.Cryptography.ICryptoTransform encryptTransform = aes.CreateEncryptor();
  66. // 加密后的输出流
  67. System.IO.MemoryStream encryptStream = new System.IO.MemoryStream();
  68. // 将加密后的目标流(encryptStream)与加密转换(encryptTransform)相连接
  69. System.Security.Cryptography.CryptoStream encryptor = new System.Security.Cryptography.CryptoStream
  70. (encryptStream, encryptTransform, System.Security.Cryptography.CryptoStreamMode.Write);
  71. // 将一个字节序列写入当前 CryptoStream (完成加密的过程)
  72. encryptor.Write(data, 0, data.Length);
  73. encryptor.Close();
  74. // 将加密后所得到的流转换成字节数组,再用Base64编码将其转换为字符串
  75. string encryptedString = Convert.ToBase64String(encryptStream.ToArray());
  76. return encryptedString;
  77. }
  78. }
  79. }

2. 之后,在Silverlight页面获取config数据的时候,通过解密算法进行解密,得到真正的IP地址信息:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Documents;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Animation;
  11. using System.Windows.Shapes;
  12. using System.Security.Cryptography;
  13. using System.IO;
  14. using System.Text;
  15. namespace GetWebConfig
  16. {
  17. public partial class MainPage : UserControl
  18. {
  19. private IDictionary<string, string> _config;
  20. public MainPage()
  21. {
  22. InitializeComponent();
  23. _config = (Application.Current as App).Configurations;
  24. if (_config.Count > 0)
  25. lblWebconfig.Content = "Web.config connectionString: " + Decrypt(_config.ElementAt(0).Key.ToString() + "==");
  26. }
  27. /// <summary>
  28. /// 解密数据
  29. /// </summary>
  30. /// <param name="input">加密后的字符串</param>
  31. /// <returns>加密前的字符串</returns>
  32. public string Decrypt(string input)
  33. {
  34. // salt值(与加密时设置的值一致)
  35. string saltValue = "saltValue";
  36. // 密码值(与加密时设置的值一致)
  37. string pwdValue = "xuyue";
  38. byte[] encryptBytes = Convert.FromBase64String(input);
  39. byte[] salt = Encoding.UTF8.GetBytes(saltValue);
  40. System.Security.Cryptography.AesManaged aes = new System.Security.Cryptography.AesManaged();
  41. System.Security.Cryptography.Rfc2898DeriveBytes rfc = new System.Security.Cryptography.Rfc2898DeriveBytes(pwdValue, salt);
  42. aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
  43. aes.KeySize = aes.LegalKeySizes[0].MaxSize;
  44. aes.Key = rfc.GetBytes(aes.KeySize / 8);
  45. aes.IV = rfc.GetBytes(aes.BlockSize / 8);
  46. // 用当前的 Key 属性和初始化向量 IV 创建对称解密器对象
  47. System.Security.Cryptography.ICryptoTransform decryptTransform = aes.CreateDecryptor();
  48. // 解密后的输出流
  49. MemoryStream decryptStream = new MemoryStream();
  50. // 将解密后的目标流(decryptStream)与解密转换(decryptTransform)相连接
  51. System.Security.Cryptography.CryptoStream decryptor = new System.Security.Cryptography.CryptoStream(
  52. decryptStream, decryptTransform, System.Security.Cryptography.CryptoStreamMode.Write);
  53. // 将一个字节序列写入当前 CryptoStream (完成解密的过程)
  54. decryptor.Write(encryptBytes, 0, encryptBytes.Length);
  55. decryptor.Close();
  56. // 将解密后所得到的流转换为字符串
  57. byte[] decryptBytes = decryptStream.ToArray();
  58. string decryptedString = UTF8Encoding.UTF8.GetString(decryptBytes, 0, decryptBytes.Length);
  59. return decryptedString;
  60. }
  61. }
  62. }

最终实现效果如下,这时候我们通过查看源文件看到的则是被加密后的信息,而不是127.0.0.1,只有在Silverlight进行解密之后,才能看到我们想要的数据。

Silverlight信息加密 - 通过Rfc2898DeriveBytes类使用基于HMACSHA1的伪随机数生成器实现PBKDF2的更多相关文章

  1. Atitit.prototype-base class-based  基于“类” vs 基于“原型”

    Atitit.prototype-base class-based  基于“类” vs 基于“原型” 1. 基于“类” vs 基于“原型”1 2.  对象的产生有两种基本方式.一种是以原型(proto ...

  2. 5.7 Liquibase:与具体数据库独立的追踪、管理和应用数据库Scheme变化的工具。-mybatis-generator将数据库表反向生成对应的实体类及基于mybatis的mapper接口和xml映射文件(类似代码生成器)

    一. liquibase 使用说明 功能概述:通过xml文件规范化维护数据库表结构及初始化数据. 1.配置不同环境下的数据库信息 (1)创建不同环境的数据库. (2)在resource/liquiba ...

  3. 【个人使用.Net类库】(3)Excel文件操作类(基于NPOI)

    Web开发工作中经常要根据业务的需要生成对应的报表.经常采用的方法如下: 将DataTable导出至Excel文件; 读取模板Excel文件; 修改模板Excel文件对应的内容. 因此,便想到封装一个 ...

  4. silverlight datagrid绑定匿名类

    原文 http://www.cnblogs.com/luweis/archive/2011/10/21/2220587.html 刚开始遇到的一个问题是这样的,我有一个datagrid,根据不同的条件 ...

  5. mybatis学习笔记(二)-- 使用mybatisUtil工具类体验基于xml和注解实现

    项目结构  基础入门可参考:mybatis学习笔记(一)-- 简单入门(附测试Demo详细过程) 开始体验 1.新建项目,新建类MybatisUtil.java,路径:src/util/Mybatis ...

  6. 1. 元信息:Meta类 2. 基于对象查询的sql优化 3. 自定义:Group_Concat() 4. ajax前后台交互

    一.元信息 ''' 1. 元信息 1. Model类可以通过元信息类设置索引和排序信息 2. 元信息是在Model类中定义一个Meta子类 class Meta: # 自定义表名 db_table = ...

  7. Json转换工具类(基于google的Gson和阿里的fastjson)

    在项目之中我们经常会涉及到字符串和各种对象的转换,为此特地整理了一下常用的转换方法 一.基于com.google.code.gson封装的json转换工具类 1. 在pom.xml文件里面引入gson ...

  8. 适用于app.config与web.config的ConfigUtil读写工具类 基于MongoDb官方C#驱动封装MongoDbCsharpHelper类(CRUD类) 基于ASP.NET WEB API实现分布式数据访问中间层(提供对数据库的CRUD) C# 实现AOP 的几种常见方式

    适用于app.config与web.config的ConfigUtil读写工具类   之前文章:<两种读写配置文件的方案(app.config与web.config通用)>,现在重新整理一 ...

  9. 基于类和基于函数的python多线程样例

    不断的练,加深记忆吧. #!/usr/bin/env python # -*- coding: utf-8 -*- import threading import time exitFlag = 0 ...

随机推荐

  1. Bring it on

    I am going to open a whole new English Blog here. Most blogs here would be computer technologies, in ...

  2. hdu 5396 Expression(区间dp)

    Problem Description Teacher Mai has n numbers a1,a2,⋯,anand n−1 operators("+", "-&quo ...

  3. Cannot find class in classpath 报错

    删除项目文件夹下的target文件夹里面内容,重新运行测试代码,报错 org.testng.TestNGException: Cannot find class in classpath: com.f ...

  4. React 入门最好的实例-TodoList

    React 的核心思想是:封装组件,各个组件维护自己的状态和 UI,当状态变更,自动重新渲染整个组件. 最近前端界闹的沸沸扬扬的技术当属react了,加上项目需要等等原因,自己也决定花些时间来好好认识 ...

  5. 初探HTML

    就在今天我抱着试一试的态度稍微看了下HTML5, 尝试着接触一点新知识, 虽然学的并不多, 但是还是异常的兴奋, 感觉有好多东西和之前的不一样了, 包括控件的创建和一些属性的设置等, 总之这些变化与改 ...

  6. 虚拟现实,增强现实,VR,AR

    现在的热点不止VR,还有AR和披着MR.HR.CR外衣的各种高级AR们,所以比较着一起说.以下知乎上一网友观点,放几条结论:1.近期(未来两三年)看,VR能火,AR尚待成熟: 2.VR设备中,插片式是 ...

  7. DEV LookUpEdit 使用方法

    public class field { public string Name { get; set; } public string Explain { get; set; } } List< ...

  8. JavaScript 函数方法 - toString()

    Function.prototype.toString() 返回函数代码的字符串形式. 描述 Function 对象覆盖了从 Object 继承来的 Object.prototype.toString ...

  9. 使用java对sql server进行增删改查

    import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import ...

  10. XML配置silverlight ,wcf 解析xml

    XML 代码: <?xml version="1.0" encoding="utf-8" ?><ChartSet  xmlns:xsi=&qu ...