字符串被破坏了。 [英] String gets corrupted.

查看:84
本文介绍了字符串被破坏了。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好b $ b

我有一个菜鸟问题。从下面的代码中,您将看到我正在尝试做的事情。我遇到了一个问题。当我向其添加数据时,Hold字符串被破坏。我确定我错过了一些基本的东西,有人会马上发现它。预先感谢您的帮助。





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.


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

推荐答案

保持不会被破坏 - 它会获得您无法读取的值。

并非每个可能的Unicode字符都是可读的:它们不会停留在A..z ,0..9,以及一些标点符号。当你开始对字符进行异或时,你很可能不会得到一个可读的字符:XOR B是一个数字:3,根本不可读!



如果你正在尝试加密东西,(这是一个非常基本的加密机制)那么你想看看输出字节值而不是Char - 它们实际上是不可读的,并不打算是! (XOR和字符可能变得非常危险,因为Unicode字符不是固定长度:你可以获得一个字节字符和多字节。如果你一直使用Char,你很可能会发现你无法解密你的在某些情况下再次提供数据。
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.


这篇关于字符串被破坏了。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆