Created
July 11, 2013 09:27
-
-
Save ShaneGowland/5973974 to your computer and use it in GitHub Desktop.
AES Encryption and Decryption in VB.NET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Public Function AES_Encrypt(ByVal input As String, ByVal pass As String) As String | |
Dim AES As New System.Security.Cryptography.RijndaelManaged | |
Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider | |
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 | |
Return input 'If encryption fails, return the unaltered input. | |
End Try | |
End Function | |
'Decrypt a string with AES | |
Public Function AES_Decrypt(ByVal input As String, ByVal pass As String) As String | |
Dim AES As New System.Security.Cryptography.RijndaelManaged | |
Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider | |
Dim decrypted 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 DESDecrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateDecryptor | |
Dim Buffer As Byte() = Convert.FromBase64String(input) | |
decrypted = System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length)) | |
Return decrypted | |
Catch ex As Exception | |
Return input 'If decryption fails, return the unaltered input. | |
End Try | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks works great