Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have a noob problem. From the code below you will see what I'm trying to do. I'm running into a problem though. The "Hold" string gets corrupted as I add data to it. I'm sure I'm missing something elementary and someone will spot it right away. Thanks in advance for any help.


VB
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim T As Byte = 0                                           'Text byte.
    Dim Hold As String = ""                                     'Holding string for final output.
    Dim CryptKey As String = "DefaultCryptKey1234567890"        'CryptKey.
    Dim K As Integer = 0                                        'CryptKey byte.
    Dim KeyPosition As Integer = 1                              'Current position in the Cryptkey.
    If TextBox2.Text <> "" Then CryptKey = TextBox2.Text        'Use default CryptKey if none entered.
    For LoopCount = 1 To Len(TextBox1.Text)                     'Main loop.
        If KeyPosition >= Len(CryptKey) Then KeyPosition = 1    'If end of CryptKey is reached, go to start again.
        K = Asc(Mid(CryptKey, KeyPosition, 1))                  'Get a new cryptkey character.
        T = Asc(Mid(TextBox1.Text, LoopCount, 1))               'Get a new text character.
        Hold += Chr(T Xor K)                                    'XOR them and add to Hold string.
        KeyPosition += 1                                        'Increment the character position in CryptKey.
    Next                                                        'All characters done?
    TextBox1.Text = Hold                                        'Dump Hold string into Textbox.
End Sub
Posted
Comments
CPallini 19-Feb-15 12:08pm    
What do you mean with 'gets corrupted'? I mean you cannot expect nice characters after applying the xor.
Member 4768218 19-Feb-15 12:52pm    
I understand that. I would like to be able to view the "Hold" string in a text box like I'm able to, for instance, view a .exe file in Windows Notepad. Hope that clarifies a bit.

1 solution

Hold doesn't get corrupted - it gets values you can't read.
Not every possible Unicode character is readable: they don;t stop at just A..z, 0..9, and some punctuation. And when you start XORing characters together, you are very, very unlikely to get a "readable" character as a result: A XOR B is a number: 3, which is not readable at all!

If you are trying to encrypt things, (and this is a pretty basic encryption mechanism) then you want to look a output Byte values instead of Char - they really aren't readable and aren't intended to be! (XOR and characters can get very dangerous, as Unicode characters aren't a fixed length: you can get one byte characters, and multibyte. If you use Char all the time, you are very likely to find that you can't decrypt your data again in some cases.
 
Share this answer
 
Comments
Member 4768218 19-Feb-15 13:09pm    
Thanks for the advice. Seems like I need to be working with byte instead of char. I'll look into it. See my reply to CPallini's comment above for more details.

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