简体   繁体   English

用C#在文本文件中写入字符串数据的字节格式的问题

[英]Problems with writing bytes format of string data in Text File in C#

I have a text file stored locally. 我有一个本地存储的文本文件。 I want to store string data in binary format there and then retrieve the data again. 我想在那里以二进制格式存储字符串数据,然后再次检索数据。 In the following code snippet, I have done the conversion. 在以下代码段中,我完成了转换。

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
class ConsoleApplication
{
    const string fileName = "AppSettings.dat";

    static void Main()
    {
        string someText = "settings";
        byte[] byteArray = Encoding.UTF8.GetBytes(someText);
        int byteArrayLenght = byteArray.Length;
        using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
        {
            writer.Write(someText);
        }
        byte[] x = new byte[byteArrayLenght];

        if (File.Exists(fileName))
        {
            using (BinaryReader reader = new BinaryReader(File.Open(fileName, FileMode.Open)))
            {
                x = reader.ReadBytes(byteArrayLenght);
            }
            string str = Encoding.UTF8.GetString(x);
            Console.Write(str);
            Console.ReadKey();
        }
    }
}

In the AppSettings.dat file the bytes are written in the following way 在AppSettings.dat文件中,字节以以下方式写入 在此处输入图片说明

But when I have assigned some random value in a byte array and save it in a file using BinaryWriter as I have done in the following code snippet 但是,当我在字节数组中分配了一些随机值并使用BinaryWriter将其保存在文件中时,就像在下面的代码片段中所做的那样

const string fileName = "AppSettings.dat";

static void Main()
{
    byte[] array = new byte[8];
    Random random = new Random();
    random.NextBytes(array);

    using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
    {
        writer.Write(array);
    }
}

It's actually saved the data in binary format in the text file, shown in the picture. 它实际上以二进制格式将数据保存在文本文件中,如图所示。 在此处输入图片说明

I don't understand why (in my first case) the byte data converted from string showing human readable format where I want to save the data in non-readable byte format(later case). 我不明白为什么(在我的第一种情况下)从显示人类可读格式的字符串转换为字节数据,为什么要以非可读字节格式(后一种情况)保存数据。 What's the explanation regarding this? 请问对此有何解释?

Is there any way where I can store string data in binary format without approaching brute force? 有什么方法可以在不接近蛮力的情况下以二进制格式存储字符串数据?

FYI - I don't want to keep the data in Base64String format, I want it to be in binary format. 仅供参考-我不想将数据保留为Base64String格式,而是希望其为二进制格式。

If security isn't a concern, and you just don't want the average usage to find your data while meddling into the settings files, a simple XOR will do: 如果不考虑安全性,并且您只是不想让普通用户在介入设置文件的同时查找数据,则可以使用简单的XOR进行:

const string fileName = "AppSettings.dat";

static void Main()
{
    string someText = "settings";
    byte[] byteArray = Encoding.UTF8.GetBytes(someText);

    for (int i = 0; i < byteArray.Length; i++)
    {
        byteArray[i] ^= 255;
    }

    File.WriteAllBytes(fileName, byteArray);

    if (File.Exists(fileName))
    {
        var x = File.ReadAllBytes(fileName);

        for (int i = 0; i < byteArray.Length; i++)
        {
            x[i] ^= 255;
        }

        string str = Encoding.UTF8.GetString(x);
        Console.Write(str);
        Console.ReadKey();
    }
}

It takes advantage of an interesting property of character encoding: 它利用了字符编码的有趣特性:

  • In ASCII, the 0-127 range contains the most used characters (a to z, 0 to 9) and the 128-256 range contains only special symbols and accents 在ASCII中,0-127范围包含最常用的字符(a到z,0到9),而128-256范围仅包含特殊符号和重音
  • For compatibility reasons, in UTF-8 the 0-127 range contains the same characters as ASCII, and the 128-256 range have a special meaning (it tells the decoder that the characters are encoded into multiple bytes) 出于兼容性原因,在UTF-8中0-127范围包含与ASCII相同的字符,而128-256范围具有特殊含义(它告诉解码器字符已编码为多个字节)

All I do is flipping the strong-bit of each byte. 我要做的就是翻转每个字节的强位。 Therefore, everything in the 0-127 range ends up in the 128-256 range, and vice-versa. 因此,0-127范围内的所有内容最终都在128-256范围内,反之亦然。 Thanks to the property I described, no matter if the text-reader tries to parse in ASCII or in UTF-8, it will only get gibberish. 多亏了我描述的属性,无论文本阅读器尝试使用ASCII还是UTF-8进行解析,它只会变得乱七八糟。

Please note that, while it doesn't produce human-readable content, it isn't secure at all. 请注意,尽管它不会产生人类可读的内容,但它绝对不安全。 Don't use it to store sensitive data. 不要使用它来存储敏感数据。

The notepad just reads your binary data and converts it to UTF8 text. 记事本仅读取您的二进制数据并将其转换为UTF8文本。

This code snippet would give you the same result. 此代码段将为您提供相同的结果。

byte[] randomBytes = new byte[20];
Random rand = new Random();
rand.NextBytes(randomBytes);
Console.WriteLine(Encoding.UTF8.GetString(randomBytes));

If you want to stop people from converting your data back to a string. 如果要阻止人们将数据转换回字符串。 then you need to encrypt your data. 那么您需要加密您的数据。 Here is a project that can help you with that. 是一个可以帮助您的项目。 But they are still able to read the data in a text editor because it converts your encrypted data to UFT8. 但是他们仍然能够在文本编辑器中读取数据,因为它会将加密的数据转换为UFT8。 They can't Convert it back to usable data unless they have to key to decrypt your data. 他们无法将其转换回可用数据,除非他们必须输入密钥才能解密您的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM