Hiyper
Enthusiast
In this tutorial, I will be teaching you the basics of encryption in VB. So lets jump right in.
Design:
First we will begin our encryption function. but first include the following imports:
Now we need to set up a few variables.
Now we need to make our MD5 hash providing function. This will hash our "key".
And our encrypt function.
That is all! You can now double click your encrypt button and add this code.
Now it will encrypt our text! You can test it out so far. Now is time for decryption.
And its as simple as that! Now all we have to do is implement that function. Double click our decrypt button
Keep in mind that this a a very simple encryption method, and is VERY unsafe. It should never be used in a professional setting, and private key method is also risky. (Consider using AES instead of DES for more safety and SHA-256 instead of MD5)
Explanations:
MD5Hash Function:
A hash is a algorithm that can ONLY be encrypted, not decrypted.
Take this string, "Hashing". Pretend our string is going to be put through a car wash. This car wash could be a lot of different washes of different speeds and how good it cleans. The car wash is our type of hash. Ours is MD5. Once a string is hashed, it can never be un-hashed. So we take our "key" and hash it.
Hashing - > MD5 "Car Wash" -> befd1ea261d11ae5ba4f3f0363313c52
Now our string "Hashing" has been turned into a long string of letters and numbers. This is undo-able (Well, it is, but that is a whole tutorial on itself).
Encrypt Function:
First thing we do is input the hashed key as our encryption key. Now our encryption method "DES" is one of many different methods. Encryption is just like hashing, but it can be undone, but only with our special key (Not really, but again a whole other tutorial for a different forum). We then set what method it is going to use (this is very complicated, just know it is how it does the encryption). Then we change our string into pure bytes. Those pure bytes are plugged into the DES encryptor, which gives us encrypted bytes, Which we convert back into a string.
Decrypt Function:
Same as encrypt, just in reverse.
Hope you enjoyed! (I typed this all by hand so let me know of any errors)
Design:
- Create a project.
- Add a textbox and name it TB_input
- Add another textbox and name it TB_output
- Add a button and name it encrypt
- add a button and name it decrypt
First we will begin our encryption function. but first include the following imports:
Code:
Imports System.Security.Cryptography
Imports System.Text
Now we need to set up a few variables.
Code:
Dim MD5 as new MD5CryptoServiceProvider
Dim DES as new TripleDESCryptoServiceProvider
Now we need to make our MD5 hash providing function. This will hash our "key".
Code:
Function MD5Hash(key as String) as Byte()
Return MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(key)
End Function
And our encrypt function.
Code:
Function Encrypt(input as String, key as String) as String
DES.Key = MD5Hash(key) ' Sets the encryption key as a md5 hash
DES.Mode = CipherMode.ECB ' uses electronic book cipher mode
Dim buffer as Byte() = ASCIIEncoding.ASCII.GetBytes(input) ' Turn our string into bytes
Return System.Convert.ToBase64String(DES.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length)) ' encrypts our bytes, then returns the string
End Function
That is all! You can now double click your encrypt button and add this code.
Code:
Private Sub encrypt_Click(sender as Object, e as EventArgs) Handles encrypt.Click
output.Text = Encrypt(input.Text, "thisisakey") '"thisisakey can be anything you want"
End Sub
Now it will encrypt our text! You can test it out so far. Now is time for decryption.
Code:
'we can basically use order of operations to reverse this
Function Decrypt(input as String, key as String) as String
DES.Key = MD5Hash(key)
DES.Mode = CipherMode.ECB
Dim buffer as Byte() = System.Convert.FromBase64String(input)
Return ASCIIEncoding.ASCII.GetString(DES.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.length))
End Function
And its as simple as that! Now all we have to do is implement that function. Double click our decrypt button
Code:
Private Sub decrypt_Click(sender as Object, e as EventArgs) Handles decrypt.Click
input.Text = Decrypt(output.Text, "thisisakey") '"thisisakey can be anything you want"
End Sub
Keep in mind that this a a very simple encryption method, and is VERY unsafe. It should never be used in a professional setting, and private key method is also risky. (Consider using AES instead of DES for more safety and SHA-256 instead of MD5)
Explanations:
MD5Hash Function:
A hash is a algorithm that can ONLY be encrypted, not decrypted.
Take this string, "Hashing". Pretend our string is going to be put through a car wash. This car wash could be a lot of different washes of different speeds and how good it cleans. The car wash is our type of hash. Ours is MD5. Once a string is hashed, it can never be un-hashed. So we take our "key" and hash it.
Hashing - > MD5 "Car Wash" -> befd1ea261d11ae5ba4f3f0363313c52
Now our string "Hashing" has been turned into a long string of letters and numbers. This is undo-able (Well, it is, but that is a whole tutorial on itself).
Encrypt Function:
First thing we do is input the hashed key as our encryption key. Now our encryption method "DES" is one of many different methods. Encryption is just like hashing, but it can be undone, but only with our special key (Not really, but again a whole other tutorial for a different forum). We then set what method it is going to use (this is very complicated, just know it is how it does the encryption). Then we change our string into pure bytes. Those pure bytes are plugged into the DES encryptor, which gives us encrypted bytes, Which we convert back into a string.
Decrypt Function:
Same as encrypt, just in reverse.
Hope you enjoyed! (I typed this all by hand so let me know of any errors)
Last edited: