Guest User

RC2 Encryption

a guest
Oct 22nd, 2010
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.78 KB | None | 0 0
  1.  
  2. Public Class RC_2
  3.     Private Shared RC2 As New RC2CryptoServiceProvider
  4.     Private Shared key As Byte(), IV As Byte() ' to hold our secret key and initialization vector...
  5.  
  6.     Private Shared Function Generate_Key() As Byte() 'Function to generate a new random secret key for encryption...
  7.         RC2.GenerateKey()
  8.         Dim a As Byte() = RC2.Key
  9.         Return a
  10.     End Function
  11.     Private Shared Function Generate_IV() As Byte() 'Function to generate a new random Initialization vector for our encryption...
  12.         RC2.GenerateIV()
  13.         Dim a As Byte() = RC2.IV
  14.         Return a
  15.     End Function
  16.  
  17.     Public Shared Function Encrypt(ByVal input As String) As Byte()
  18.         'Set and save our key and the vector....These generate a new key and initializaion vector every time you call them...
  19.         key = Generate_Key()
  20.         IV = Generate_IV()
  21.         Dim Crypt As ICryptoTransform = RC2.CreateEncryptor(key, IV)
  22.         Dim input_bytes As Byte() = System.Text.Encoding.Default.GetBytes(input)
  23.         Dim mem As New MemoryStream
  24.         Dim crypto As New CryptoStream(mem, Crypt, CryptoStreamMode.Write)
  25.         Dim out As Byte()
  26.         crypto.Write(input_bytes, 0, input_bytes.Length)
  27.         crypto.FlushFinalBlock()
  28.         out = mem.ToArray
  29.         Return out
  30.     End Function
  31.  
  32.     Public Shared Function Decrypt(ByVal input As String) As Byte()
  33.         Dim Crypt As ICryptoTransform = RC2.CreateDecryptor(key, IV)
  34.         Dim input_bytes As Byte() = System.Text.Encoding.Default.GetBytes(input)
  35.         Dim mem As New MemoryStream(input_bytes)
  36.         Dim crypto As New CryptoStream(mem, Crypt, CryptoStreamMode.Read)
  37.         Dim out(input_bytes.Length - 1) As Byte
  38.         crypto.Read(out, 0, input_bytes.Length)
  39.         Return out
  40.     End Function
  41. End Class
Advertisement
Add Comment
Please, Sign In to add comment