Posted By

rengber on 04/28/08


Tagged

uuencode base64


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

umang_nine
ddevening
rlynch3


Base64 Encode or Decode a File


 / Published in: C#
 

Useful for sending files past filters via email.

  1. private void btnEncode_Click(object sender, EventArgs e)
  2. {
  3. if (!string.IsNullOrEmpty(txtInFile.Text))
  4. {
  5. FileStream fs = new FileStream(txtInFile.Text,
  6. FileMode.Open,
  7. FileAccess.Read);
  8. byte[] filebytes = new byte[fs.Length];
  9. fs.Read(filebytes, 0, Convert.ToInt32(fs.Length));
  10. string encodedData =
  11. Convert.ToBase64String(filebytes,
  12. Base64FormattingOptions.InsertLineBreaks);
  13. txtEncoded.Text = encodedData;
  14. }
  15. }
  16.  
  17. private void btnDecode_Click(object sender, EventArgs e)
  18. {
  19. if (!string.IsNullOrEmpty(txtOutFile.Text))
  20. {
  21. byte[] filebytes = Convert.FromBase64String(txtEncoded.Text);
  22. FileStream fs = new FileStream(txtOutFile.Text,
  23. FileMode.CreateNew,
  24. FileAccess.Write,
  25. FileShare.None);
  26. fs.Write(filebytes, 0, filebytes.Length);
  27. fs.Close();
  28. }
  29. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: ch4ndra on May 30, 2009

I need implementation that source in visual c# 2005,,please,,, send me at : [email protected]

thanks very much.....

Posted By: Clueless0417 on February 4, 2011

Yea, it works great until you throw Russian or Arabic or Chinese or anything not ASCII at it. Doesn't ANYBODY know how to actually decode Base64? Microsoft obviously doesn't.

Posted By: rengber on April 27, 2011

What are you talking about, this isn't for text, it's for binary. The file you end with is binary identical to the file you start with.

Turning binary data into an ASCII string is the definition of Base64, languages don't come into play, it's a string of gibberish bytes.

You need to login to post a comment.