C# Recommendations for the best way to write text to a file such that if opened in notepad the user wont understand it
Looking for recommendations for a simple way to write a string to a file so that the resulting file would not be readable/editable 开发者_开发百科in Notepad. I thought about using some serious encryption, but I think that is just over kill for what I need. Just looking for a easy unreadable text format to convert to and from.
You could Base64 encode it:
string text = "My quasi secret text.";
byte[] buffer = System.Text.UTF8Encoding.GetBytes(text);
string protectedText = Convert.ToBase64String(buffer);
File.WriteAllText(filename, protectedText)
EDIT: Updated to UTF8Encoding to handle unicode.
How about using a binary formatter to serialize it?
That would pretty well ensure that people opening the file in Notepad would just end up with gobbledegook. But (it had to be said) if you attach any importance to people not being able to see or mess with this data, it needs to be truly encrypted.
Google translate it into French then translate it back. This won't work if your users speak French.
If it doesn't need to be encrypted, then I would suggest Convert.ToBase64String/Convert.FromBase64String.
Simple and reliable.
encryption, or maybe encoding (e.g. Base64 encoding? - or even a simpler one?). This would make it non readable, but still editable.
Something super simple would be Base64 encoding:
Convert.ToBase64String();
Encryption: to put (a message) into code or to distort so that it cannot be understood without the appropriate decryption equipment
By definition, you are trying to encrypt the data. The only difference is you only need an encryption that's strong enough to not allow laymen from accessing it's contents.
Like others have mentioned, using Base64 would effectively encrypt the data against your average user.
So in the end here is what I did.
TO Write the data:
//Write the text in a way the user wont recognize if they open the file in notepad.
byte[] buffer = System.Text.ASCIIEncoding.UTF8.GetBytes(data);
string protectedText = Convert.ToBase64String(buffer);
File.WriteAllText(UserToUserLevelDataFile, protectedText);
TO Read the data:
string longText = File.ReadAllText(UserToUserLevelDataFile);
data = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(longText));
Works like a charm, thanks everyone for your ideas!
精彩评论