How to detect if a string is Base64Encoded or not?

2019-04-08 00:16发布

Which is the best method to detect if a string is Base64Encoded or not (using Delphi)?

4条回答
兄弟一词,经得起流年.
2楼-- · 2019-04-08 00:46

Best you can do is try to decode it. If the decode fails then the input was not base64 encoded. It the string successfully decodes then the input might have been base64 encoded.

查看更多
老娘就宠你
3楼-- · 2019-04-08 00:55

You can check if the string only contains Base64 valids chars

function StringIsBase64(const InputString : String ) : Boolean;
const
  Base64Chars: Set of AnsiChar = ['A'..'Z','a'..'z','0'..'9','+','/','='];
var
  i : integer;
begin
  Result:=True;
   for i:=1 to Length(InputString) do
   {$IFDEF UNICODE}
   if not CharInSet(InputString[i],Base64Chars) then
   {$ELSE}
   if not (InputString[i] in Base64Chars) then
   {$ENDIF}
   begin
     Result:=False;
     break;
   end;
end;

The = char is used for padding so you can add an aditional valiation to the function for padded base64 strings checking if the length of the string is mod 4

查看更多
We Are One
4楼-- · 2019-04-08 00:59

In addition to RRUZ answer you can also check the length of the string (is it a multiple of 4).

function IsValidBase64(const aValue: string): Boolean;
var
  i: Integer;
  lValidChars: set of Char;
begin
  Result := aValue <> '';
  lValidChars := ['a'..'z', 'A'..'Z', '0'..'9', '/', '+'];
  //length of string should be multiple of 4
  if Length(aValue) mod 4 > 0 then
    Result := False
  else
    for i := 1 to Length(aValue) do
    begin
      if aValue[i] = '=' then
      begin
        if i < Length(aValue) - 1 then
        begin              
          Result := False;
          Exit;
        end
        else
          lValidChars := ['='];
      end
      else if not (aValue[i] in lValidChars) then
      begin
        Result := False;
        Break;
      end;
    end;
end;

Please note that this code is Delphi 7 code and not adjusted for Unicode use.

查看更多
唯我独甜
5楼-- · 2019-04-08 01:05

As was already told here, there is no reliable verification if a certain string is Base64 encoded or not, so even when you consider the input as a valid Base64 encoded string, it doesn't mean the string is actually encoded that way. I'm posting here just another version of a validation function, which according to RFC 4648 verifies:

  • if the input string is not empty and its length is multiple of 4
  • if the input string contains at most two padding characters and only at the end of the string
  • if the input string contains only characters from the Base64 alphabet (see the Page 5, Table 1)

function IsValidBase64EncodedString(const AValue: string): Boolean;
const
  Base64Alphabet = ['A'..'Z', 'a'..'z', '0'..'9', '+', '/'];
var
  I: Integer;
  ValLen: Integer;
begin
  ValLen := Length(AValue);
  Result := (ValLen > 0) and (ValLen mod 4 = 0);
  if Result then
  begin
    while (AValue[ValLen] = '=') and (ValLen > Length(AValue) - 2) do
      Dec(ValLen);
    for I := ValLen downto 1 do
      if not (AValue[I] in Base64Alphabet) then
      begin
        Result := False;
        Break;
      end;
  end;
end;
查看更多
登录 后发表回答