Advertisement
calfred2808

Encrypt/Decrypt

Nov 11th, 2012
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.93 KB | None | 0 0
  1.  'The function used to encrypt the text
  2.     Private Function Encrypt(ByVal strText As String, ByVal strEncrKey _
  3.              As String) As String
  4.         Dim byKey() As Byte = {}
  5.         Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
  6.  
  7.         Try
  8.             byKey = System.Text.Encoding.UTF8.GetBytes(Left(strEncrKey, 8))
  9.  
  10.             Dim des As New DESCryptoServiceProvider()
  11.             Dim inputByteArray() As Byte = Encoding.UTF8.GetBytes(strText)
  12.             Dim ms As New MemoryStream()
  13.             Dim cs As New CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write)
  14.             cs.Write(inputByteArray, 0, inputByteArray.Length)
  15.             cs.FlushFinalBlock()
  16.             Return Convert.ToBase64String(ms.ToArray())
  17.  
  18.         Catch ex As Exception
  19.             Return ex.Message
  20.         End Try
  21.  
  22.     End Function
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. 'The function used to decrypt the text
  71.  
  72.   Private Function Decrypt(ByVal strText As String, ByVal sDecrKey _
  73.                As String) As String
  74.         Dim byKey() As Byte = {}
  75.         Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
  76.         Dim inputByteArray(strText.Length) As Byte
  77.  
  78.         Try
  79.             byKey = System.Text.Encoding.UTF8.GetBytes(Left(sDecrKey, 8))
  80.             Dim des As New DESCryptoServiceProvider()
  81.             inputByteArray = Convert.FromBase64String(strText)
  82.             Dim ms As New MemoryStream()
  83.             Dim cs As New CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write)
  84.  
  85.             cs.Write(inputByteArray, 0, inputByteArray.Length)
  86.             cs.FlushFinalBlock()
  87.             Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8
  88.  
  89.             Return encoding.GetString(ms.ToArray())
  90.  
  91.         Catch ex As Exception
  92.             Return ex.Message
  93.         End Try
  94.  
  95.     End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement