Delphi 下的Base64编码和解码

function Base64Encode(s : string) : string;  //base64 编码
var base64 : TIdEncoderMIME;
    tmpBytes : TBytes;
begin
  base64 := TIdEncoderMIME.Create(nil);
  try
    base64.FillChar := '=';
    tmpBytes := TEncoding.UTF8.GetBytes(s);
    Result := base64.EncodeBytes(TIdBytes(tmpBytes));
  finally
    base64.Free;
  end;
end;

function Base64Decode(s : string) : string;  //base64 解码
var base64 : TIdDeCoderMIME;
    tmpBytes : TBytes;
begin
  Result := s;
  base64 := TIdDecoderMIME.Create(nil);
  try
    base64.FillChar := '=';
    tmpBytes := TBytes(base64.DecodeBytes(s));
    Result := TEncoding.UTF8.GetString(tmpBytes);
  finally
    base64.Free;
  end;
end;

你可能感兴趣的:(Delphi)