c#文件编码和解码,不可读 [英] c# file encoding and decoding, unreadable

查看:317
本文介绍了c#文件编码和解码,不可读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您使用txt编辑器打开某个文件时,无法读取编码类型,并且它只是一堆字符,我想在保存文件时使用我的程序,以及如何在c#中执行此操作:

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?

推荐答案


将一些字符串例如Sweet保存到文件中,如果您在文本编辑中打开文件,您将看到像nfgkdn @ {3!

只是一个简单的例子

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

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

这篇关于c#文件编码和解码,不可读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆