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:
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:
Private Shared Function AES_EncryptString(ByVal input As String, ByVal pass As String, ByVal type As Integer) As String Dim AES = New System.Security.Cryptography.RijndaelManaged Dim HASH_AES Select Case type Case 0 HASH_AES = New System.Security.Cryptography.MD5CryptoServiceProvider Case 1 HASH_AES = New System.Security.Cryptography.SHA256CryptoServiceProvider Case 2 HASH_AES = New System.Security.Cryptography.SHA512CryptoServiceProvider End Select Dim encrypted As String = "" Try Dim hash(31) As Byte Dim temp As Byte() = HASH_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass)) Array.Copy(temp, 0, hash, 0, 16) Array.Copy(temp, 0, hash, 15, 16) AES.Key = hash AES.Mode = Security.Cryptography.CipherMode.ECB Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(input) encrypted = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length)) Return encrypted Catch ex As Exception Throw New Exception("Error: Unable to perform Encryption on string! See Exception: " & ex.ToString) End Try End Function