如何将一串文本从 Base64 解码为字节数组,并在不损坏数据的情况下获取此字节数组的字符串属性 [英] How to decode a string of text from a Base64 to a byte array, and the get the string property of this byte array without data corruption

查看:28
本文介绍了如何将一串文本从 Base64 解码为字节数组,并在不损坏数据的情况下获取此字节数组的字符串属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我有一个文本字符串,用 Base 64 编码,如下所示:

Ok so I have a string of text, encoded in Base 64 like below:

string myText = "abcBASE64TEXTGOESHEREdef==";  // actual string is 381 characters long with trailing '=='

然后我将我的字符串从 Base 64 转换为字节数组,如下所示:

I then convert my string from Base 64 to a byte array like so:

byte[] decodedFromBase64 = Convert.FromBase64String(myText);

此时,我想获取此字节数组的字符串值并将其保存在文本文件中而不会丢失或损坏数据.下面的代码似乎没有这样做:

At this point, I want to get the string value of this byte array and save this in a text file without data loss or corruption. The code below doesn't seem to be doing it:

string myDecodedText = Encoding.ASCII.GetString(decodedFromBase64);
StreamWriter myStreamWriter = new StreamWriter("C:\OpenSSL-Win32\bin\textToDecrypt.txt");
myStreamWriter.Write(myString);
myStreamWriter.Flush();
myStreamWriter.Close();

谁能告诉我哪里出错了.

Can somebody please tell me where I'm going wrong.

输出不可读,我需要获取解码后的字符串,然后使用 OpenSSL 对其进行解密.OpenSSL 的输出和结果如下:

The output is unreadable, I need to take the decoded string and then use OpenSSL to decrypt it. The Output and the result from OpenSSL are both below:

推荐答案

如果字符串经过编码,那么内容看起来很像您在文本文件中的内容.但是为了确保文件不会损坏,您应该将文件内容编写为二进制文件,而不是使用文本编码器.查看 File.WriteAllBytes().

If the string is encoded then the contents would look much like what you have in your text file. But to ensure that the file is not getting corrupt you should write the file content as binary instead of using a text encoder. Check out File.WriteAllBytes().

这篇关于如何将一串文本从 Base64 解码为字节数组,并在不损坏数据的情况下获取此字节数组的字符串属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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