BASE64 Encode and Decode is not working
-
21-12-2019 - |
質問
I am working on android applicatin in Delphi XE5 and I need to BASE64 encode and decode some strings.
This function is working fine for english characters, but what I want to encode €, $ or any special iso8859-2 characters encoding doesn't work.
Any idea how to fix it?
I found BASE64 unit http://www.delphipraxis.net/991-base64-mime-en-decoding.html
But does FireMonkey support AnsiString and PAnsiChar type and what unit to include to use this type?
My code
uses IdCoderMIME;
...
function Encode64(S: string): string;
var
IdEncoderMIME: TIdEncoderMIME;
begin
try
IdEncoderMIME := TIdEncoderMIME.Create(nil);
Result := IdEncoderMIME.EncodeString(S);
finally
IdEncoderMIME.Free;
end;
end;
function Decode64(S: string): string;
var
IdDecoderMIME: TIdDecoderMIME;
var
IdDecoderMIME: TIdDecoderMIME;
begin
try
IdDecoderMIME := TIdDecoderMIME.Create(nil);
Result := IdDecoderMIME.DecodeString(S);
finally
IdDecoderMIME.Free;
end;
end;
解決
Officially, Delphi DOES NOT support AnsiString
and (P)AnsiChar
on mobile platforms. Unofficially, the support code for them is still present in the compiler and RTL, it is just hidden so you cannot access it anymore. There is a third-party patch available that re-enables access.
When encoding/decoding a string, you have to take character encoding into account. Base64 encodes bytes, not characters. You have to convert a string to a byte sequence before then Base64 encoding the bytes, and then Base64 decode the byte sequence before then converting it back to a string.
The TIdEncoderMIME.EncodeString()
and TIdDecoderMIME.DecodeString()
methods have an optional TIdTextEncoding
or IIdTextEncoding
parameter (depending on your version of Indy) for that string<->bytes conversion. If you do not specify a text encoding, Indy will use its default text encoding, which is 7bit ASCII by default (configurable via the IdGlobal.GIdDefaultTextEncoding
variable).
For example:
uses
..., IdGlobal, IdCoderMIME;
function Encode64(const S: string: const ByteEncoding: IIdTextEncoding = nil): string;
begin
Result := TIdEncoderMIME.EncodeString(S, ByteEncoding);
end;
function Decode64(const S: string: const ByteEncoding: IIdTextEncoding = nil): string;
begin
Result := TIdDecoderMIME.DecodeString(S, ByteEncoding);
end;
uses
..., IdGlobal;
var
s, base64: string;
begin
s := '€$';
base64 := Encode64(s, IndyTextEncoding_UTF8);
s := Decode64(base64, IndyTextEncoding_UTF8);
end;
uses
..., IdGlobal;
var
s, base64: string;
enc: IIdTextEncoding;
begin
enc := IndyTextEncoding(28592); // ISO-8859-2
s := '€$';
base64 := Encode64(s, enc);
s := Decode64(base64, enc);
end;
uses
..., IdGlobal, IdGlobalProtocols;
var
s, base64: string;
enc: IIdTextEncoding;
begin
enc := CharsetToEncoding('ISO-8859-2');
s := '€$';
base64 := Encode64(s, enc);
s := Decode64(base64, enc);
他のヒント
The key point is that base64 is an encoding of byte arrays to text. So if you wish to encode text, you must first transform the text into a byte array. And to do so you need to pick a specific text encoding.
For the sake of example, let us suppose that your text encoding is UTF-8. The procedure for encoding text to base64 runs like this:
- Encode the plain text into a byte array using the UTF-8 encoding.
- Encode that byte array into a base64 string.
In the opposite direction it goes like this:
- Decode the base64 string into a byte array.
- Decode the byte array to a string using the UTF-8 encoding.
Delphi ships with libraries that can perform all of these steps. The TEncoding
class handles the UTF-8 part, and the Soap.EncdDecd
unit does base64. There is no need to use Indy here just to perform base64 encoding.
Unfortunately the EncodeString
and DecodeString
functions in Soap.EncdDecd
do not handle Unicode correctly. I prefer to avoid them and do it all using the EncodeStream
and DecodeStream
functions. Like this:
uses
System.SysUtils, System.Classes, Soap.EncdDecd;
function EncodeString(const Input: string): string;
var
InStr, OutStr: TStringStream;
begin
InStr := TStringStream.Create(Input, TEncoding.UTF8);
try
OutStr := TStringStream.Create('');
try
EncodeStream(InStr, OutStr);
Result := OutStr.DataString;
finally
OutStr.Free;
end;
finally
InStr.Free;
end;
end;
function DecodeString(const Input: string): string;
var
InStr, OutStr: TStringStream;
begin
InStr := TStringStream.Create(Input);
try
OutStr := TStringStream.Create('', TEncoding.UTF8);
try
DecodeStream(InStr, OutStr);
Result := OutStr.DataString;
finally
OutStr.Free;
end;
finally
InStr.Free;
end;
end;