Cookie確實在WEB應用方面為訪問者和編程者都提供了方便
如果你未曾留意你的機器裡有Cookie文件
所以奉勸大家不要將敏感的用戶數據存放在Cookie中
在以前的ASP版本中沒有加密的功能
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013111310104770.jpg)
一
簡單地說
目前有兩種加密方法
我們對Cookie數據的加密采取對稱加密法
·System
·System
·System
·System
下面將示范DES和TripleDES算法
二
Imports System
Imports System
Imports System
Imports System
Public Class CryptoUtil
Private Shared KEY_
Private Shared IV_
Private Shared KEY_
Private Shared IV_
Public Shared Function Encrypt(ByVal value As String) As String
If value <>
Dim cryptoProvider As DESCryptoServiceProvider = _
New DESCryptoServiceProvider()
Dim ms As MemoryStream = New MemoryStream()
Dim cs As CryptoStream = _
New CryptoStream(ms
CryptoStreamMode
Dim sw As StreamWriter = New StreamWriter(cs)
sw
sw
cs
ms
Return Convert
End If
End Function
Public Shared Function Decrypt(ByVal value As String) As String
If value <>
Dim cryptoProvider As DESCryptoServiceProvider = _
New DESCryptoServiceProvider()
Dim buffer As Byte() = Convert
Dim ms As MemoryStream = New MemoryStream(buffer)
Dim cs As CryptoStream = _
New CryptoStream(ms
CryptoStreamMode
Dim sr As StreamReader = New StreamReader(cs)
Return sr
End If
End Function
Public Shared Function EncryptTripleDES(ByVal value As String) As String
If value <>
Dim cryptoProvider As TripleDESCryptoServiceProvider = _
New TripleDESCryptoServiceProvider()
Dim ms As MemoryStream = New MemoryStream()
Dim cs As CryptoStream = _
New CryptoStream(ms
CryptoStreamMode
Dim sw As StreamWriter = New StreamWriter(cs)
sw
sw
cs
ms
Return Convert
End If
End Function
Public Shared Function DecryptTripleDES(ByVal value As String) As String
If value <>
Dim cryptoProvider As TripleDESCryptoServiceProvider = _
New TripleDESCryptoServiceProvider()
Dim buffer As Byte() = Convert
Dim ms As MemoryStream = New MemoryStream(buffer)
Dim cs As CryptoStream = _
New CryptoStream(ms
CryptoStreamMode
Dim sr As StreamReader = New StreamReader(cs)
Return sr
End If
End Function
End Class
上面我們將一組字節初始化為密鑰
三
下面我們就創建一個簡單的類
Public Class CookieUtil
Public Shared Sub SetTripleDESEncryptedCookie(ByVal key As String
ByVal value As String)
key = CryptoUtil
value = CryptoUtil
SetCookie(key
End Sub
Public Shared Sub SetTripleDESEncryptedCookie(ByVal key As String
ByVal value As String
From:http://tw.wingwit.com/Article/program/net/201311/12808.html