Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I encode and decode a base64 string?

Tags:

c#

base64

  1. How do I return a base64 encoded string given a string?

  2. How do I decode a base64 encoded string into a string?

like image 217
Kevin Driedger Avatar asked Jul 31 '12 15:07

Kevin Driedger


People also ask

How do I decode a Base64 string?

To decode with base64 you need to use the --decode flag. With encoded string, you can pipe an echo command into base64 as you did to encode it. Using the example encoding shown above, let's decode it back into its original form. Provided your encoding was not corrupted the output should be your original string.

Can Base64 encoding be decoded?

In JavaScript there are two functions respectively for decoding and encoding Base64 strings: btoa() : creates a Base64-encoded ASCII string from a "string" of binary data ("btoa" should be read as "binary to ASCII"). atob() : decodes a Base64-encoded string("atob" should be read as "ASCII to binary").

What is Base64 encoding and decoding?

What Does Base64 Mean? Base64 is an encoding and decoding technique used to convert binary data to an American Standard for Information Interchange (ASCII) text format, and vice versa.


1 Answers

Encode

public static string Base64Encode(string plainText) {   var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);   return System.Convert.ToBase64String(plainTextBytes); } 

Decode

public static string Base64Decode(string base64EncodedData) {   var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);   return System.Text.Encoding.UTF8.GetString(base64EncodedBytes); } 
like image 172
Kevin Driedger Avatar answered Sep 21 '22 14:09

Kevin Driedger