当前位置:编程学习 > asp >>

用ASP.NET加密Cookie数据

 一、.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,这是一个必须考虑的问题。再者,加密的数据越多或算法越复杂,就会占有更多的服务器资源,进而减慢整个站点的访问速度。
  
  二、创建一个简单的加密应用类
  
  .NET的所有加密和解密通过CryptoStream类别来处理,它衍生自System.IO.Stream,将字符串作为以资料流为基础的模型,供加密转换之用。下面是一个简单的加密应用类的代码:
  
  Imports System.Diagnostics
  
  Imports System.Security.Cryptography
  
  Imports System.Text
  
  Imports System.IO
  
  
  Public Class CryptoUtil
  
  
  随机选8个字节既为密钥也为初始向量
  
  Private Shared KEY_64() As Byte = {42, 16, 93, 156, 78, 4, 218, 32}
  
  Private Shared IV_64() As Byte = {55, 103, 246, 79, 36, 99, 167, 3}
  
  
  对TripleDES,采取24字节或192位的密钥和初始向量
  
  Private Shared KEY_192() As Byte = {42, 16, 93, 156, 78, 4, 218, 32, _
  
  15, 167, 44, 80, 26, 250, 155, 112, _
  
  2, 94, 11, 204, 119, 35, 184, 197}
  
  Private Shared IV_192() As Byte = {55, 103, 246, 79, 36, 99, 167, 3, _
  
  42, 5, 62, 83, 184, 7, 209, 13, _
  
  145, 23, 200, 58, 173, 10, 121, 222}
  
  
  标准的DES加密
  
  Public Shared Function Encrypt(ByVal value As String) As String
  
  If value <> "" Then
  
  Dim cryptoProvider As DESCryptoServiceProvider = _
  
  New DESCryptoServiceProvider()
  
  Dim ms As MemoryStream = New MemoryStream()
  
  Dim cs As CryptoStream = _
  
  New CryptoStream(ms, cryptoProvider.CreateEncryptor(KEY_64, IV_64), _
  
  CryptoStreamMode.Write)
  
  Dim sw As StreamWriter = New StreamWriter(cs)
  
  
  sw.Write(value)
  
  sw.Flush()
  
  cs.FlushFinalBlock()
  
  ms.Flush()
  
  
  再转换为一个字符串
  
  Return Convert.ToBase64String(ms.GetBuffer(), 0, ms.Length)
  
  End If
  
  End Function
  
  
  
  标准的DES解密
  
  Public Shared Function Decrypt(ByVal value As String) As String
  
  If value <> "" Then
  
  Dim cryptoProvider As DESCryptoServiceProvider = _
  
  New DESCryptoServiceProvider()
  
  
  从字符串转换为字节组
  
  Dim buffer As Byte() = Convert.FromBase64String(value)
  
  Dim ms As MemoryStream = New MemoryStream(buffer)
  
  Dim cs As CryptoStream = _
  
  New CryptoStream(ms, cryptoProvider.CreateDecryptor(KEY_64, IV_64), _
  
  CryptoStreamMode.Read)
  
  Dim sr As StreamReader = New StreamReader(cs)
  
  
  Return sr.ReadToEnd()
  
  End If
  
  End Function
  
  
  TRIPLE DES加密
  
  Public Shared Function EncryptTripleDES(ByVal value As String) As String
  
  If value <> "" Then
  
  Dim cryptoProvider As TripleDESCryptoServiceProvider = _
  
  New TripleDESCryptoServiceProvider()
  
  Dim ms As MemoryStream = New MemoryStream()
  
  Dim cs As CryptoStream = _
  
  New CryptoStream(ms, cryptoProvider.CreateEncryptor(KEY_192, IV_192), _
  
  CryptoStreamMode.Write)
  
  Dim sw As StreamWriter = New StreamWriter(cs)
  
  
  sw.Write(value)
  
  sw.Flush()
  
  cs.FlushFinalBlock()
  
  ms.Flush()
  
  
  再转换为一个字符串
  
  Return Convert.ToBase64String(ms.GetBuffer(), 0, ms.Length)
  
  End If
  
  End Function
  
  
  
  TRIPLE DES解密
  
  Public Shared Function DecryptTripleDES(ByVal value As String) As String
  
  If value <> "" Then
  
  Dim cryptoProvider As TripleDESCryptoServiceProvider = _
  
  New TripleDESCryptoServiceProvider()
  
  
  从字符串转换为字节组
  
  Dim buffer As Byte() = Convert.FromBase64String(value)
  
  Dim ms As MemoryStream = New MemoryStream(buffer)
  
  Dim cs As CryptoStream = _
  
  New CryptoStream(ms, cryptoProvider.CreateDecryptor(KEY_192, IV_192), _
  
  CryptoStreamMode.Read)
  
  Dim sr As StreamReader = New StreamReader(cs)
  
  
  Return sr.ReadToEnd()
  
  End If
  
  End Function
  
  
  End Class
  
  上面我们将一组字节初始化为密钥,并且使用的是数字常量,如果你在实际应用中也这样做,这些字节一定要在0和255之间,这是一个字节允许的范围值。
  
  三、创建一个Cookie的应用类
  
  下面我们就创建一个简单的类,来设置和获取Cookies。
  
  Public Class CookieUtil
  
  
  设置COOKIE *****************************************************
  
  
  SetTripleDESEncryptedCookie (只针对密钥和Cookie数据)
  
  Public Shared Sub SetTripleDESEncryptedCookie(ByVal key As String, _
  
  ByVal value As String)
  
  key = CryptoUtil.EncryptTripleDES(key)
  
  value = CryptoUtil.EncryptTripleDES(value)
  
  
  SetCookie(key, value)
  
  End Sub
  
  
  SetTripleDESEncryptedCookie (增加了Cookie数据的有效期参数)
  
  Public Shared Sub SetTripleDESEncryptedCookie(ByVal key As String, _
  
  ByVal value As String, ByVal expires A
本贴来自天极网群乐社区--http://q.yesky.com/group/review-17600756.html

 

补充:综合编程 , 安全编程 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,