Results 1 to 4 of 4

Thread: [RESOLVED] How to merge these 2 into one program?

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    36

    Resolved [RESOLVED] How to merge these 2 into one program?

    First is a windows form application:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim ds As New DataSet
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         ds.ReadXml("DeExample za net.xml")
    7.         DataGridView1.DataSource = ds.Tables(0)
    8.         DataGridView1.ReadOnly = True
    9.  
    10.         Dim xml As XDocument = XDocument.Load("Example za net.xml")
    11.         Dim list1 As List(Of String) = (From node In xml...<Fuel_Cons> Select StrConv(node.Value, VbStrConv.ProperCase)).Distinct.ToList
    12.         list1.Insert(0, "*")
    13.         ComboBox1.DataSource = list1
    14.         Dim list2 As List(Of String) = (From node In xml...<Weather> Select StrConv(node.Value, VbStrConv.ProperCase)).Distinct.ToList
    15.         list2.Insert(0, "*")
    16.         ComboBox2.DataSource = list2
    17.     End Sub
    18.  
    19. End Class

    Second is a windows console application:

    vb Code:
    1. Imports System
    2. Imports System.IO
    3. Imports System.Security
    4. Imports System.Security.Cryptography
    5. Imports System.Runtime.InteropServices
    6. Imports System.Text
    7.  
    8.  
    9. Module Module1
    10.  
    11.     ' Call this function to remove the key from memory after it is used for security.
    12.     <DllImport("kernel32.dll")> _
    13.     Public Sub ZeroMemory(ByVal addr As IntPtr, ByVal size As Integer)
    14.     End Sub
    15.  
    16.     Sub DecryptFile(ByVal sInputFilename As String, _
    17.         ByVal sOutputFilename As String, _
    18.         ByVal sKey As String)
    19.  
    20.         Dim DES As New DESCryptoServiceProvider()
    21.         'A 64-bit key and an IV are required for this provider.
    22.         'Set the secret key for the DES algorithm.
    23.         DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey)
    24.         'Set the initialization vector.
    25.         DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
    26.  
    27.         'Create the file stream to read the encrypted file back.
    28.         Dim fsread As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read)
    29.         'Create the DES decryptor from the DES instance.
    30.         Dim desdecrypt As ICryptoTransform = DES.CreateDecryptor()
    31.         'Create the crypto stream set to read and to do a DES decryption transform on incoming bytes.
    32.         Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read)
    33.         'Print out the contents of the decrypted file.
    34.         Dim fsDecrypted As New StreamWriter(sOutputFilename)
    35.         fsDecrypted.Write(New StreamReader(cryptostreamDecr).ReadToEnd)
    36.         fsDecrypted.Flush()
    37.         fsDecrypted.Close()
    38.     End Sub
    39.  
    40.     Public Sub Main()
    41.         'Must be 64 bits, 8 bytes.
    42.         Dim sSecretKey As String
    43.  
    44.         ' Get the key for the file to encrypt.
    45.         ' You can distribute this key to the user who will decrypt the file.
    46.         sSecretKey = "12345678"
    47.  
    48.         ' For additional security, pin the key.
    49.         Dim gch As GCHandle = GCHandle.Alloc(sSecretKey, GCHandleType.Pinned)
    50.  
    51.         ' Decrypt the file.
    52.         DecryptFile("\ENExample za net.xml", _
    53.                     "\DeExample za net.xml", _
    54.                     sSecretKey)
    55.  
    56.         ' Remove the key from memory.
    57.         ZeroMemory(gch.AddrOfPinnedObject(), sSecretKey.Length * 2)
    58.         gch.Free()
    59.     End Sub
    60.  
    61. 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?

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    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.

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    36

    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.

  4. #4
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width