Hello all, I found a couple functions to encrypt and decrypt any string with a chosen en/decryption key.
I have added onto it and thought I would release it for you peoples to use, if you need something like this.
Now, I have a 7 line file, each line with a different, seperate value. So, I use these subs to encrypt/decrypt that file for me.
That'd need to be changed around a bit depending on your needs and use of these functions, though.
The Load sub is called when loading each form. It just loads/reloads the text file into the variable.
It is all public shared because it is located in a seperate class, so it does not clutter all my forms. I can easily call it from any form by making it public shared.
I have added onto it and thought I would release it for you peoples to use, if you need something like this.
VB.NET:
Option Explicit On
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography
Public Class Crypt
Public Shared ary As String()
Public Shared DES As New TripleDESCryptoServiceProvider
Public Shared MD5 As New MD5CryptoServiceProvider
Public Shared Function MD5Hash(ByVal value As String) As Byte()
Return MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(value))
End Function
Public Shared Function Encrypt(ByVal stringToEncrypt As String, ByVal key As String) As String
DES.Key = Crypt.MD5Hash(key)
DES.Mode = CipherMode.ECB
Dim Buffer As Byte() = ASCIIEncoding.ASCII.GetBytes(stringToEncrypt)
Return Convert.ToBase64String(DES.CreateEncryptor().TransformFinalBlock(Buffer, 0, Buffer.Length))
End Function
Public Shared Function Decrypt(ByVal encryptedString As String, ByVal key As String) As String
Try
DES.Key = Crypt.MD5Hash(key)
DES.Mode = CipherMode.ECB
Dim Buffer As Byte() = Convert.FromBase64String(encryptedString)
Return ASCIIEncoding.ASCII.GetString(DES.CreateDecryptor().TransformFinalBlock(Buffer, 0, Buffer.Length))
Catch ex As Exception
MessageBox.Show("Invalid config.ini", "Invalid", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Main.Close()
End Try
End Function
End Class
Now, I have a 7 line file, each line with a different, seperate value. So, I use these subs to encrypt/decrypt that file for me.
VB.NET:
Public Shared configfile As String = My.Application.Info.DirectoryPath.ToString & ".\config.ini"
Public Shared Sub Load()
Dim str As String = My.Computer.FileSystem.ReadAllText(configfile)
ary = str.Split(vbNewLine)
DecryptStuff()
End Sub
Public Shared Sub DecryptStuff()
Dim i As Integer
For i = 0 To 6
Dim dec = Decrypt(ary(i), "KEY")
ary(i) = dec
Next
End Sub
Public Shared Sub EncryptStuff()
Dim objWriter As New System.IO.StreamWriter(configfile, False)
Dim i As Integer
For i = 0 To 6
Dim enc = Encrypt(ary(i), "KEY")
ary(i) = enc
objWriter.WriteLine(ary(i))
Next
objWriter.Close()
DecryptStuff()
End Sub
The Load sub is called when loading each form. It just loads/reloads the text file into the variable.
It is all public shared because it is located in a seperate class, so it does not clutter all my forms. I can easily call it from any form by making it public shared.