Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AES Encrypt String in VB.NET

Tags:

vb.net

aes

I have a program based in Visual Basic 2010.

I want to use a custom keyword and AES encryption to generate registration keys on our company website, that will unlock the software regardless of whether or not the software is connected to the internet.

To do this, I want to encrypt certain user information (and a validation code) into an AES encrypted string using a utility I'll build on my website. Then, I want my program to decrypt the string into the user information and validation code, then use that information to validate the registration key.

Which brings me to the question - how do I encrypt and decrypt a string in AES programatically? Is there a code template I can use somewhere, or a built in method?

like image 593
CodeMouse92 Avatar asked May 13 '11 04:05

CodeMouse92


People also ask

How do I encrypt text in VB net?

There's a text box, an "encrypt" button, and a "decrypt" button. The idea is to type something into the text box ("like 'hello world'"), click "encrypt", and see the encrypted version appear in the text box. Clicking "decrypt" should then take you back to the original string.

How do you encrypt data in Visual Basic?

To create the encryption wrapperAdd a private method that creates a byte array of a specified length from the hash of the specified key. Add a constructor to initialize the 3DES cryptographic service provider. The key parameter controls the EncryptData and DecryptData methods.

How do you decrypt a string?

Decryption Approach:Find the length L of the string. Find the ceil and floor values of √Length and assign them to the variables. Create a 2D matrix and fill the matrix by characters of string column-wise. Read the matrix row-wise to get the decrypted string.


1 Answers

A quick Google Search brings up the following functions: I've not tested whether the output they produce is correct, however they do appear to compile correctly.

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
        End Try
    End Function

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
        End Try
    End Function

Sourced from: http://www.l33thackers.com/Thread-VB-NET-AES-Encryption

like image 91
Shane Gowland Avatar answered Sep 20 '22 16:09

Shane Gowland