【轉】用ASP.NET加密Cookie數據

來源:http://www.cnblogs.com/taizhouxiaoba/archive/2009/02/05/1384772.htmlhtml

Cookie中的數據以文本的形式存在客戶端計算機,考慮它的安全性,最好在將數據存入Cookie以前對其進行加密。
加密的方法不少,比較簡單一點的有:Base64,md5,sha等,而相對比較複雜一點的有:DES,TripleDES,RC2,Rijndael等。
下面是的代碼實現了將數據存入Cookie以前採用散列的算法進行加密. 算法

1 Private   void   Login_Click(object   sender,System   EventArgs   e) 
2 { 
3     string   Name   =   NameBox.Text; 
4     string   Pass   =   PassBox.Text; 
5     Response.Cookies["name"].Value   =   FormsAuthentication.HashPasswordForStoringInConfigFile(Name,   "md5"); 
6     Response.Cookies["pass"].Value   =   FormsAuthentication.HashPasswordForStoringInConfigFile(Pass,   "md5"); 
7 } 

加密的方法不少,使用比較複雜的加密算法,安全性比較高些,但佔用服務器資源比較大,會減慢整個網站的訪問速度。
因此對Cookie加密在考慮三個方面:1:安全性,2:Cookie容量,3:整個網站的性能編程

 

 

來源:http://bbs.csdn.net/topics/30038595瀏覽器

Cookie確實在WEB應用方面爲訪問者和編程者都提供了方便,然而從安全方面考慮是有問題的,首先,Cookie數據包含在HTTP請求和響應的包頭裏透明地傳遞,也就是說聰明的人是能清清楚楚看到這些數據的 。其次,Cookie數據以Cookie文件格式存儲在瀏覽者計算機的cache目錄裏,其中就包含有關網頁、密碼和其餘用戶行爲的信息,那麼只要進入硬盤就能打開Cookie文件。圖1是一個Cookie文件的內容:

若是你不曾留意你的機器裏有Cookie文件,能夠按下列方法查看:打開IE,選擇「工具」菜單裏的「Internet選項」,而後在彈出的對話框裏點擊「設置」按鈕,在設置對話框裏點擊「查看」鈕,就會打開一個窗口顯示瀏覽器放在硬盤裏的全部緩存數據,其中就有大量的Cookie文件。

因此奉勸你們不要將敏感的用戶數據存放在Cookie中,要麼就經過加密將這些數據保護起來。

在之前的ASP版本中沒有加密的功能,如今.NET構架在System.Security.Cryptography命名空間裏提供了許多加密類能夠利用。
1、.NET的密碼系統概要

簡單地說,加密就是將原始字符(字節)串轉變爲徹底不一樣的字符串的處理過程,達到原始字符沒法破譯的目的。這個處理過程是用另外一個字符串(稱爲「密鑰」),採起復雜的、混合的算法,「搗進」原始字符串。有時還使用一個稱爲「初始向量」的字符串,在密鑰搗進以前先打亂目標字符串,預防目標字符串中較明顯的內容被識破。加密的功效取決於所用密鑰的大小,密鑰越長,保密性越強。典型的密鑰長度有64位、128位、192位、256位和512位。攻擊者惟一的方法是建立一個程序嘗試每個可能的密鑰組合,但64位密鑰也有72,057,594,037,927,936種組合。

目前有兩種加密方法:對稱加密(或稱私有密鑰)和非對稱加密(或稱公共密鑰)。對稱加密技術的數據交換兩邊(即加密方和解密方)必須使用一個保密的私有密鑰。非對稱加密技術中,解密方向加密方要求一個公共密鑰,加密方在創建一個公共密鑰給解密方後,用公共密鑰建立惟一的私有密鑰。加密方用私有密鑰加密送出的信息,對方用公共密鑰解密。保護HTTP傳輸安全的SSL就是使用非對稱技術。

咱們對Cookie數據的加密採起對稱加密法。.NET構架從基本的SymmetricAlgorithm類擴展出來四種算法:

·System.Security.Cryptography.DES

·System.Security.Cryptography.TripleDES

·System.Security.Cryptography.RC2

·System.Security.Cryptography.Rijndael

下面將示範DES和TripleDES算法。DES的密鑰大小限制在64位,但用於Cookie的加密是有效的。TripleDES完成了三次加密,並有一個較大的密鑰位數,因此它更安全。使用那一種算法不只要考慮加密強度,還要考慮Cookie的大小。由於加密後的Cookie數據將變大,而且,密鑰越大,加密後的數據就越大,然而Cookie數據的大小限制在4KB,這是一個必須考慮的問題。再者,加密的數據越多或算法越複雜,就會佔有更多的服務器資源,進而減慢整個站點的訪問速度。

2、建立一個簡單的加密應用類

.NET的全部加密和解密經過CryptoStream類別來處理,它衍生自System.IO.Stream,將字符串做爲以資料流爲基礎的模型,供加密轉換之用。下面是一個簡單的加密應用類的代碼:緩存

  1 Imports System.Diagnostics
  2  
  3 Imports System.Security.Cryptography
  4  
  5 Imports System.Text
  6  
  7 Imports System.IO
  8  
  9 
 10 Public Class CryptoUtil
 11  
 12 
 13 '隨機選8個字節既爲密鑰也爲初始向量
 14  
 15 Private Shared KEY_64() As Byte = {42, 16, 93, 156, 78, 4, 218, 32}
 16  
 17 Private Shared IV_64() As Byte = {55, 103, 246, 79, 36, 99, 167, 3}
 18  
 19 
 20 '對TripleDES,採起24字節或192位的密鑰和初始向量
 21  
 22 Private Shared KEY_192() As Byte = {42, 16, 93, 156, 78, 4, 218, 32, _
 23  
 24 15, 167, 44, 80, 26, 250, 155, 112, _
 25  
 26 2, 94, 11, 204, 119, 35, 184, 197}
 27  
 28 Private Shared IV_192() As Byte = {55, 103, 246, 79, 36, 99, 167, 3, _
 29  
 30 42, 5, 62, 83, 184, 7, 209, 13, _
 31  
 32 145, 23, 200, 58, 173, 10, 121, 222}
 33  
 34 
 35 '標準的DES加密
 36  
 37 Public Shared Function Encrypt(ByVal value As String) As String
 38  
 39 If value <> "" Then
 40  
 41 Dim cryptoProvider As DESCryptoServiceProvider = _
 42  
 43 New DESCryptoServiceProvider()
 44  
 45 Dim ms As MemoryStream = New MemoryStream()
 46  
 47 Dim cs As CryptoStream = _
 48  
 49 New CryptoStream(ms, cryptoProvider.CreateEncryptor(KEY_64, IV_64), _
 50  
 51 CryptoStreamMode.Write)
 52  
 53 Dim sw As StreamWriter = New StreamWriter(cs)
 54  
 55 
 56 sw.Write(value)
 57  
 58 sw.Flush()
 59  
 60 cs.FlushFinalBlock()
 61  
 62 ms.Flush()
 63  
 64 
 65 '再轉換爲一個字符串
 66  
 67 Return Convert.ToBase64String(ms.GetBuffer(), 0, ms.Length)
 68  
 69 End If
 70  
 71 End Function
 72  
 73 
 74 
 75 '標準的DES解密
 76  
 77 Public Shared Function Decrypt(ByVal value As String) As String
 78  
 79 If value <> "" Then
 80  
 81 Dim cryptoProvider As DESCryptoServiceProvider = _
 82  
 83 New DESCryptoServiceProvider()
 84  
 85 
 86 '從字符串轉換爲字節組
 87  
 88 Dim buffer As Byte() = Convert.FromBase64String(value)
 89  
 90 Dim ms As MemoryStream = New MemoryStream(buffer)
 91  
 92 Dim cs As CryptoStream = _
 93  
 94 New CryptoStream(ms, cryptoProvider.CreateDecryptor(KEY_64, IV_64), _
 95  
 96 CryptoStreamMode.Read)
 97  
 98 Dim sr As StreamReader = New StreamReader(cs)
 99  
100 
101 Return sr.ReadToEnd()
102  
103 End If
104  
105 End Function
106  
107 
108 'TRIPLE DES加密
109  
110 Public Shared Function EncryptTripleDES(ByVal value As String) As String
111  
112 If value <> "" Then
113  
114 Dim cryptoProvider As TripleDESCryptoServiceProvider = _
115  
116 New TripleDESCryptoServiceProvider()
117  
118 Dim ms As MemoryStream = New MemoryStream()
119  
120 Dim cs As CryptoStream = _
121  
122 New CryptoStream(ms, cryptoProvider.CreateEncryptor(KEY_192, IV_192), _
123  
124 CryptoStreamMode.Write)
125  
126 Dim sw As StreamWriter = New StreamWriter(cs)
127  
128 
129 sw.Write(value)
130  
131 sw.Flush()
132  
133 cs.FlushFinalBlock()
134  
135 ms.Flush()
136  
137 
138 '再轉換爲一個字符串
139  
140 Return Convert.ToBase64String(ms.GetBuffer(), 0, ms.Length)
141  
142 End If
143  
144 End Function
145  
146 
147 
148 'TRIPLE DES解密
149  
150 Public Shared Function DecryptTripleDES(ByVal value As String) As String
151  
152 If value <> "" Then
153  
154 Dim cryptoProvider As TripleDESCryptoServiceProvider = _
155  
156 New TripleDESCryptoServiceProvider()
157  
158 
159 '從字符串轉換爲字節組
160  
161 Dim buffer As Byte() = Convert.FromBase64String(value)
162  
163 Dim ms As MemoryStream = New MemoryStream(buffer)
164  
165 Dim cs As CryptoStream = _
166  
167 New CryptoStream(ms, cryptoProvider.CreateDecryptor(KEY_192, IV_192), _
168  
169 CryptoStreamMode.Read)
170  
171 Dim sr As StreamReader = New StreamReader(cs)
172  
173 
174 Return sr.ReadToEnd()
175  
176 End If
177  
178 End Function
179  
180 
181 End Class
182  

上面咱們將一組字節初始化爲密鑰,而且使用的是數字常量,若是你在實際應用中也這樣作,這些字節必定要在0和255之間,這是一個字節容許的範圍值。

3、建立一個Cookie的應用類

下面咱們就建立一個簡單的類,來設置和獲取Cookies。安全

  1 Public Class CookieUtil
  2  
  3 
  4 '設置COOKIE *****************************************************
  5  
  6 
  7 'SetTripleDESEncryptedCookie (只針對密鑰和Cookie數據)
  8  
  9 Public Shared Sub SetTripleDESEncryptedCookie(ByVal key As String, _
 10  
 11 ByVal value As String)
 12  
 13 key = CryptoUtil.EncryptTripleDES(key)
 14  
 15 value = CryptoUtil.EncryptTripleDES(value)
 16  
 17 
 18 SetCookie(key, value)
 19  
 20 End Sub
 21  
 22 
 23 'SetTripleDESEncryptedCookie (增長了Cookie數據的有效期參數)
 24  
 25 Public Shared Sub SetTripleDESEncryptedCookie(ByVal key As String, _
 26  
 27 ByVal value As String, ByVal expires As Date)
 28  
 29 key = CryptoUtil.EncryptTripleDES(key)
 30  
 31 value = CryptoUtil.EncryptTripleDES(value)
 32  
 33 
 34 SetCookie(key, value, expires)
 35  
 36 End Sub
 37  
 38 
 39 
 40 'SetEncryptedCookie(只針對密鑰和Cookie數據)
 41  
 42 Public Shared Sub SetEncryptedCookie(ByVal key As String, _
 43  
 44 ByVal value As String)
 45  
 46 key = CryptoUtil.Encrypt(key)
 47  
 48 value = CryptoUtil.Encrypt(value)
 49  
 50 
 51 SetCookie(key, value)
 52  
 53 End Sub
 54  
 55 
 56 'SetEncryptedCookie (增長了Cookie數據的有效期參數)
 57  
 58 Public Shared Sub SetEncryptedCookie(ByVal key As String, _
 59  
 60 ByVal value As String, ByVal expires As Date)
 61  
 62 key = CryptoUtil.Encrypt(key)
 63  
 64 value = CryptoUtil.Encrypt(value)
 65  
 66 
 67 SetCookie(key, value, expires)
 68  
 69 End Sub
 70  
 71 
 72 
 73 'SetCookie (只針對密鑰和Cookie數據)
 74  
 75 Public Shared Sub SetCookie(ByVal key As String, ByVal value As String)
 76  
 77 '編碼部分
 78  
 79 key = HttpContext.Current.Server.UrlEncode(key)
 80  
 81 value = HttpContext.Current.Server.UrlEncode(value)
 82  
 83 
 84 Dim cookie As HttpCookie
 85  
 86 cookie = New HttpCookie(key, value)
 87  
 88 SetCookie(cookie)
 89  
 90 End Sub
 91  
 92 
 93 'SetCookie(增長了Cookie數據的有效期參數)
 94  
 95 Public Shared Sub SetCookie(ByVal key As String, _
 96  
 97 ByVal value As String, ByVal expires As Date)
 98  
 99 '編碼部分
100  
101 key = HttpContext.Current.Server.UrlEncode(key)
102  
103 value = HttpContext.Current.Server.UrlEncode(value)
104  
105 
106 Dim cookie As HttpCookie
107  
108 cookie = New HttpCookie(key, value)
109  
110 cookie.Expires = expires
111  
112 SetCookie(cookie)
113  
114 End Sub
相關文章
相關標籤/搜索