[RESOLVED] How to merge these 2 into one program?
First is a windows form application:
vb Code:
Public Class Form1
Dim ds As New DataSet
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ds.ReadXml("DeExample za net.xml")
DataGridView1.DataSource = ds.Tables(0)
DataGridView1.ReadOnly = True
Dim xml As XDocument = XDocument.Load("Example za net.xml")
Dim list1 As List(Of String) = (From node In xml...<Fuel_Cons> Select StrConv(node.Value, VbStrConv.ProperCase)).Distinct.ToList
list1.Insert(0, "*")
ComboBox1.DataSource = list1
Dim list2 As List(Of String) = (From node In xml...<Weather> Select StrConv(node.Value, VbStrConv.ProperCase)).Distinct.ToList
list2.Insert(0, "*")
ComboBox2.DataSource = list2
End Sub
End Class
Second is a windows console application:
vb Code:
Imports System
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography
Imports System.Runtime.InteropServices
Imports System.Text
Module Module1
' Call this function to remove the key from memory after it is used for security.
<DllImport("kernel32.dll")> _
Public Sub ZeroMemory(ByVal addr As IntPtr, ByVal size As Integer)
End Sub
Sub DecryptFile(ByVal sInputFilename As String, _
ByVal sOutputFilename As String, _
ByVal sKey As String)
Dim DES As New DESCryptoServiceProvider()
'A 64-bit key and an IV are required for this provider.
'Set the secret key for the DES algorithm.
DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey)
'Set the initialization vector.
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
'Create the file stream to read the encrypted file back.
Dim fsread As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read)
'Create the DES decryptor from the DES instance.
Dim desdecrypt As ICryptoTransform = DES.CreateDecryptor()
'Create the crypto stream set to read and to do a DES decryption transform on incoming bytes.
Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read)
'Print out the contents of the decrypted file.
Dim fsDecrypted As New StreamWriter(sOutputFilename)
fsDecrypted.Write(New StreamReader(cryptostreamDecr).ReadToEnd)
fsDecrypted.Flush()
fsDecrypted.Close()
End Sub
Public Sub Main()
'Must be 64 bits, 8 bytes.
Dim sSecretKey As String
' Get the key for the file to encrypt.
' You can distribute this key to the user who will decrypt the file.
sSecretKey = "12345678"
' For additional security, pin the key.
Dim gch As GCHandle = GCHandle.Alloc(sSecretKey, GCHandleType.Pinned)
' Decrypt the file.
DecryptFile("\ENExample za net.xml", _
"\DeExample za net.xml", _
sSecretKey)
' Remove the key from memory.
ZeroMemory(gch.AddrOfPinnedObject(), sSecretKey.Length * 2)
gch.Free()
End Sub
End Module
Now is there a way to merge this into one application? So that the second application doesnt have to have a file as the output, but that the output, (the decoded file) can be stored in local memory or something and be read by first program?
Re: How to merge these 2 into one program?
The simplest way is to create a class out of the second program and add it to the first one with minimal changes. You can use IO.MemoryStream class instead of IO.FileStream. And as far as I know cryptoproviders of the System.Security namespace can work not only with streams but with arrays also.
If you want to keep these two programs separate you can use named pipes for communications between them.
Re: How to merge these 2 into one program?
can you explain me how to do the first thing in a more detailed way i tryed adding a module (by going to projects add module) but i have no idea on how to call it, nor how to edit it, i know this is way over my head, but i still want to do it no matter how long it takes me to.
Re: How to merge these 2 into one program?
Add a new class to your first program
Provide some nice name to this class
Copy & Paste Imports statements to a new class
Copy & Paste Subs from your second program to the class.
Change ZeroMemory declaration to Public SharedSub ...
Change Public Sub Main() to Private Sub PinSecretKey()
Note: This is futile, because if I open your executable in Reflector your 'secret' key will be right there in the open.
Now change your Sub DecryptFile into a Function which takes an IO.Stream as a parameter (encrypted stream) and returns IO.Stream (decrypted stream)
Also, I had a demo of TripleDES which illustrates the process of encrypting/decrypting which you may find useful. Also, I suggest using AES since it's considered to be more secure.
Here's a link http://www.vbforums.com/showpost.php...80&postcount=2