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

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
How to store delegates in a List(如何将代表存储在列表中)
How delegates work (in the background)?(代表如何工作(在后台)?)
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)