VBNet.Su - Visual Basic .NET. ASP .NET. Framework .NET.
Список форумов
Windows Forms

Кодировать текст

RE: Кодировать текст

Быстрый переход:
RE: Кодировать текст24 января 2009 / 08:17
Ігор
Наш человек
ку 47.181654625+

вопросов: 8
советов: 9
ответов: 14
комментариев: 45


  1. Imports System.Security.Cryptography
  2. Imports System.IO
  3.  
  4. Public Module Encryptorrr
  5.  
  6. Private encv() As Byte = {64, 122, 10, 56, 20, _
  7.  74, 121, 39, 19, 91, 145, 78, 189, 211, 133, 162}
  8.  
  9. Public Function Encrypt(ByRef data() As Byte, ByVal key() As Byte) As Boolean
  10.     If (data Is Nothing) OrElse (data.Length <= 0) Then Return False
  11.     If (key Is Nothing) OrElse (key.Length <= 0) Then Return False
  12.     Dim key2() As Byte = key.Clone(), data2() As Byte = data.Clone()
  13.     If key2.Length > 32 Then ReDim Preserve key2(31)
  14.     Dim n As Int64
  15.     For n = 0 To 31
  16.         If n > key2.GetUpperBound(0) Then
  17.            ReDim Preserve key2(key2.Length)
  18.            key2(key2.GetUpperBound(0)) = CByte(n)
  19.         End If
  20.     Next
  21.     Dim arr() As Byte = {CByte(Asc("i")), CByte(Asc("g")), CByte(Asc("o")), CByte(Asc("r"))}
  22.     Dim res As Boolean = True
  23.     Dim ms As MemoryStream = Nothing
  24.     Dim cs As CryptoStream = Nothing
  25.     Dim rm As RijndaelManaged = Nothing
  26.     Dim bw As BinaryWriter = Nothing
  27.     Try
  28.         ReDim Preserve data2(data2.GetUpperBound(0) + 4)
  29.         Call Array.Copy(arr, 0, data2, data.Length, 4)
  30.         rm = New RijndaelManaged
  31.         ms = New MemoryStream()
  32.         cs = New CryptoStream(ms, rm.CreateEncryptor(key2, encv), CryptoStreamMode.Write)
  33.         cs.Write(data2, 0, data2.Length)
  34.         Call cs.FlushFinalBlock()
  35.         bw = New BinaryWriter(ms)
  36.         Call bw.Seek(0, SeekOrigin.End)
  37.         Dim bs() As Byte = BitConverter.GetBytes(CType((data.GetLongLength(0) + 4), Int64))
  38.         bw.Write(bs) : bw.Flush() : Erase bs   
  39.         data = ms.ToArray()
  40.     Catch
  41.         res = False
  42.     Finally
  43.        Try
  44.           If Not IsNothing(rm) Then rm.Clear()
  45.           If Not IsNothing(cs) Then cs.Close()
  46.        Catch
  47.        Finally
  48.           If Not IsNothing(ms) Then ms.Close()
  49.           If Not IsNothing(bw) Then bw.Close()
  50.           Erase arr : Erase data2 : Erase key2
  51.        End Try
  52.     End Try
  53.     Return res
  54. End Function
  55.  
  56. Public Function Decrypt(ByRef data() As Byte, ByVal key() As Byte) As Boolean
  57.    If IsNothing(data) OrElse (data.Length <= 8) Then Return False
  58.    If (key Is Nothing) OrElse (key.Length <= 0) Then Return False
  59.    Dim key2() As Byte = key.Clone(), data2() As Byte = data.Clone()
  60.    Dim l As Int64, res As Boolean = True
  61.    Try
  62.       l = BitConverter.ToInt64(data2, data2.GetLongLength(0) - 8)
  63.       ReDim Preserve data2(data2.GetLongLength(0) - 9)
  64.    Catch
  65.       Return False
  66.    End Try
  67.    If key2.Length > 32 Then ReDim Preserve key2(31)
  68.    Dim n As Int64
  69.    For n = 0 To 31
  70.        If n > key2.GetUpperBound(0) Then
  71.           ReDim Preserve key2(key2.Length)
  72.           key2(key2.GetUpperBound(0)) = CByte(n)
  73.        End If
  74.    Next
  75.    Dim decripted() As Byte = Nothing
  76.    Dim rm As RijndaelManaged = Nothing
  77.    Dim ms As MemoryStream = Nothing
  78.    Dim cs As CryptoStream = Nothing
  79.    Try
  80.        ReDim decripted(data2.Length - 1)
  81.        ms = New MemoryStream(data2)
  82.        rm = New RijndaelManaged
  83.        cs = New CryptoStream(ms, rm.CreateDecryptor(key2, encv), CryptoStreamMode.Read)
  84.        cs.Read(decripted, 0, decripted.Length)
  85.        ReDim Preserve decripted(l - 1)
  86.        If (decripted(decripted.GetUpperBound(0) - 3) = Asc("i")) AndAlso _
  87.           (decripted(decripted.GetUpperBound(0) - 2) = Asc("g")) AndAlso _
  88.           (decripted(decripted.GetUpperBound(0) - 1) = Asc("o")) AndAlso _
  89.           (decripted(decripted.GetUpperBound(0)) = Asc("r")) Then
  90.           ReDim Preserve decripted(decripted.GetUpperBound(0) - 4)
  91.           data = decripted
  92.        Else
  93.           Erase decripted
  94.           res = False
  95.        End If
  96.    Catch
  97.        res = False
  98.    Finally
  99.        Try
  100.           If Not IsNothing(rm) Then rm.Clear()
  101.           If Not IsNothing(cs) Then cs.Close()
  102.        Catch
  103.        Finally
  104.           If Not IsNothing(ms) Then ms.Close()
  105.           Erase data2 : Erase key2
  106.        End Try
  107.    End Try
  108.    Return res
  109. End Function
  110.  
  111. End Module
  112.  

..и было сказано:
Спасибо: Marat
Это отдельная страница сообщения форума. Чтобы посмотреть всю ветку, нажмите на эту ссылку.