Advertisement
Guest User

Crypt

a guest
Feb 23rd, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Imports System.IO
  2. Imports System.Text
  3. Imports System.Security.Cryptography
  4.  
  5. Public Class Form1
  6.  
  7.     Private Sub cmdEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEncrypt.Click
  8.         txtCipherText.Text = Encrypt(txtPlainText.Text, txtPassPhrase.Text, "s@1tValue", "SHA1", 2, "@1B2c3D4e5F6g7H8", 256)
  9.     End Sub
  10.  
  11.     Private Sub cmdDecrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDecrypt.Click
  12.         txtPlainText.Text = Decrypt(txtCipherText.Text, txtPassPhrase.Text, "s@1tValue", "SHA1", 2, "@1B2c3D4e5F6g7H8", 256)
  13.     End Sub
  14.  
  15.     Private Function Encrypt(ByVal plainText As String, ByVal passPhrase As String, ByVal saltValue As String, ByVal hashAlgorithm As String, ByVal passwordIterations As Integer, ByVal initVector As String, ByVal keySize As Integer) As String
  16.  
  17.         Dim initVectorBytes As Byte()
  18.         initVectorBytes = Encoding.ASCII.GetBytes(initVector)
  19.  
  20.         Dim saltValueBytes As Byte()
  21.         saltValueBytes = Encoding.ASCII.GetBytes(saltValue)
  22.  
  23.         Dim plainTextBytes As Byte()
  24.         plainTextBytes = Encoding.UTF8.GetBytes(plainText)
  25.  
  26.         Dim password As Rfc2898DeriveBytes = New Rfc2898DeriveBytes(passPhrase, saltValueBytes, passwordIterations)
  27.         Dim keyBytes As Byte()
  28.         keyBytes = password.GetBytes(CInt(keySize / 8))
  29.  
  30.         Dim symmetricKey As RijndaelManaged
  31.         symmetricKey = New RijndaelManaged()
  32.         symmetricKey.Mode = CipherMode.CBC
  33.  
  34.         Dim encryptor As ICryptoTransform
  35.         encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes)
  36.  
  37.         Dim memoryStream As MemoryStream
  38.         memoryStream = New MemoryStream()
  39.  
  40.         Dim cryptoStream As CryptoStream
  41.         cryptoStream = New CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)
  42.         cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length)
  43.         cryptoStream.FlushFinalBlock()
  44.  
  45.         Dim cipherTextBytes As Byte()
  46.         cipherTextBytes = memoryStream.ToArray()
  47.         memoryStream.Close()
  48.         cryptoStream.Close()
  49.  
  50.         Dim cipherText As String
  51.         cipherText = Convert.ToBase64String(cipherTextBytes)
  52.         Encrypt = cipherText
  53.  
  54.     End Function
  55.  
  56.     Private Function Decrypt(ByVal cipherText As String, ByVal passPhrase As String, ByVal saltValue As String, ByVal hashAlgorithm As String, ByVal passwordIterations As Integer, ByVal initVector As String, ByVal keySize As Integer) As String
  57.  
  58.         On Error Resume Next
  59.  
  60.         Dim initVectorBytes As Byte()
  61.         initVectorBytes = Encoding.ASCII.GetBytes(initVector)
  62.  
  63.         Dim saltValueBytes As Byte()
  64.         saltValueBytes = Encoding.ASCII.GetBytes(saltValue)
  65.  
  66.         Dim cipherTextBytes As Byte()
  67.         cipherTextBytes = Convert.FromBase64String(cipherText)
  68.  
  69.         Dim password As Rfc2898DeriveBytes = New Rfc2898DeriveBytes(passPhrase, saltValueBytes, passwordIterations)
  70.         Dim keyBytes As Byte()
  71.         keyBytes = password.GetBytes(CInt(keySize / 8))
  72.  
  73.         Dim symmetricKey As RijndaelManaged
  74.         symmetricKey = New RijndaelManaged()
  75.         symmetricKey.Mode = CipherMode.CBC
  76.  
  77.         Dim decryptor As ICryptoTransform
  78.         decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes)
  79.  
  80.         Dim memoryStream As MemoryStream
  81.         memoryStream = New MemoryStream(cipherTextBytes)
  82.  
  83.         Dim cryptoStream As CryptoStream
  84.         cryptoStream = New CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)
  85.  
  86.         Dim plainTextBytes As Byte()
  87.         ReDim plainTextBytes(cipherTextBytes.Length)
  88.  
  89.         Dim decryptedByteCount As Integer
  90.         decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length)
  91.  
  92.         memoryStream.Close()
  93.         cryptoStream.Close()
  94.  
  95.         Dim plainText As String
  96.         plainText = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount)
  97.  
  98.         Decrypt = plainText
  99.     End Function
  100. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement