Dasher Labs
Dasher Labs

Reputation: 99

c# file encoding and decoding, unreadable

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?

Upvotes: 0

Views: 4762

Answers (1)

I4V
I4V

Reputation: 35353

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);
}

Upvotes: 3

Related Questions