Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I'm trying to demonstrate easy encryption in my project. In this part, i'm supposed to replace each character in textbox1 to special characters assigned. But in this code the actual text gets replaced, I don't want that to happen,instead I want that to be shown in textbox2 retaining actual text in textbox1. How can I do that?

Here is the code:

VB
Private arLetterChars() As Char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 "
    Private arEncryptedChars() As Char = "¿ɸ¢ç¶‹!˜Ö´åÄÚᴁÍìæ¯~[Ûÿ|ÑûÏÉí³èô§ŠÀÙ:ÒÓ?>þ.äƒ%*š¾†±♠}@æŒ#/{۞],"
    '// encrypt.
    Private Sub Btnaddenc_Click(sender As System.Object, e As System.EventArgs) Handles BtnAddEnc.Click
        With Textbox1
            For Each myTextBoxChar As Char In Textbox1.Text '// loop thru TextBox, one char. at a time.
                For i As Integer = 0 To arLetterChars.Length - 1 '// loop thru all letters in the Array.
                    '// if TextBox char ='s the char in your Array, replace the TextBox char with the same #'ed Array char of the Encrypted letters.
                    If myTextBoxChar = arLetterChars(i) Then Textbox1.Text = Textbox1.Text.Replace(myTextBoxChar, arEncryptedChars(i))
                Next
            Next
        End With
    End Sub
Posted
Updated 22-Apr-14 1:04am
v2
Comments
Mike Meinz 22-Apr-14 6:48am    
Assign the value from Textbox1.Text into a local variable. Iterate through the local variable applying your algortithm. Then, assign the local variable to Tetbox2.Text.

1 solution

If you don't want to affect Textbox1, then don't assign to it...
Change
VB
If myTextBoxChar = arLetterChars(i) Then Textbox1.Text = Textbox1.Text.Replace(myTextBoxChar, arEncryptedChars(i))
To
VB
If myTextBoxChar = arLetterChars(i) Then Textbox2.Text = Textbox1.Text.Replace(myTextBoxChar, arEncryptedChars(i))
 
Share this answer
 
Comments
Rodney Mathew 22-Apr-14 7:28am    
I did what you said but not all characters got replaced... I tried to revert back to previous, and that works fine.
For eg: Mathew should be ?É*Àô† but it is mathe†
OriginalGriff 22-Apr-14 7:56am    
What I said above was to guide you - not to be a complete solution to your problem! :laugh:

Personally, I wouldn't do it quite the way you are anyway: it's very inefficient!
If you thing about it, each string is an array of characters, and you can convert from a string to a character array and vice versa.
So, what I would do is convert the input string to a char array, and loop through that using a For loop.
Find the character in the "Letters" table, and replace it in the array with the character from the "Encrypted" table.
Then at the end convert the character array back to a string, and put it to the textBox2.Text

If you use the built in method Array.IndexOf, it will save you having to search the "Letters" table yourself, as well...

Does that make sense?
Rodney Mathew 22-Apr-14 8:32am    
I tried it differently using your suggestion and now it's working! Thank you!
OriginalGriff 22-Apr-14 8:37am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900