Advertisement
ZeekoSec

String Encrypt/Decrypt

Mar 27th, 2015
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.62 KB | None | 0 0
  1. Imports System
  2. Imports System.Collections.Generic
  3. Imports System.Linq
  4. Imports System.Text
  5. Imports System.Security.Cryptography
  6. Imports System.IO
  7.  
  8. Namespace My
  9.    
  10.     Public Class strCrypto
  11.        
  12.         ' This constant string is used as a "salt" value for the PasswordDeriveBytes function calls.
  13.         ' This size of the IV (in bytes) must = (keysize / 8).  Default keysize is 256, so the IV must be
  14.         ' 32 bytes long.  Using a 16 character string here gives us 32 bytes when converted to a byte array.
  15.         Private Const initVector As String = "r5dm5fgm24mfhfku"
  16.        
  17.         Private Const passPhrase As String = "yourpassphrase"
  18.        
  19.         ' email password encryption password
  20.         ' This constant is used to determine the keysize of the encryption algorithm.
  21.         Private Const keysize As Integer = 256
  22.        
  23.         Public Shared Function encryptString(ByVal plainText As String) As String
  24.             'if the plaintext  is empty or null string just return an empty string
  25.             If ((plainText = "")  _
  26.                         OrElse (plainText Is Nothing)) Then
  27.                 Return ""
  28.             End If
  29.             Dim initVectorBytes() As Byte = Encoding.UTF8.GetBytes(initVector)
  30.             Dim plainTextBytes() As Byte = Encoding.UTF8.GetBytes(plainText)
  31.             Dim password As PasswordDeriveBytes = New PasswordDeriveBytes(passPhrase, Nothing)
  32.             Dim keyBytes() As Byte = password.GetBytes((keysize / 8))
  33.             Dim symmetricKey As RijndaelManaged = New RijndaelManaged
  34.             symmetricKey.Mode = CipherMode.CBC
  35.             Dim encryptor As ICryptoTransform = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes)
  36.             Dim memoryStream As MemoryStream = New MemoryStream
  37.             Dim cryptoStream As CryptoStream = New CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)
  38.             cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length)
  39.             cryptoStream.FlushFinalBlock
  40.             Dim cipherTextBytes() As Byte = memoryStream.ToArray
  41.             memoryStream.Close
  42.             cryptoStream.Close
  43.             Return Convert.ToBase64String(cipherTextBytes)
  44.         End Function
  45.        
  46.         Public Shared Function decryptString(ByVal cipherText As String) As String
  47.             'if the ciphertext is empty or null string just return an empty string
  48.             If ((cipherText = "")  _
  49.                         OrElse (cipherText Is Nothing)) Then
  50.                 Return ""
  51.             End If
  52.             Dim initVectorBytes() As Byte = Encoding.ASCII.GetBytes(initVector)
  53.             Dim cipherTextBytes() As Byte = Convert.FromBase64String(cipherText)
  54.             Dim password As PasswordDeriveBytes = New PasswordDeriveBytes(passPhrase, Nothing)
  55.             Dim keyBytes() As Byte = password.GetBytes((keysize / 8))
  56.             Dim symmetricKey As RijndaelManaged = New RijndaelManaged
  57.             symmetricKey.Mode = CipherMode.CBC
  58.             Dim decryptor As ICryptoTransform = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes)
  59.             Dim memoryStream As MemoryStream = New MemoryStream(cipherTextBytes)
  60.             Dim cryptoStream As CryptoStream = New CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)
  61.             Dim plainTextBytes() As Byte = New Byte((cipherTextBytes.Length) - 1) {}
  62.             Dim decryptedByteCount As Integer = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length)
  63.             memoryStream.Close
  64.             cryptoStream.Close
  65.             Return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount)
  66.         End Function
  67.     End Class
  68. End Namespace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement