EDN Admin
Well-known member
Hello... Im having problems with code I got from this thread:
http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/beefbf08-0497-40f1-b258-ba09afc49cb7/
The topic was locked, so I had to make a new thread here. I literally copied + pasted the code from the original post and just renamed things so I could understand them better, but I cant launch the code without receiving an error.
Public Class Login
Dim Thread As System.Threading.Thread
Private Function GenerateKey() As String
Dim DESCrypto As DESCryptoServiceProvider = CType(DESCryptoServiceProvider.Create(), DESCryptoServiceProvider)
Return ASCIIEncoding.ASCII.GetString(DESCrypto.Key)
End Function
Private Sub EncryptFile(ByVal Filename As String, ByVal Key As String)
Dim ByteArray As Byte() = System.IO.File.ReadAllBytes(Filename)
Dim DES As New DESCryptoServiceProvider()
Dim MS As New MemoryStream()
DES.Key = ASCIIEncoding.ASCII.GetBytes(Key)
DES.IV = ASCIIEncoding.ASCII.GetBytes(Key)
Dim DESEncrypt As ICryptoTransform = DES.CreateEncryptor()
Dim CryptoStream As New CryptoStream(MS, DESEncrypt, CryptoStreamMode.Write)
CryptoStream.Write(ByteArray, 0, ByteArray.Length)
CryptoStream.Close()
Dim EncryptedData As Byte() = MS.ToArray()
System.IO.File.WriteAllBytes(Filename, EncryptedData)
End Sub
Private Function DecryptFileAsByte(ByVal Filename As String, ByVal Key As String) As Byte()
Dim ByteArray As Byte() = System.IO.File.ReadAllBytes(Filename)
Dim DES As New DESCryptoServiceProvider()
Dim MS As New MemoryStream()
DES.Key() = ASCIIEncoding.ASCII.GetBytes(Key)
DES.IV = ASCIIEncoding.ASCII.GetBytes(Key)
Dim DESDecrypt As ICryptoTransform = DES.CreateDecryptor()
Dim CryptoStream As New CryptoStream(MS, DESDecrypt, CryptoStreamMode.Write)
CryptoStream.Write(ByteArray, 0, ByteArray.Length)
CryptoStream.Close()
Dim DecryptedByte As Byte() = MS.ToArray
Return DecryptedByte
End Function
Private Sub RunDecryptedApp(ByVal [Data] As Byte())
Dim Assembly As System.Reflection.Assembly = System.Reflection.Assembly.Load([Data])
Dim Method As MethodInfo = Assembly.EntryPoint
If Method IsNot Nothing Then
Dim ObjectIdentifier As Object = Assembly.CreateInstance(Method.Name)
Method.Invoke(ObjectIdentifier, Nothing)
Else
Application.Exit()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
EncryptFile("C:UsersAdministratorDesktopDemo.txt", GenerateKey)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Thread = New System.Threading.Thread(AddressOf RunDecryptedAppThread)
Thread.Start()
End Sub
Private Sub RunDecryptedAppThread()
RunDecryptedApp(DecryptFileAsByte("C:UsersAdministratorDesktopDemo.txt", GenerateKey))
End Sub
End Class
The error is "Bad Data" at line:
CryptoStream.Close()
And then "Could not load file or assembly 8 bytes loaded from WindowsApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null or one of its dependencies. An attempt was made to load a program with an incorrect format.
This was while testing on a .txt document. I will be actually doing this on a .NET exe file, so please help on that end too, if need be.
Thank you for your time!
View the full article
http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/beefbf08-0497-40f1-b258-ba09afc49cb7/
The topic was locked, so I had to make a new thread here. I literally copied + pasted the code from the original post and just renamed things so I could understand them better, but I cant launch the code without receiving an error.
Public Class Login
Dim Thread As System.Threading.Thread
Private Function GenerateKey() As String
Dim DESCrypto As DESCryptoServiceProvider = CType(DESCryptoServiceProvider.Create(), DESCryptoServiceProvider)
Return ASCIIEncoding.ASCII.GetString(DESCrypto.Key)
End Function
Private Sub EncryptFile(ByVal Filename As String, ByVal Key As String)
Dim ByteArray As Byte() = System.IO.File.ReadAllBytes(Filename)
Dim DES As New DESCryptoServiceProvider()
Dim MS As New MemoryStream()
DES.Key = ASCIIEncoding.ASCII.GetBytes(Key)
DES.IV = ASCIIEncoding.ASCII.GetBytes(Key)
Dim DESEncrypt As ICryptoTransform = DES.CreateEncryptor()
Dim CryptoStream As New CryptoStream(MS, DESEncrypt, CryptoStreamMode.Write)
CryptoStream.Write(ByteArray, 0, ByteArray.Length)
CryptoStream.Close()
Dim EncryptedData As Byte() = MS.ToArray()
System.IO.File.WriteAllBytes(Filename, EncryptedData)
End Sub
Private Function DecryptFileAsByte(ByVal Filename As String, ByVal Key As String) As Byte()
Dim ByteArray As Byte() = System.IO.File.ReadAllBytes(Filename)
Dim DES As New DESCryptoServiceProvider()
Dim MS As New MemoryStream()
DES.Key() = ASCIIEncoding.ASCII.GetBytes(Key)
DES.IV = ASCIIEncoding.ASCII.GetBytes(Key)
Dim DESDecrypt As ICryptoTransform = DES.CreateDecryptor()
Dim CryptoStream As New CryptoStream(MS, DESDecrypt, CryptoStreamMode.Write)
CryptoStream.Write(ByteArray, 0, ByteArray.Length)
CryptoStream.Close()
Dim DecryptedByte As Byte() = MS.ToArray
Return DecryptedByte
End Function
Private Sub RunDecryptedApp(ByVal [Data] As Byte())
Dim Assembly As System.Reflection.Assembly = System.Reflection.Assembly.Load([Data])
Dim Method As MethodInfo = Assembly.EntryPoint
If Method IsNot Nothing Then
Dim ObjectIdentifier As Object = Assembly.CreateInstance(Method.Name)
Method.Invoke(ObjectIdentifier, Nothing)
Else
Application.Exit()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
EncryptFile("C:UsersAdministratorDesktopDemo.txt", GenerateKey)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Thread = New System.Threading.Thread(AddressOf RunDecryptedAppThread)
Thread.Start()
End Sub
Private Sub RunDecryptedAppThread()
RunDecryptedApp(DecryptFileAsByte("C:UsersAdministratorDesktopDemo.txt", GenerateKey))
End Sub
End Class
The error is "Bad Data" at line:
CryptoStream.Close()
And then "Could not load file or assembly 8 bytes loaded from WindowsApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null or one of its dependencies. An attempt was made to load a program with an incorrect format.
This was while testing on a .txt document. I will be actually doing this on a .NET exe file, so please help on that end too, if need be.
Thank you for your time!
View the full article