Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# strings
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 22-Sep-12 2:53am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

It's a pretty primitive form of "Encryption" - there are a lot betters ones built into .NET. But it's pretty simple...
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);
  Permalink  
Comments
Toli Cuturicu at 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)

  Print Answers RSS
0 OriginalGriff 341
1 Marcin Kozub 225
2 Praneet Nadkar 197
3 Sergey Alexandrovich Kryukov 190
4 Shweta N Mishra 161
0 OriginalGriff 8,149
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 22 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100