pastebin

Paste Search Dynamic
Recent pastes
Security Cryptography
  1. imports System.IO
  2. imports System.Security.Cryptography
  3. public class Test
  4.         public Shared sub Main()
  5.                 ' your code goes here
  6.                 dim userID as string = ""
  7.         Console.WriteLine("Enter User ID: ")
  8.         userID = Console.ReadLine
  9.         dim myKey as string = ""
  10.         Console.WriteLine("Enter My Key: ")
  11.         myKey = Console.ReadLine
  12.         Console.WriteLine("UserID: " & userID)
  13.         Console.WriteLine("MyKey: " & myKey)
  14.         Console.WriteLine("Conversion is in process...........")
  15.         dim output as string = Encryption(System.Text.Encoding.UTF8.GetBytes(userID), myKey)
  16.         Console.WriteLine("Output: " & output)
  17.         Console.ReadKey()
  18.         end sub
  19.           public Shared function Encryption(byval data() as byte, byval MyKey as string) as string
  20.         try
  21.             'Comment for below mention line -- Convert MyKey ie., 64 character Key to Bytes of length : 48
  22.             dim EncData() as byte
  23.             dim _key() as byte = Convert.FromBase64String(MyKey)
  24.             'Comment for below mention line -- For more security purpose, again generating Bytes of below mentioned comma seperated values of length : 16
  25.             dim _iv() as byte = {83, 71, 26, 58, 54, 35, 22, 11, 83, 71, 26, 58, 54, 35, 22, 11}
  26.             'Comment for below mention line -- Passing both above generated bytes values to below mention Encry System.Security.Cryptography.Rfc2898DeriveBytes function
  27.             dim keyBuilder as Rfc2898DeriveBytes = new Rfc2898DeriveBytes(MyKey, _iv)
  28.             'Comment for below mention line -- String to a filestream, used a memorystream
  29.             dim ms as new MemoryStream()
  30.             'Comment for below mention line -- Creating object of System.Security.Cryptography.RijndaelManaged
  31.             dim aes as new RijndaelManaged()
  32.             'Comment for below mention line -- Setting Advanced Encryption Standard KeySize
  33.             aes.KeySize = 256
  34.             'Comment for below mention line -- A byte array filled with pseudo-random key bytes.
  35.             aes.IV = keyBuilder.GetBytes(ctype(aes.BlockSize / 8, integer))
  36.             aes.Key = keyBuilder.GetBytes(ctype(aes.KeySize / 8, integer))
  37.             'Initializes a new instance of the System.Security.Cryptography.CryptoStream class
  38.             dim cs as new CryptoStream(ms, aes.CreateEncryptor, CryptoStreamMode.write)
  39.             'Comment for below mention line -- Writes a sequence of bytes to the current System.Security.Cryptography.CryptoStream
  40.             cs.write(data, 0, data.Length)
  41.             'Comment for below mention line -- clears the buffer.
  42.             cs.FlushFinalBlock()
  43.             'The byte array from which this stream was created
  44.             EncData = ms.GetBuffer()
  45.             redim Preserve EncData(ms.Position - 1)
  46.             dim base64Encoded as string
  47.             base64Encoded = System.Convert.ToBase64String(EncData)
  48.             'Return ms.Position
  49.             Return base64Encoded
  50.         catch ex as Exception
  51.             throw ex
  52.         end try
  53.     end function
  54. end class
Parsed in 0.036 seconds