2 Replies - 645 Views - Last Post: 01 March 2010 - 01:00 PM Rate Topic: -----

#1 zaqwertyyuio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-February 10

Why won't this code work

Posted 27 February 2010 - 12:39 PM

Why Wont This Code Work?

I Have a password safe program that allows you to enter various passwords from different websites and adds them to a password protected list which then saves them to a file and then the file gets encrypted then when the program starts it decrypts the file and loads the list into the password protected listbox

Overall a very hard program to hack but there is a problem but i dont know what it is.
I Am Using the DES Cryptography module and no problems show up on the normal panel
I Will Post all my code below:
My Code
Imports System
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography
Imports System.Runtime.InteropServices
Imports System.Text
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text = "q#w/e'" Then
            Panel1.Visible = False
            Button6.Visible = True
        Else
            MsgBox("incorect")
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        ListBox1.Items.Add(TextBox2.Text)
        ListBox1.SelectedIndex = ListBox1.SelectedIndex + 1

        ListBox2.Items.Add(TextBox3.Text)
        ListBox2.SelectedIndex = ListBox2.SelectedIndex + 1
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        TextBox2.Text = ""
        TextBox3.Text = ""
    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Panel1.Visible = True
        Button6.Visible = False
    End Sub
    Dim w As IO.StreamWriter
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim i As Integer
        w = New IO.StreamWriter("c:\Users\Jack\sites.txt")
        For i = 0 To ListBox1.Items.Count - 1
            w.WriteLine(ListBox1.Items.Item(i))
        Next
        EncryptFile("c:\Users\Jack\sites.txt", "c:\Users\Jack\sites2.txt", "Hello")

        w.Close()
        Dim i2 As Integer
        w = New IO.StreamWriter("c:\Users\Jack\Passwords.txt")
        For i = 0 To ListBox2.Items.Count - 1
            w.WriteLine(ListBox2.Items.Item(i2))
        Next

        EncryptFile("c:\Users\Jack\Passwords.txt", "c:\Users\Jack\passwords2.txt", "Hello")
        w.Close()

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loadfiles()
    End Sub
    Dim r As IO.StreamReader

    Sub loadfiles()
        DecryptFile("c:\Users\Jack\sites2.txt", "c:\Users\Jack\sites.txt", "Hello")
        r = New IO.StreamReader("C:\Users\Jack\sites.txt")
        While (r.Peek > -1)
            ListBox1.Items.Add(r.ReadLine)
        End While
        r.Close()
        EncryptFile("c:\Users\Jack\Passwords2.txt", "c:\Users\Jack\passwords.txt", "Hello")
        r = New IO.StreamReader("C:\Users\Jack\Passwords.txt")
        While (r.Peek > -1)
            ListBox2.Items.Add(r.ReadLine)
        End While
        r.Close()
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        ListBox1.Items.Clear()
        ListBox2.Items.Clear()

    End Sub

End Class


And the cryptography module:
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

    ' Function to generate a 64-bit key.
    Function GenerateKey() As String
        ' Create an instance of a symmetric algorithm. The key and the IV are generated automatically.
        Dim desCrypto As DESCryptoServiceProvider = DESCryptoServiceProvider.Create()

        ' Use the automatically generated key for encryption. 
        Return ASCIIEncoding.ASCII.GetString(desCrypto.Key)

    End Function

    Sub EncryptFile(ByVal sInputFilename As String, _
                   ByVal sOutputFilename As String, _
                   ByVal sKey As String)

        Dim fsInput As New FileStream(sInputFilename, _
                                    FileMode.Open, FileAccess.Read)
        Dim fsEncrypted As New FileStream(sOutputFilename, _
                                    FileMode.Create, FileAccess.Write)

        Dim DES As New DESCryptoServiceProvider()

        'Set secret key for DES algorithm.
        'A 64-bit key and an IV are required for this provider.
        DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey)

        'Set the initialization vector.
        DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)

        'Create the DES encryptor from this instance.
        Dim desencrypt As ICryptoTransform = DES.CreateEncryptor()
        'Create the crypto stream that transforms the file stream by using DES encryption.
        Dim cryptostream As New CryptoStream(fsEncrypted, _
                                            desencrypt, _
                                            CryptoStreamMode.Write)

        'Read the file text to the byte array.
        Dim bytearrayinput(fsInput.Length - 1) As Byte
        fsInput.Read(bytearrayinput, 0, bytearrayinput.Length)
        'Write out the DES encrypted file.
        cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)
        cryptostream.Close()
    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 = GenerateKey()

        ' For additional security, pin the key.
        Dim gch As GCHandle = GCHandle.Alloc(sSecretKey, GCHandleType.Pinned)


        ' Encrypt the file.        
        EncryptFile("%USERPROFILE%\MyData.txt", _
                        "%USERPROFILE%\Encrypted.txt", _
                        sSecretKey)

        ' Decrypt the file.
        DecryptFile("%USERPROFILE%\Encrypted.txt", _
                    "%USERPROFILE%\Decrypted.txt", _
                    sSecretKey)

        ' Remove the key from memory. 
        ZeroMemory(gch.AddrOfPinnedObject(), sSecretKey.Length * 2)
        gch.Free()
    End Sub

End Module




The add Feature and password protection works fine but when i try to save the list to a text file and encrypt it I Get This Error in the encryptfile() function:
Dim fsInput As New FileStream(sInputFilename, _
                                    FileMode.Open, FileAccess.Read) 


IS HIGHLIGHTED

AND I Get The Error Message:
IOException was unhandled
The process cannot access the file 'c:\Users\Jack\sites.txt' because it is being used by another process.


This post has been edited by zaqwertyyuio: 28 February 2010 - 02:16 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Why won't this code work

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Why won't this code work

Posted 27 February 2010 - 12:42 PM

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple

items that are vital in order for someone to properly help you:


[list]
[*] Post the code you're having problems with (DONE)
[*] Post the exact error you're receiving, if you are receiving one
[*] If no error explain what the code is doing versus what you want it to do
[*] Post your question in the body of your post, not the description field
[/list
Was This Post Helpful? 0
  • +
  • -

#3 Asscotte  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 35
  • View blog
  • Posts: 610
  • Joined: 08-February 09

Re: Why won't this code work

Posted 01 March 2010 - 01:00 PM

If you open a file you always have to close it again, or you will get this error, use fsInput.close.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1