Guest User

Visual Basic Code / Tutorial: How to Encrypt / Decrypt Using

a guest
Jan 16th, 2016
2,356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '-----------------------------ENCRYPT
  2.  
  3. Imports System.IO
  4. Imports System.Security.Cryptography
  5.  
  6. Module Encrypt
  7.  
  8.     Sub EncryptText()
  9.         Try
  10.             Dim EncryptStream As New FileStream("C:\somePath\Test.txt", FileMode.Open)
  11.             Dim RMCrypto As New RijndaelManaged()
  12.             Dim key As Byte() = {&H1, &H2, &H3, &H4, &H5, &H6, &H7, &H8, &H9, &H10, &H11, &H12, &H13, &H14, &H15, &H16}
  13.             Dim IV As Byte() = {&H1, &H2, &H3, &H4, &H5, &H6, &H7, &H8, &H9, &H10, &H11, &H12, &H13, &H14, &H15, &H16}
  14.             Dim CryptStream As New CryptoStream(EncryptStream, RMCrypto.CreateEncryptor(key, IV), CryptoStreamMode.Write)
  15.             Dim sWriter As New StreamWriter(CryptStream)
  16.  
  17.             sWriter.WriteLine(Form1.TextBox1.Text)
  18.             MsgBox("The plaintext has been converted to ciphertext.", MsgBoxStyle.Information, "Encrypted Text")
  19.  
  20.             sWriter.Close()
  21.             CryptStream.Close()
  22.             EncryptStream.Close()
  23.  
  24.         Catch ex As Exception
  25.             MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Encrypting Error")
  26.         End Try
  27.  
  28.     End Sub
  29.  
  30. End Module
  31.  
  32.  
  33.  
  34.  
  35. '--------------------------DECRYPT
  36.  
  37. Imports System.IO
  38. Imports System.Security.Cryptography
  39.  
  40. Module Decrypt
  41.  
  42.     Sub Decyrpt()
  43.  
  44.         Dim key As Byte() = {&H1, &H2, &H3, &H4, &H5, &H6, &H7, &H8, &H9, &H10, &H11, &H12, &H13, &H14, &H15, &H16}
  45.         Dim IV As Byte() = {&H1, &H2, &H3, &H4, &H5, &H6, &H7, &H8, &H9, &H10, &H11, &H12, &H13, &H14, &H15, &H16}
  46.  
  47.         Try
  48.             Dim DecryptStream As New FileStream("C:\somePath\Test.txt", FileMode.OpenOrCreate)
  49.             Dim RMCrypto As New RijndaelManaged()
  50.             Dim CryptStream As New CryptoStream(DecryptStream, RMCrypto.CreateDecryptor(key, IV), CryptoStreamMode.Read)
  51.             Dim sRead As New StreamReader(CryptStream)
  52.  
  53.             Form1.TextBox2.Text = String.Format("The plaintext is: {0} {1}", vbCrLf, sRead.ReadToEnd())
  54.             sRead.Close()
  55.             DecryptStream.Close()
  56.  
  57.         Catch ex As Exception
  58.             MsgBox(ex.Message, MsgBoxStyle.Critical, "Decrypting Error")
  59.         End Try
  60.     End Sub
  61.  
  62. End Module
Advertisement
Add Comment
Please, Sign In to add comment