| 网站首页 | VB.Net实例分析 | .Net技术文摘 | 下载中心 | VB.Net书籍笔记 | .Net源码 | VBS脚本与组件调用 | Windows2008技术文摘 | 给我们留言 | 
最新公告:

  没有公告

您现在的位置: 乐博网 >> VB.Net实例分析 >> 加密压缩编程 >> 实例分析正文
最新推荐 更多内容
Rijndael加密的实例
Rijndael加密的实例
作者:Ilu    来源:乐博网整理     更新时间:2009-9-3
Rijndael加密的代码如下:
  1. Imports System.ComponentModel
  2. Imports System.Security.Cryptography
  3. Imports System.Text
  4. Imports System.IO
  5. '更多.net源代码和教程,请关注乐博网www.lob.cn
vb.net Code:
  1. #Region " Rijndael Encryption Algorithm "
  2.     Public Class RijndaelManagedEncryption
  3. #Region " Global Variables "
  4.         Private Shared encoding As New UTF8Encoding
  5.         Private Shared RijndaelAlg As New RijndaelManaged
  6.         Private Shared HashAlg As HashAlgorithm = New SHA256Managed
  7.         Enum RijndaelCryptographicAction
  8.             Encrypt = 0
  9.             Decrypt = 1
  10.         End Enum
  11. #End Region
  12.  
  13. #Region " Encryption/Decryption Events "
  14.         Public Overloads Shared Function EncryptDecrypt(ByVal text As String, ByVal key As String, _
  15.                                                         ByVal action As RijndaelCryptographicAction) As String
  16.             Dim textBytes As Byte() = encoding.GetBytes(text)
  17.             Dim keyBytes As Byte() = CheckKey(key)
  18.  
  19.             Return EncryptDecrypt(textBytes, keyBytes, action)
  20.         End Function
  21.  
  22.         Public Overloads Shared Function EncryptDecrypt(ByVal text As String, ByVal key As Byte(), _
  23.                                                         ByVal action As RijndaelCryptographicAction) As String
  24.             Dim textBytes As Byte() = encoding.GetBytes(text)
  25.             Dim keyBytes As Byte() = CheckKey(key)
  26.  
  27.             Return EncryptDecrypt(textBytes, keyBytes, action)
  28.         End Function
  29.  
  30.         Public Overloads Shared Function EncryptDecrypt(ByVal data As Byte(), ByVal key As String, _
  31.                                                         ByVal action As RijndaelCryptographicAction) As String
  32.             Dim keyBytes As Byte() = CheckKey(key)
  33.  
  34.             Return EncryptDecrypt(data, keyBytes, action)
  35.         End Function
  36.  
  37.         Public Overloads Shared Function EncryptDecrypt(ByVal data As Byte(), ByVal key As Byte(), _
  38.                                                         ByVal action As RijndaelCryptographicAction) As String
  39.             RijndaelAlg.BlockSize = 256
  40.  
  41.             If Not key.Length = 32 Then
  42.                 key = CheckKey(key)
  43.             End If
  44.  
  45.             Dim IV As Byte() = GenerateIV(key)
  46.             Try
  47.                 Select Case action
  48.                     Case CryptographicAction.Encrypt
  49.                         Using memStream As New MemoryStream
  50.                             Using cStream As New CryptoStream(memStream, _
  51.                                                        RijndaelAlg.CreateEncryptor(key, IV), _
  52.                                                        CryptoStreamMode.Write)
  53.                                 cStream.Write(data, 0, data.Length)
  54.                             End Using
  55.                             Return Convert.ToBase64String(memStream.ToArray)
  56.                         End Using
  57.  
  58.                     Case CryptographicAction.Decrypt
  59.                         Dim textBytes As Byte() = Convert.FromBase64String(encoding.GetString(data))
  60.                         Using memStream As New MemoryStream(textBytes)
  61.                             Dim decryptedData(textBytes.Length) As Byte
  62.                             Using cStream As New CryptoStream(memStream, _
  63.                                                        RijndaelAlg.CreateDecryptor(key, IV), _
  64.                                                        CryptoStreamMode.Read)
  65.                                 cStream.Read(decryptedData, 0, decryptedData.Length)
  66.                             End Using
  67.                             Return encoding.GetString(decryptedData)
  68.                         End Using
  69.  
  70.                     Case Else
  71.                         Return Nothing
  72.                 End Select
  73.  
  74.             Catch ex As CryptographicException
  75.                 Return "Error - Invalid Password"
  76.             End Try
  77.         End Function
  78. #End Region
  79.  
  80.         'Generate a byte array containing the generated hash from the given key.
  81. #Region " Key Check "
  82.         Private Overloads Shared Function CheckKey(ByVal key As String) As Byte()
  83.             Dim byteKey As Byte() = encoding.GetBytes(key)
  84.  
  85.             Return CheckKey(byteKey)
  86.         End Function
  87.  
  88.         Private Overloads Shared Function CheckKey(ByVal key As Byte()) As Byte()
  89.             Return HashAlg.ComputeHash(key)
  90.         End Function
  91. #End Region
  92.  
  93.         'Generate a byte array containing the generated IV from the given key.
  94. #Region " Generate IV "
  95.         Private Overloads Shared Function GenerateIV(ByVal key As String) As Byte()
  96.             Dim keyBytes As Byte() = encoding.GetBytes(key)
  97.  
  98.             Return GenerateIV(keyBytes)
  99.         End Function
  100.  
  101.         Private Overloads Shared Function GenerateIV(ByVal key As Byte()) As Byte()
  102.             Dim keyReverse As String = encoding.GetString(key).ToCharArray.Reverse.ToString
  103.             Dim NewKey As Byte() = encoding.GetBytes(keyReverse)
  104.  
  105.             Return HashAlg.ComputeHash(NewKey)
  106.         End Function
  107. #End Region
  108.     End Class
  109. #End Region

  • 上一篇:

  • 下一篇:
  • 【字体: 】【打印此文】【关闭窗口
      相关文章:(只显示最新16条)
    VB.Net获取随机密码的实例
    VB.Net用Salt哈希数据的实例
    用DPAPI加密和解密数据的实例
    储存加密的数据库连接字符串到注册表的实例
    Salt加密数据的.Net实例
    Rijndael加密解密的实例
    AES加密的实例
    SHA256 Hash加密的实例
    VB.Net对XML元素进行加密解密的实例
    VB.Net文件CRC32算法的实例
    VB.Net文件SHA1算法的实例
    Blowfish加密算法的实例
    Rijndael类的实例
    TripleDES加解密的实例
    VB.Net实现des加密算法的实例
    VB.Net实现RSA加密的实例

    | 设为首页 | 加入收藏 | 联系站长 | | 友情链接 | 版权申明 |
    乐博网欢迎各种媒体转载我们的原创作品[转载请注明出处];我们鼓励更多VB.Net开发者一起加入研究与探讨;如发现文章访问错误、内容错误或版权疑问、内容有违相关法律(如涉及政治、色情、反动或散布虚假有害信息)等情况,请及时向我们举报,我们将及时纠正!
    联系邮箱:Shiny#vip.qq.com (#替换为@) QQ交流群: 40797788 [闽ICP备05014267号]