0

When you open some file with txt editor the encoding type can not be read and its just a mess of characters, I want to use this with my program when saving a file and how to do this in c# with:

BinaryWriter bw = new BinaryWriter(File.Create(path), Encoder.SOME_ENCODING);

and then decode it when loading. So what encoding should I use for this?

8
  • 2
    Any encoding, so long as you both read and write using the same encoding. Commented Dec 20, 2012 at 20:48
  • You should use an unicode encoding, like UTF-8. Because it can encode all the characters used in all the languages today. Otherwise you get a bunch of ??????????????????? in the file in place of characters that couldn't be encoded in the target encoding. Commented Dec 20, 2012 at 20:52
  • Unless I misread this (and I may well have), I got the impression he is starting with a file of unknown encoding, and wants to determine the encoding so he can read and re-save it with the same encoding. (I could be way off base, but that's how I read the question) Commented Dec 20, 2012 at 20:53
  • @Esailija Unicode is 16 bit, UTF8 is 8 bit. They are not the same. You would have to use UTF16 to approximate Unicode. Commented Dec 20, 2012 at 21:00
  • 2
    You are thinking of Encryption, rather than Encoding, it seems. You want to be able to save data in some format that is readable essentially only by your program, because you know the decryption key. There are ways to do this, some of them even built in. I dont know them. Commented Dec 20, 2012 at 21:21

1 Answer 1

3

I want to save some string e.g "Sweet" to a file and if you open the file in a text edit you will see somthing like "nfgkdn@{3!"

Just a simple example

Obfuscate("a.txt", "hello");
string orgstr = Deobfuscate("a.txt");

Data in a.txt : Mj82NjU=

void Obfuscate(string fileName, string data)
{
    var bytes = Encoding.UTF8.GetBytes(data);
    for (int i = 0; i < bytes.Length; i++) bytes[i] ^= 0x5a;
    File.WriteAllText(fileName,Convert.ToBase64String(bytes));
}

string Deobfuscate(string fileName)
{
    var bytes = Convert.FromBase64String(File.ReadAllText(fileName));
    for (int i = 0; i < bytes.Length; i++) bytes[i] ^= 0x5a;
    return Encoding.UTF8.GetString(bytes);
}
Sign up to request clarification or add additional context in comments.

5 Comments

Effective. Encrypted though, not encoded. I believe this would do exactly what the user wanted. Something to be very careful though...this is based on the String DataType, and WILL cause OutOfMemory exceptions for any large piece of data. Its fine for small strings, but nearly useless for any large saveFile. I used a similar method in my own code and had to re-write it to use more memory sensative segments of data because the files were too large to fit into a single string object.
@Nevyn a) it is not Encryption(as function name implies) b) I don't think the size of the string is the main concern of OP. c) I don't want to make the answer more complex just to make it work with huge size of data.
What is an Encryption, but an algorithmic shift or replacement of the original text. I understand your point, and these methods work perfectly for most data. I was just pointing out that the larger the block of data you want to run this on, the more memory it takes, and the more likely you are to hit the string size limit.
@Nevyn - From wikipedia - "In cryptography, encryption is the process of encoding messages (or information) in such a way that eavesdroppers or hackers cannot read it, but that authorized parties can". Clearly anyone can read base64, this is not encryption.
I had written my own cipher which does this as well, but to a greater degree in that I hash the password and iterate over the 64 bytes from said hash + a dynamic offset. It's not impossible to crack, but the complexity of the algorithm makes it too time consuming and logistically unrealistic for anyone to try. I highly recommend a tactic like this if you're looking for simple obfuscation, encryption size ratio, and performance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.