14

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 '=='

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.

Edit: 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:

Output

OpenSSL

abatishchev
  • 98,240
  • 88
  • 296
  • 433
JMK
  • 27,273
  • 52
  • 163
  • 280
  • can you give more information about what is going wrong? do you receive an exception? or is the output unreadable? – Tim Cools Dec 27 '11 at 13:19
  • So the base-64 string is an encoded byte array of another string? Was the string originally converted into a byte[] using ASCII? – vcsjones Dec 27 '11 at 13:20
  • Maybe you use a wrong encoding? Can you show what you get and what you expect? – Jan Dec 27 '11 at 13:20
  • What do you know about the original data? Is it really ASCII text? How does a hex dump of the `decodedFromBase64` array look? – Anders Marzi Tornblad Dec 27 '11 at 13:26
  • The original data is a Persons Name that has been Encrypted with my RSA 3DES public key and then encoded in Base64. – JMK Dec 27 '11 at 13:28

3 Answers3

14
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;
}
Hussein Zawawi
  • 2,907
  • 2
  • 26
  • 44
3

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().

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Matthew Whited
  • 22,160
  • 4
  • 52
  • 69
1
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);
    }