Advertisement
ZeekoSec

TrippleDES

Mar 26th, 2015
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.57 KB | None | 0 0
  1.  Public Function EncryptData(
  2.     ByVal plaintext As String) As String
  3.  
  4.         ' Convert the plaintext string to a byte array.
  5.         Dim plaintextBytes() As Byte =
  6.             System.Text.Encoding.Unicode.GetBytes(plaintext)
  7.  
  8.         ' Create the stream.
  9.         Dim ms As New System.IO.MemoryStream
  10.         ' Create the encoder to write to the stream.
  11.         Dim encStream As New CryptoStream(ms, TripleDes.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write)
  12.  
  13.         ' Use the crypto stream to write the byte array to the stream.
  14.         encStream.Write(plaintextBytes, 0, plaintextBytes.Length)
  15.         encStream.FlushFinalBlock()
  16.  
  17.         ' Convert the encrypted stream to a printable string.
  18.         Return Convert.ToBase64String(ms.ToArray)
  19.     End Function
  20.  
  21.     Public Function DecryptData(ByVal encryptedtext As String) As String
  22.  
  23.         ' Convert the encrypted text string to a byte array.
  24.         Dim encryptedBytes() As Byte = Convert.FromBase64String(encryptedtext)
  25.  
  26.         ' Create the stream.
  27.         Dim ms As New System.IO.MemoryStream
  28.         ' Create the decoder to write to the stream.
  29.         Dim decStream As New CryptoStream(ms, TripleDes.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write)
  30.  
  31.         ' Use the crypto stream to write the byte array to the stream.
  32.         decStream.Write(encryptedBytes, 0, encryptedBytes.Length)
  33.         decStream.FlushFinalBlock()
  34.  
  35.         ' Convert the plaintext stream to a string.
  36.         Return System.Text.Encoding.Unicode.GetString(ms.ToArray)
  37.     End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement