Simple encryption style gone wrong!!

drew4663

Well-known member
Joined
May 3, 2007
Messages
62
Programming Experience
1-3
Background Story:
I have a pop up screen that validates a registration file. This registration file is a text file using my custom file extension. The registration file contains info the user has entered in but it is encrypted and then sent to my web server where the info is validated every time the program loads.

The Excuse:
I am a complete novice and trying to fill some big coding gaps so please excuse my, most likely incorrect, long about way of coding.

The Forms:
Form 1
Two textboxes
Two buttons - Encrypts

Form 2
Two textboxes
One button - Decrypt

The Process:
In textbox1 you enter the password and click the "Commit" button. This disables changing the textbox values. Textbox2 is also populated with the "encrypted" value. The second button saves the result and is saved as a custom file.

The Problem(s)

The value in textbox2(encrypted) is going in sequential order.
Also, if I type the same letter twice it only accounts for 1 letter.

The Code and Example
VB.NET:
dim a as string = "a"
dim apass as string = "001"
dim b as string = "b"
dim bpass as string = "002"
dim b as string = "c"
dim bpass as string = "003"

        If TextBox1.Text.Contains(a) Then
            TextBox2.AppendText(apass)
        End If
        If TextBox1.Text.Contains(b) Then
            TextBox2.AppendText(bpass)
        End If
        If TextBox1.Text.Contains(c) Then
            TextBox2.AppendText(cpass)
        End If


If I typed aaccabc the result would be "001002003" instead of "001001003003001002003"


Can someone give me some advice or let me know my options in this being successful? I searched and have read everything I know to look for. I am to the point where I am now unsure what I should be looking for or be changing. Sorry for being so winded. Any help wold be greatly appreciated. Thank you.


Andrew
 
Your logic is flawed in that Contains will return true only 1 time if there are 1 or more instances of the string you're passing in.

You'll want to loop through each character and make the substitution.

VB.NET:
For i As Integer = 0 To TextBox1.Text.Length - 1 Step 1
    Select Case TextBox1.Text.Substring(i, 1)
        Case "a"
            TextBox2.AppendText("001")
        Case "b"
            TextBox2.AppendText("002")
        Case "c"
            TextBox2.AppendText("003")
    End Select
Next

Another approach would be to use String.Replace since that's what you're looking to accomplish here.

VB.NET:
TextBox3.Text = TextBox1.Text.Replace("a", "001").Replace("b", "002").Replace("c", "003")
 
Your logic is flawed
Yes. Thank you for stating the obvious. I hope with the training courses, I will be taking to learn vb.net, my logic will no longer be flawed. With that being said...I thank you for your reply and your willingness to share your learnings with someone who is new.
I understand the second coding but the first one you gave may take me a while (on my own). Either way I appreciate you teaching me something new and I will try me best to take it even further and possibly be as generous as you one day and help someone too. Again, thanks.


Andrew
 
Yes. Thank you for stating the obvious. I hope with the training courses, I will be taking to learn vb.net, my logic will no longer be flawed. With that being said...I thank you for your reply and your willingness to share your learnings with someone who is new.
I understand the second coding but the first one you gave may take me a while (on my own). Either way I appreciate you teaching me something new and I will try me best to take it even further and possibly be as generous as you one day and help someone too. Again, thanks.


Andrew

The first one is a simple loop, looking at 1 letter at a time in the string and using the select case statement, it'll append the "001", "002", etc to the textbox's text property based on the letter being looked at.
 
The purpose of this board is not to belittle a lack of knowledge and I appologize if I phrased my response in a way that offended. I was merely trying to point out that the contains member function did something other than what you were expecting.
 
To JuggaloBrotha -

Thank you for your information.

To MattP - I appreciate your character to apologize but I wasn't upset at your statement. I agreed with you. It's obviously hard to tell people's true feelings sometimes looking at text so I thank you for letting me know your intentions.


Best of luck to you both and again thank you for sharing your wisdom.


Andrew
 
Back
Top