繁体   English   中英

如何将文本字符串从Base64解码为字节数组,以及如何在不破坏数据的情况下获取此字节数组的string属性

[英]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

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

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

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

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

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

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

有人可以告诉我我要去哪里错了。

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

输出量

的OpenSSL

public static string base64Decode(string data)
{
     byte[] toDecodeByte = Convert.FromBase64String(data);

     System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
     System.Text.Decoder utf8Decode = encoder.GetDecoder();

     int charCount = utf8Decode.GetCharCount(toDecodeByte, 0, toDecodeByte.Length);

     char[] decodedChar = new char[charCount];
     utf8Decode.GetChars(toDecodeByte, 0, toDecodeByte.Length, decodedChar, 0);
     string result = new String(decodedChar);
     return result;
}

如果对字符串进行了编码,则内容看起来很像文本文件中的内容。 但是为了确保文件不会损坏,您应该将文件内容写为二进制文件,而不是使用文本编码器。 检出File.WriteAllBytes()

static void Main(string[] args)
    {
        string completeUrl = SERVICE_URL;
        WebRequest request = WebRequest.Create(completeUrl);
        request.Credentials = CredentialCache.DefaultCredentials;
        WebProxy proxyObject = new WebProxy(SERVICE_URL, true);
        request.Proxy = proxyObject;
        HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();
        byte[] data;
        const int BUFFER_SIZE = 2048;
        int bytesRead;
        byte[] buffer = new byte[BUFFER_SIZE];
        using (MemoryStream mss = new MemoryStream())
        {
            using (BinaryReader readers = new
            BinaryReader(webResponse.GetResponseStream(), System.Text.Encoding.UTF8))
            {
                while ((bytesRead = readers.Read(buffer, 0, BUFFER_SIZE)) > 0)
                {
                    mss.Write(buffer, 0, bytesRead);
                }
                data = mss.ToArray();
                System.Text.Encoding enc = System.Text.Encoding.GetEncoding("iso-8859-1");
                string str = enc.GetString(data);
                XmlDocument xdoc = new XmlDocument();
                xdoc.LoadXml(str);
                XmlNode xmlList = xdoc.ChildNodes[1];
                XmlNode item = xmlList.ChildNodes[1]; //this is your data : JVBERi0xLjUNCiXNzc3NDQoxIDAgb2JqDQo8PA0KL0NyZWF0b3IgKERvY3VtZW50UHJ
                Base64Decode(item.InnerText.ToString());
                Console.WirteLine("File successfully saved");
                Console.ReadLine();
            }
        }
    }

    public static void Base64Decode(string base64EncodedData)
    {
        var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
        File.WriteAllBytes("pdf.pdf", base64EncodedBytes);
    }

暂无
暂无

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

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