繁体   English   中英

如何从文件中读取二进制格式的字符串?

[英]How to read a binary formatted string from a file?

我想以某种方式将字符串存储在文件中,以至于无法(轻松)读取。 所以我使用这样的BinaryFormatter:

using (FileStream fs = File.Create(sfDialog.FileName, 2048, FileOptions.None))
{
    BinaryFormatter bf = new BinaryFormatter();
    bf.Serialize(fs, Encoding.Unicode.GetBytes(this.BodyText));
}

其中this.BodyText是要保存的字符串。

现在我很难从文件中读回来。 我试过BinaryReader没有成功,所以我想,我必须使用BinaryFormatter 我尝试了Deserialize方法,但它返回一个无法强制转换为字符串的对象。 Convert.ToBase64String也不能在对象上使用。

有谁知道如何解决我的问题?

我同意其他人,你应该使用一些适当的加密而不是这个。

但要回答你的实际问题:你正在序列化一个字节数组。 这就是你反序列化时得到的结果。

因此,在反序列化之后,强制转换为byte[]并将此字节数组转换为字符串:

var s = Encoding.Unicode.GetString((byte[])deserializedValue);

使用此功能加密和解密。

    string passPhrase = "Pasword";        // can be any string
    string saltValue = "sALtValue";        // can be any string
    string hashAlgorithm = "SHA1";             // can be "MD5"
    int passwordIterations = 7;                  // can be any number
    string initVector = "~1B2c3D4e5F6g7H8"; // must be 16 bytes
    int keySize = 256;                // can be 192 or 128

    private string Encrypt(string data)
    {
        byte[] bytes = Encoding.ASCII.GetBytes(this.initVector);
        byte[] rgbSalt = Encoding.ASCII.GetBytes(this.saltValue);
        byte[] buffer = Encoding.UTF8.GetBytes(data);
        byte[] rgbKey = new PasswordDeriveBytes(this.passPhrase, rgbSalt, this.hashAlgorithm, this.passwordIterations).GetBytes(this.keySize / 8);
        RijndaelManaged managed = new RijndaelManaged();
        managed.Mode = CipherMode.CBC;
        ICryptoTransform transform = managed.CreateEncryptor(rgbKey, bytes);
        MemoryStream stream = new MemoryStream();
        CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Write);
        stream2.Write(buffer, 0, buffer.Length);
        stream2.FlushFinalBlock();
        byte[] inArray = stream.ToArray();
        stream.Close();
        stream2.Close();
        return Convert.ToBase64String(inArray);
    }

    private string Decrypt(string data)
    {
        byte[] bytes = Encoding.ASCII.GetBytes(this.initVector);
        byte[] rgbSalt = Encoding.ASCII.GetBytes(this.saltValue);
        byte[] buffer = Convert.FromBase64String(data);
        byte[] rgbKey = new PasswordDeriveBytes(this.passPhrase, rgbSalt, this.hashAlgorithm, this.passwordIterations).GetBytes(this.keySize / 8);
        RijndaelManaged managed = new RijndaelManaged();
        managed.Mode = CipherMode.CBC;
        ICryptoTransform transform = managed.CreateDecryptor(rgbKey, bytes);
        MemoryStream stream = new MemoryStream(buffer);
        CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Read);
        byte[] buffer5 = new byte[buffer.Length];
        int count = stream2.Read(buffer5, 0, buffer5.Length);
        stream.Close();
        stream2.Close();
        return Encoding.UTF8.GetString(buffer5, 0, count);
    }

如果您希望字符串不易读取,则应使用适当的加密。

例如,您可以使用将使用Windows凭据作为加密密钥的DPAPI。 关于这个概念的优点/缺点的更多细节

或者你可以阅读Josh Galloway的文章探索3种不同的方法,其中第三种看起来很有希望,即使我自己没有尝试过,但很酷的部分是它不需要你的加密数据在.config文件中。 )

此外,关于原始问题,不要忘记,因为你打电话

Encoding.Unicode.GetBytes(this.BodyText)

在反序列化时,您需要调用Encoding服务以从字节数组中获取Unicode字符串:

Encoding.Unicode.GetString(myByteArray);

尝试这个 :

using (FileStream fs = File.Create(sfDialog.FileName, 2048, FileOptions.None))
{
    BinaryFormatter deserializer = new BinaryFormatter();
    deserializedObject = System.Text.Encoding.Unicode.GetString((byte[])deserializer.Deserialize(memoryStream));
}

暂无
暂无

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

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