简体   繁体   中英

TextBox text and xor filter

my problem today in C#. I make xor crypt for my text lines, and want make for it generator, but my TextBox with source text return diffential string and result is not true.

Xor function:

private string GetText(byte[] Text)
{
    byte[] Key = { 0x12, 0x05, 0x52 };
    // ----
    for (int i = 0; i < Text.Length; i++)
    {
        Text[i] ^= Key[i % 3];
    }
    // ----
    return Encoding.ASCII.GetString(Text);
}

True result:

string Text = ".\\MyExample.txt";
textBox2.Text = GetText(Encoding.ASCII.GetBytes(Text)); //Result: <Yk@*sh"~`|f}&

False result:

string Text = textBox1.Text; //Text: ".\\MyExample.txt"
textBox2.Text = GetText(Encoding.ASCII.GetBytes(Text)); //Result: <Y_|jd?bi7<q*f

Why i get different results and how it fix?

Your code contains an escaped backslash. C# is converting \\\\ to a single \\ :

string Text = ".\\MyExample.txt";  // Text == ".\MyExample.txt"

If you want Text to contain \\\\ , use one of the following methods:

string Text = @".\\MyExample.txt";
string Text = ".\\\\MyExample.txt";

The text you are defining in the first sample is not the same as the text coming from the TextBox.

string Text = ".\\MyExample.txt";
string TextBoxText = textBox1.Text; // is actually ".\\\\MyExample.txt"

The \\ is an escape character, thus if you were to display ".\\\\MyExample.txt" in your TextBox, you would see .\\MyExample.txt .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM