Advertisement
Guest User

Untitled

a guest
Feb 27th, 2012
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. Imports System.Security.Cryptography
  2. Imports System.IO
  3. Imports System.Text
  4. Public Class Form1
  5. Inherits System.Windows.Forms.Form
  6.  
  7. ' Encrypt using stream (binary)
  8. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  9. Dim rd As New RijndaelManaged
  10.  
  11. Dim md5 As New MD5CryptoServiceProvider
  12. Dim key() As Byte = md5.ComputeHash(Encoding.UTF8.GetBytes(TextBox2.Text))
  13.  
  14. md5.Clear()
  15. rd.Key = key
  16. rd.GenerateIV()
  17.  
  18. Dim iv() As Byte = rd.IV
  19. Dim ms As New MemoryStream
  20.  
  21. ms.Write(iv, 0, iv.Length)
  22.  
  23. Dim cs As New CryptoStream(ms, rd.CreateEncryptor, CryptoStreamMode.Write)
  24. Dim data() As Byte = System.Text.Encoding.UTF8.GetBytes(Textbox1.Text)
  25.  
  26. cs.Write(data, 0, data.Length)
  27. cs.FlushFinalBlock()
  28.  
  29. Dim encdata() As Byte = ms.ToArray()
  30. TextBox3.Text = Convert.ToBase64String(encdata)
  31. cs.Close()
  32. rd.Clear()
  33. TextBox1.Text = ""
  34. End Sub
  35. ' Decrypt using stream (binary)
  36. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  37. Dim rd As New RijndaelManaged
  38. Dim rijndaelIvLength As Integer = 16
  39. Dim md5 As New MD5CryptoServiceProvider
  40. Dim key() As Byte = md5.ComputeHash(Encoding.UTF8.GetBytes(Textbox2.Text))
  41.  
  42. md5.Clear()
  43.  
  44. Dim encdata() As Byte = Convert.FromBase64String(textbox3.text)
  45. Dim ms As New MemoryStream(encdata)
  46. Dim iv(15) As Byte
  47.  
  48. ms.Read(iv, 0, rijndaelIvLength)
  49. rd.IV = iv
  50. rd.Key = key
  51.  
  52. Dim cs As New CryptoStream(ms, rd.CreateDecryptor, CryptoStreamMode.Read)
  53.  
  54. Dim data(ms.Length - rijndaelIvLength) As Byte
  55. Dim i As Integer = cs.Read(data, 0, data.Length)
  56.  
  57. TextBox1.Text = System.Text.Encoding.UTF8.GetString(data, 0, i)
  58. cs.Close()
  59. rd.Clear()
  60. End Sub
  61. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement