不再回头 .net学习日记&资料

我再也不愿听你要求 我受够了你那些自私要求

  IT博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  39 随笔 :: 2 文章 :: 14 评论 :: 0 Trackbacks
Imports  System
Imports  System.IO
Imports  System.Text
Imports  System.Diagnostics
Imports  System.Security.Cryptography
Imports  System.Text.RegularExpressions

' 使用标准DES对称加密
'
skey 为长度为8的字符 如:12345678
'
此方法用64位加密
Public   Function  EncryptDes( ByVal  SourceStr  As   String As   String

    
' get encodekey string from web.config
     Dim  skey  As   String
    skey 
=  ConfigurationSettings.AppSettings( " EnCodeKey " )

    
' put the input string into the byte array
     Dim  des  As  DESCryptoServiceProvider  =   New  DESCryptoServiceProvider()
    
Dim  inputByteArray  As   Byte ()
    inputByteArray 
=  Encoding.Default.GetBytes(SourceStr)

    
' set encrypt object and skey
    des.Key  =  ASCIIEncoding.ASCII.GetBytes(skey)
    des.IV 
=  ASCIIEncoding.ASCII.GetBytes(skey)
    
Dim  ms  As  MemoryStream  =   New  MemoryStream()
    
Dim  cs  As  CryptoStream  =   New  CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write)
    
Dim  sw  As  StreamWriter  =   New  StreamWriter(cs)
    sw.Write(SourceStr)
    sw.Flush()
    cs.FlushFinalBlock()
    ms.Flush()
    
Return  Convert.ToBase64String(ms.GetBuffer(),  0 , ms.Length)

End Function


' 使用标准DES对称解密
Public   Function  DecryptDes( ByVal  SourceStr  As   String As   String

    
' get encodekey string from web.config
     Dim  sKey  As   String
    sKey 
=  ConfigurationSettings.AppSettings( " EnCodeKey " )

    
' put the input string into the byte array
     Dim  des  As  DESCryptoServiceProvider  =   New  DESCryptoServiceProvider()

    des.Key 
=  ASCIIEncoding.ASCII.GetBytes(sKey)
    des.IV 
=  ASCIIEncoding.ASCII.GetBytes(sKey)

    
Dim  buffer  As   Byte ()  =  Convert.FromBase64String(SourceStr)

    
Dim  ms  As  MemoryStream  =   New  MemoryStream(buffer)
    
Dim  cs  As  CryptoStream  =   New  CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Read)
    
Dim  sr  As  StreamReader  =   New  StreamReader(cs)
    
Return  sr.ReadToEnd()

End Function


使用的DES对称加密
posted on 2006-09-25 00:01 不再回头 阅读(260) 评论(0)  编辑 收藏 引用 所属分类: .Net 2.0
只有注册用户登录后才能发表评论。