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(如何将一串文本从 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 解码为字节数组,并在不损坏数据的情况下获取此字节数组的字符串属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将一串文本从 Base64 解码为字节数组,并在不


- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- 使用 rss + c# 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01