Advertisement
calfred2808

Text En/De

Nov 11th, 2012
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.30 KB | None | 0 0
  1. Imports System.Security.Cryptography  
  2. Imports system.Text  
  3. Imports System.IO  
  4.  
  5. Public Function Encrypt(ByVal text As String, ByVal key As String) As String
  6.         Try
  7.             Dim crp As New TripleDESCryptoServiceProvider  
  8.             Dim uEncode As New UnicodeEncoding  
  9.             Dim bytPlainText() As Byte = uEncode.GetBytes(text)  
  10.             Dim stmCipherText As New MemoryStream  
  11.             Dim slt() As Byte = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}  
  12.             Dim pdb As New Rfc2898DeriveBytes(key, slt)  
  13.             Dim bytDerivedKey() As Byte = pdb.GetBytes(24)  
  14.  
  15.             crp.Key = bytDerivedKey  
  16.             crp.IV = pdb.GetBytes(8)  
  17.  
  18.             Dim csEncrypted As New CryptoStream(stmCipherText, crp.CreateEncryptor(), CryptoStreamMode.Write)  
  19.  
  20.             csEncrypted.Write(bytPlainText, 0, bytPlainText.Length)  
  21.             csEncrypted.FlushFinalBlock()  
  22.             Return Convert.ToBase64String(stmCipherText.ToArray())  
  23.         Catch ex As Exception  
  24.             Throw
  25.         End Try
  26.     End Function
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45. Function Decrypt(ByVal text As String, ByVal key As String) As String
  46.  
  47.         Dim crp As TripleDESCryptoServiceProvider  
  48.         Try
  49.             crp = New TripleDESCryptoServiceProvider  
  50.             Dim uEncode As New UnicodeEncoding  
  51.             Dim bytCipherText() As Byte = Convert.FromBase64String(text)  
  52.             Dim stmPlainText As New MemoryStream  
  53.             Dim stmCipherText As New MemoryStream(bytCipherText)  
  54.             Dim slt() As Byte = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}  
  55.             Dim pdb As New Rfc2898DeriveBytes(key, slt)  
  56.             Dim bytDerivedKey() As Byte = pdb.GetBytes(24)  
  57.             crp.Key = bytDerivedKey  
  58.             crp.IV = pdb.GetBytes(8)  
  59.  
  60.             Dim csDecrypted As New CryptoStream(stmCipherText, crp.CreateDecryptor(), CryptoStreamMode.Read)  
  61.             Dim sw As New StreamWriter(stmPlainText)  
  62.             Dim sr As New StreamReader(csDecrypted)  
  63.             sw.Write(sr.ReadToEnd)  
  64.             sw.Flush()  
  65.             csDecrypted.Clear()  
  66.             crp.Clear()  
  67.             Return uEncode.GetString(stmPlainText.ToArray())  
  68.         Catch ex As Exception  
  69.             Throw
  70.         End Try
  71.  
  72.     End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement