How to Decode a Base64 Encoded String

How do I encode and decode a base64 string?

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);
}

how to decode string in base64 and keep it as string in c# asp.net4

Base-64 could be an encoding of anything. If you know it's an encoding of an ASCII string, you could do this:

var bytes = Convert.FromBase64String(u);
var text = System.Text.Encoding.UTF8.GetString(bytes, 0, bytes.Length);

If it's an encoding of a Unicode string (you weren't specific), you could do this:

var bytes = Convert.FromBase64String(u);
var text = System.Text.Encoding.Unicode.GetString(bytes, 0, bytes.Length);

Decode base64 string C#

the correct Base64String is:

PGlmcmFtZSBzcmM9Imh0dHA6Ly9lbWJlZC5yZWR0dWJlLmNvbS8/aWQ9Mzg1NjAmYmdjb2x
vcj0wMDAwMDAiIGZyYW1lYm9yZGVyPSIwIiB3aWR0aD0iNDM0IiBoZWlnaHQ9IjM0NCIgc2Nyb2xsaW5n
PSJubyIgYWxsb3dmdWxsc2NyZWVuPjwvaWZyYW1lPg==

Well this is not a valid Base64String. Base64String can not have \ character. remove this character and it will work

Return decoded String from base64-encoded ByteArrayOutputStream

To find the way back from your Base64 string you need to "inflate" the compressed content like this:

    String base64 = Base64.getEncoder().encodeToString(bArr); // eJzzSM3JyVcozy/KSQEAGKsEPQ==

// reverse
byte[] decodedBytes = Base64.getDecoder().decode(base64);
ByteArrayOutputStream byteArrayOutputStream2 = new ByteArrayOutputStream();
InflaterOutputStream inflaterOutputStream = new InflaterOutputStream(byteArrayOutputStream2);
inflaterOutputStream.write(decodedBytes);
inflaterOutputStream.close();
System.out.println("out: "+ byteArrayOutputStream2.toString()); // Hello World
byteArrayOutputStream2.close();

Base64 Java encode and decode a string

You can use following approach:

import org.apache.commons.codec.binary.Base64;

// Encode data on your side using BASE64
byte[] bytesEncoded = Base64.encodeBase64(str.getBytes());
System.out.println("encoded value is " + new String(bytesEncoded));

// Decode data on other side, by processing encoded data
byte[] valueDecoded = Base64.decodeBase64(bytesEncoded);
System.out.println("Decoded value is " + new String(valueDecoded));

Hope this answers your doubt.

Does anyone know how to decode and encode a string in Base64 using Base64?

First:

  • Choose an encoding. UTF-8 is generally a good choice; stick to an encoding which will definitely be valid on both sides. It would be rare to use something other than UTF-8 or UTF-16.

Transmitting end:

  • Encode the string to bytes (e.g. text.getBytes(encodingName))
  • Encode the bytes to base64 using the Base64 class
  • Transmit the base64

Receiving end:

  • Receive the base64
  • Decode the base64 to bytes using the Base64 class
  • Decode the bytes to a string (e.g. new String(bytes, encodingName))

So something like:

// Sending side
byte[] data = text.getBytes("UTF-8");
String base64 = Base64.encodeToString(data, Base64.DEFAULT);

// Receiving side
byte[] data = Base64.decode(base64, Base64.DEFAULT);
String text = new String(data, "UTF-8");

Or with StandardCharsets:

// Sending side
byte[] data = text.getBytes(StandardCharsets.UTF_8);
String base64 = Base64.encodeToString(data, Base64.DEFAULT);

// Receiving side
byte[] data = Base64.decode(base64, Base64.DEFAULT);
String text = new String(data, StandardCharsets.UTF_8);

Decoding Base64 String Returns nil

Your string contains underscore character ('_' U+005F LOW LINE), which is invalid as Base-64.

There are some variations of Base-64, so you need to find which one your Gmail something is using. (Wikipedia Base-64.)

Assuming it uses base64url URL- and filename-safe, you may need to write something like this:

extension String {
func urlSafeBase64Decoded() -> String? {
var st = self
.replacingOccurrences(of: "_", with: "/")
.replacingOccurrences(of: "-", with: "+")
let remainder = self.count % 4
if remainder > 0 {
st = self.padding(toLength: self.count + 4 - remainder,
withPad: "=",
startingAt: 0)
}
guard let d = Data(base64Encoded: st, options: .ignoreUnknownCharacters) else{
return nil
}
return String(data: d, encoding: .utf8)
}
}

Example:

let base64 = "WW91ciBhY2NvdW50IHNhbG1hbm1hamlkMTRAZ21haWwuY29tIGlzIGxpc3RlZCBhcyB0aGUgcmVjb3Zlcnk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BBbXBoaXRoZWF0cmUgUGFya3dheSwgTW91bnRhaW4gVmlldywgQ0EgOTQwNDMsIFVTQQ0K"

print(base64.urlSafeBase64Decoded() ?? "* decoding failed*")

Output:

You received this email to let you know about important changes to your
Google Account and services.
© 2019 Google LLC, 1600 Amphitheatre Parkway, Mountain View, CA 94043, USA


Related Topics



Leave a reply



Submit