如何将一串文本从 Base64 解码为字节数组,并在不损坏数据的情况下获取此字节数组的字符串属性
- 作者: 你说无所谓的有所谓
- 来源: 51数据库
- 2022-12-15
问题描述
好的,我有一个文本字符串,用 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().
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
