Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Base64-decode a string using NetEncoding in Delphi XE7?

In Delphi XE7 Update 1, when trying to execute this code in a VCL program:

uses System.NetEncoding;
...
tempstring := TNetEncoding.Base64.Decode(tempstring);

I get this error message:

Error message

So how can I base64-decode a string using NetEncoding?

like image 419
user1580348 Avatar asked Feb 23 '15 08:02

user1580348


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.

How do you convert a string to Base64?

To convert a string into a Base64 character the following steps should be followed: Get the ASCII value of each character in the string. Compute the 8-bit binary equivalent of the ASCII values. Convert the 8-bit characters chunk into chunks of 6 bits by re-grouping the digits.

Is BTOA a Base64?

The btoa() method creates a Base64-encoded ASCII string from a binary string (i.e., a string in which each character in the string is treated as a byte of binary data).

What is b64 decode?

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

You are calling the Decode overload that accepts base64 encoded text and returns a string. The way that Decode method is implemented is as follows:

  1. Decode the base64 to binary.
  2. Treat the binary as UTF-8 encoded text.
  3. Decode the UTF-8 bytes to text.

Your error message indicates that the binary that results from step 1 is not valid UTF-8. In order to work out why this is so requires knowledge of where the base64 encoded text originated.

Your current code assumes that the following process took place to create the base64 encoded value:

  1. Encode some text to UTF-8 bytes.
  2. Base 64 encode those bytes.

The problem is not necessarily in your code to decode. The problem is that your decoding does not match the process that originally encoded. The fault could lie either in the original encoding, or the decoding. You need to check how the base64 value was encoded. Was it encoded using the steps described above?

If I had to guess, I would suspect that the original data was not in fact textual. My guess is that the original data is binary, that is a stream of bytes. I suspect that you started from some code which held a binary data in an AnsiString variable. It is very common to see Delphi code that abuses strings in this way. For historical reasons it seems to have been written in to the lore that AnsiString can be used to hold binary data. I guess this all started when Delphi 2 introduced AnsiString and at that time there were no dynamic arrays.

Anyway, if you have previously been using AnsiString to hold binary data then you should take this opportunity to stop doing so. Use TBytes, for instance, for that purpose. In which case you would use:

var
  bytes: TBytes;
....
bytes := TNetEncoding.Base64.DecodeStringToBytes(...);

to decode your base64 value to binary.

So, suppose that the base64 that you have is actually ANSI encoded text. Then you would decode it like this:

var
  bytes: TBytes;
  text: string;
....
bytes := TNetEncoding.Base64.DecodeStringToBytes(...);
text := TEncoding.ANSI.GetString(bytes);

This assumes the prevailing ANSI code page on the machine on which you decode. If you need to specify an ANSI code page then you might write it like this:

var
  bytes: TBytes;
  text: string;
  Encoding: TEncoding;
....
bytes := TNetEncoding.Base64.DecodeStringToBytes(...);
Encoding := TEncoding.GetEncoding(CodePage);
try
  text := Encoding.GetString(bytes);
finally
  Encoding.Free;
end;

As you can see, I am now reduced to guessing. If you want to make progress you will find out how your data was encoded.

like image 65
David Heffernan Avatar answered Sep 20 '22 13:09

David Heffernan