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

时间:2023-03-09 17:58:18
Silverlight信息加密 - 通过Rfc2898DeriveBytes类使用基于HMACSHA1的伪随机数生成器实现PBKDF2

原文: 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地址暴露在外面

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

解决方法

在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