9 Replies - 1091 Views - Last Post: 16 August 2011 - 09:35 PM Rate Topic: -----

#1 NY Andrew  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 79
  • Joined: 25-May 11

Help Decrypting and checking RegistryKey value on form startup

Posted 16 August 2011 - 04:49 PM

Alright so I have spent an entire day and am getting nowhere. I have encrypted a user inputted value and successfully stored it in the Windows Registry. Now I am having problems Decrypting that RegistryKey when my Form starts up and making sure it is the same key that was originally stored. My decrypting code has been working but if I go inside the Registry and add an extra character on to that keys value, my program is still running.. It is not supposed to run if that RegistryKey is not the one that was originally user inputted.. :helpsmilie:

Some Encrypting code I am using.
    Public Function EncryptKey(ByVal strKey As String)
        Dim ch As Char
        Dim intCh As Integer
        Dim tmp As String = Nothing
        Dim i As Integer
        For i = 0 To Len(strKey) - 1
            intCh = Asc(strKey.Chars(i))
            If (intCh < 245) Then
                intCh = intCh + 10
            Else
                intCh = 255 - intCh
            End If
            ch = Chr(intCh)
            tmp = tmp & ch
        Next
        Return tmp
    End Function

    Public StoredKey As String

Dim key As Long
My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\myprogram", "key", EncryptKey(Trim(key)))
            StoredKey = Trim(key)

Public StoredKey As String

    Public Function DecryptPassword(ByVal strKey As String)
        Dim ch As Char
        Dim intCh As Integer
        Dim tmp As String = Nothing
        Dim i As Integer
        For i = 0 To Len(strKey) - 1
            intCh = Asc(strKey.Chars(i))
            If (intCh > 10) Then
                intCh = intCh - 10
            Else
                intCh = 255 - intCh
            End If
            ch = Chr(intCh)
            tmp = tmp & ch
        Next
        Return tmp
    End Function

    Dim key As Long



Under the Forms Load even I have tried:
        If (My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\myprogram", "key", Nothing) Is Nothing) Then
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\myprogram", "key", "")
            StoredKey = ""
        Else
            StoredKey = DecryptPassword(CStr(My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\myprogram", "key", Nothing)))
        End If

        If (StrComp(key, StoredKey) = 0) Then
            MsgBox("Registry key NOT added, already exists.")
        Else
            RegisterForm.Show()
        End If


And I also have tried A LOT of other examples, but I am not going too post them all. So is there anyway I can check if a RegistryKey exists and Decrypt it to make sure that it has not been changed on my programs startup?

Thanks for your help. After a full day of nothing working out this is my last hope, before I just throw in the towel on this entire concept..
Updated to show more code.

This post has been edited by NY Andrew: 16 August 2011 - 05:10 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Help Decrypting and checking RegistryKey value on form startup

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9213
  • View blog
  • Posts: 34,599
  • Joined: 12-June 08

Re: Help Decrypting and checking RegistryKey value on form startup

Posted 16 August 2011 - 05:42 PM

There's nothing there to indicate the form should close. It runs the checks, makes a passing "meh" that they are not matching, and continues on with the application. Maybe throw a "me.close" after you informing the user the encrypted strings don't match. You know so the form will not continue with the rest of the code and infact short circuits to prevent access.
Was This Post Helpful? 0
  • +
  • -

#3 NY Andrew  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 79
  • Joined: 25-May 11

Re: Help Decrypting and checking RegistryKey value on form startup

Posted 16 August 2011 - 06:22 PM

Well if the strings don't match, I have the code open up the RegisterForm. I think my problem is within the form load event.
I have tried this on its own and it did not work:
If (StrComp(key, StoredKey) = 0) Then
MsgBox("Registry key NOT added, already exists.")
Else
RegisterForm.Show()
End If

And then I was working with this on its own:
If (My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\myprogram", "key", Nothing) Is Nothing) Then
My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\myprogram", "key", "")
StoredKey = ""
Else
StoredKey = DecryptPassword(CStr(My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\myprogram", "key", Nothing)))
End If

But I am having no luck so I tried adding them both and STILL not working.. I keep thinking I might need to merge those two together somehow.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9213
  • View blog
  • Posts: 34,599
  • Joined: 12-June 08

Re: Help Decrypting and checking RegistryKey value on form startup

Posted 16 August 2011 - 06:52 PM

Your logic isn't adding up.

You have some 'mainform'... in it's load event you check for this reg key. It it doesn't have it it opens a new form called RegisterForm... which presumably allows for a proper registry key to be set.

Questions - what happens if the user closes the RegisterForm before setting a key? Is the application as a whole supposed to close? Dance a funny dance?

Why are you just doing a 'show' on RegisterForm? The rest of the load event for the main form goes off because show should probably be the modal call for a dialog box.
Was This Post Helpful? 0
  • +
  • -

#5 NY Andrew  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 79
  • Joined: 25-May 11

Re: Help Decrypting and checking RegistryKey value on form startup

Posted 16 August 2011 - 06:58 PM

This is what happens when the RegisterForm is closed:
    Private Sub RegisterForm_Closing(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Closing

        Dim msgbox As DialogResult
        msgbox = MessageBox.Show _
                ("This software is not activated on this computer yet and will not work.", _
                "Software not activated!", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
        MainForm.Close()
        Application.Exit()

    End Sub

In the Project Properties window I have MainForm set as the startup form and so when the key has been verified then it just continues on the to the main form without having to call MainForm.Show()
I have still been messing around with this and no luck. I think I'd be better off starting from scratch somehow..
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9213
  • View blog
  • Posts: 34,599
  • Joined: 12-June 08

Re: Help Decrypting and checking RegistryKey value on form startup

Posted 16 August 2011 - 07:04 PM

Christ... the amount of OO issues is physically painful.

Okay - forms... they are objects and treat them as such. Just calling the form's name as short cut removes any sort of flow of operations.

declare a new instance of your RegisterForm when you need it to show up..

dim foo as new RegisterForm

foo.showdialog


Then.. oh then you can utilize how the form was closed. Try slapping the show dialog in a select/case statement. You can interact with the multiple ways a form is closed. Encapsulate your logic in each form... black box it up so the objects have a descent path so you are not getting random crap happening.

        Select Case foo.ShowDialog
            Case Windows.Forms.DialogResult.OK
'-- code here
            Case Windows.Forms.DialogResult.None
'-- code here
            Case Windows.Forms.DialogResult.Abort
'-- code here

'-- .... 
        End Select


Since your registerform isn't the parent of your main form there's no reason to call close on the mainfrom from inside the regform.

The reason your code isn't working logically is because you don't have a clear chain of events for it to happen - this goes back to learning the OO concepts.

Go read up on how objects are supposed to interact and all that jazz.
Was This Post Helpful? 0
  • +
  • -

#7 NY Andrew  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 79
  • Joined: 25-May 11

Re: Help Decrypting and checking RegistryKey value on form startup

Posted 16 August 2011 - 07:14 PM

As I work with what you are saying I am just going to throw this out there. The problem I seem to be having is that if I go into the Registry and edit that key, when the program starts up it still works. It's like it doesn't know what the original key was. Maybe I need to store 2 keys in the registry or something so then it will know what the key is.. I have no idea anymore..
Was This Post Helpful? 0
  • +
  • -

#8 NY Andrew  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 79
  • Joined: 25-May 11

Re: Help Decrypting and checking RegistryKey value on form startup

Posted 16 August 2011 - 09:24 PM

Thank you all for your help, I have successfully solved this issue on my own. I spent the entire day switching things around and eventually ended up with a combination that works.
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9213
  • View blog
  • Posts: 34,599
  • Joined: 12-June 08

Re: Help Decrypting and checking RegistryKey value on form startup

Posted 16 August 2011 - 09:32 PM

I'm not going to lie - but randomly swapping code in the dark seems like a horribly inefficient way of writing code.

... but congrats I guess on solving your issue.
Was This Post Helpful? 0
  • +
  • -

#10 NY Andrew  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 79
  • Joined: 25-May 11

Re: Help Decrypting and checking RegistryKey value on form startup

Posted 16 August 2011 - 09:35 PM

View Postmodi123_1, on 17 August 2011 - 12:32 AM, said:

I'm not going to lie - but randomly swapping code in the dark seems like a horribly inefficient way of writing code.

... but congrats I guess on solving your issue.

Haha it truly is, but after spending an entire day on getting nowhere, you'll start doing anything to get it to work. :rolleyes:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1