Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27329

VS 2010 AES Encryption

$
0
0
Hi Guys,

I am building myself a library to use whenever I need to do encryption I have various things in it such as AES, 3DES and RSA Encryption types as well as Being able to has passwords.

When it comes to the AES stuff I know there is 256/512 etc and the code sample I had seems to specify RijndaelManaged where as there is SHA256Managed etc and I am trying to build my routine so it supports the different types however I have a feeling I have done it completely wrong. Please could someone point me in the right direction? This is what I have so far:

VB.NET Code:
  1. Private Shared Function AES_EncryptString(ByVal input As String, ByVal pass As String, ByVal type As Integer) As String
  2.             Dim AES = New System.Security.Cryptography.RijndaelManaged
  3.             Dim HASH_AES
  4.  
  5.             Select Case type
  6.                 Case 0
  7.                     HASH_AES = New System.Security.Cryptography.MD5CryptoServiceProvider
  8.                 Case 1
  9.                     HASH_AES = New System.Security.Cryptography.SHA256CryptoServiceProvider
  10.                 Case 2
  11.                     HASH_AES = New System.Security.Cryptography.SHA512CryptoServiceProvider
  12.             End Select
  13.  
  14.             Dim encrypted As String = ""
  15.             Try
  16.                 Dim hash(31) As Byte
  17.                 Dim temp As Byte() = HASH_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
  18.                 Array.Copy(temp, 0, hash, 0, 16)
  19.                 Array.Copy(temp, 0, hash, 15, 16)
  20.                 AES.Key = hash
  21.  
  22.                 AES.Mode = Security.Cryptography.CipherMode.ECB
  23.                 Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor
  24.                 Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(input)
  25.                 encrypted = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
  26.                 Return encrypted
  27.             Catch ex As Exception
  28.                 Throw New Exception("Error: Unable to perform Encryption on string! See Exception: " & ex.ToString)
  29.             End Try
  30.         End Function

Viewing all articles
Browse latest Browse all 27329

Trending Articles