c# file encoding and decoding, unreadable

家住魔仙堡 提交于 2019-12-25 17:20:31

问题


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?


回答1:


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);
}
|improve this answer

来源:https://stackoverflow.com/questions/13980207/c-sharp-file-encoding-and-decoding-unreadable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!