Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
My current project(home) has a windows form with a textbox and a button in it. The user hasto enter a string in the textbox (say his name). This value is stored in a variable "string textdata;". Now I've to write this data into a text file "Log.txt".

For this, i'm using "File.WriteAllBytes(textdata)". Using another form, the file "Log.txt" will be accessed and the string values are displayed in a multiline textbox. This was going fine until I got an idea of encrypting the data in the Log.txt file. For this, I want to perform one's complement on the string variable and write the complemented data into the file and the same must be read from another form and proper string must be displayed in the text box.

I'll explain the logic in detail:

1> Form_Read is a form used to read the "log.txt" file and display the string in the file in a text box.
2> Form_Write is a form used to get the username and write it into the "log.txt" file.

My intention is:

1> Before writing the string data into "log.txt", it must be complemented i.e., if 1001 is to be written on the file, it must be written as 0110 so that the file data will not be visible in normal way.
2> Before displaying the contents of "log.txt" in text box of another form, it must be complemented once again so that the normal text will be displayed in the text box.

How Can I do this???

I've tried using byte[] variable= Encoding.ASCII.GetBytes(data);
but the output in either case is just a normal integer values and the proper text is not being displayed. Please give a proper solution..

Thanks in advance
Posted

1 solution

It's a pretty primitive form of "Encryption" - there are a lot betters ones built into .NET. But it's pretty simple...
C#
string inp = "Hello there";
byte[] data = Encoding.ASCII.GetBytes(inp);
for (int i = 0; i < data.Length; i++)
    {
    data[i] ^= 0xFF;
    }
//... Save and then load
for (int i = 0; i < data.Length; i++)
    {
    data[i] ^= 0xFF;
    }
string outp = System.Text.Encoding.ASCII.GetString(data);
 
Share this answer
 
Comments
Toli Cuturicu 22-Sep-12 17:59pm    
I think I could be able to "decrypt" such a string if I find one ;-)
My 5, of course.

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