为什么 EncdDecd.EncodeStream 返回一个新行?

Why EncdDecd.EncodeStream is returning a new line?

我正在使用函数 EncodeStream(来自 EncdDecd.pas)将字节数组转换为 base64 字符串,但我正在获取一个包含换行符的字符串。

这是我正在使用的转换函数:

uses
  EncdDecd, ...;

function EncodeBase64(AValue : TBytes) : string;
var
  StreamDecoded : TMemoryStream;
  StreamEncoded : TStringStream;
begin
  StreamDecoded := TMemoryStream.Create;
  StreamEncoded := TStringStream.Create('');
  try
    StreamDecoded.WriteBuffer(AValue[0], Length(AValue));
    StreamDecoded.Position := 0;
    EncdDecd.EncodeStream(StreamDecoded, StreamEncoded);
    Result := StreamEncoded.DataString;
  finally
    StreamEncoded.Free;
    StreamDecoded.Free;
  end;
end;

这是一个简单的测试,它会在 EncodeBase64 函数产生的字符串中出现一个新行:

var
  Bts : TBytes;
begin
  SetLength(Bts, 64);

  Bts[0] := 241;
  Bts[1] := 96;
  Bts[2] := 227;
  Bts[3] := 47;
  Bts[4] := 91;
  Bts[5] := 80;
  Bts[6] := 83;
  Bts[7] := 14;
  Bts[8] := 103;
  Bts[9] := 54;
  Bts[10] := 95;
  Bts[11] := 212;
  Bts[12] := 116;
  Bts[13] := 91;
  Bts[14] := 149;
  Bts[15] := 179;
  Bts[16] := 34;
  Bts[17] := 104;
  Bts[18] := 175;
  Bts[19] := 54;
  Bts[20] := 187;
  Bts[21] := 208;
  Bts[22] := 2;
  Bts[23] := 76;
  Bts[24] := 110;
  Bts[25] := 187;
  Bts[26] := 32;
  Bts[27] := 226;
  Bts[28] := 138;
  Bts[29] := 217;
  Bts[30] := 8;
  Bts[31] := 42;
  Bts[32] := 8;
  Bts[33] := 128;
  Bts[34] := 245;
  Bts[35] := 79;
  Bts[36] := 63;
  Bts[37] := 140;
  Bts[38] := 48;
  Bts[39] := 74;
  Bts[40] := 83;
  Bts[41] := 114;
  Bts[42] := 73;
  Bts[43] := 16;
  Bts[44] := 97;
  Bts[45] := 151;
  Bts[46] := 138;
  Bts[47] := 239;
  Bts[48] := 12;
  Bts[49] := 164;
  Bts[50] := 170;
  Bts[51] := 114;
  Bts[52] := 170;
  Bts[53] := 12;
  Bts[54] := 241;
  Bts[55] := 136;
  Bts[56] := 105;
  Bts[57] := 247;
  Bts[58] := 2;
  Bts[59] := 30;
  Bts[60] := 125;
  Bts[61] := 21;
  Bts[62] := 245;
  Bts[63] := 102;

  ShowMessage(EncodeBase64(Bts));
end;

ShowMessage 方法显示以下结果:

8WDjL1tQUw5nNl/UdFuVsyJorza70AJMbrsg4orZCCoIgPVPP4wwSlNySRBhl4rvDKSqcqoM8Yhp 9wIefRX1Zg==

在实际情况下,我将此字符串写为 ini 文件的属性值,新行导致从 ini 文件读回时部分字符串丢失 (TIniFile.ReadString 只读第一行)。

谁能帮我理解为什么会出现新行以及在编写 ini 文件时我应该如何管理这些字符串?

注:

我正在 Delphi2007 上测试。

line feed in base64 is discussed in RFC 4648.

MIME is often used as a reference for base 64 encoding. However, MIME does not define "base 64" per se, but rather a "base 64 Content-Transfer-Encoding" for use within MIME. As such, MIME enforces a limit on line length of base 64-encoded data to 76 characters. MIME inherits the encoding from Privacy Enhanced Mail (PEM), stating that it is "virtually identical"; however, PEM uses a line length of 64 characters. The MIME and PEM limits are both due to limits within SMTP.

Implementations MUST NOT add line feeds to base-encoded data unless the specification referring to this document explicitly directs base
encoders to add line feeds after a specific number of characters.

您使用的编码器在编写时考虑了 MIME,并且每 76 个字符强制换行。

Delphi 的现代版本引入了 System.NetEncoding 单元,该单元现在包含 base64 的 RTL 实现。 System.NetEncoding 中的功能现在允许您抑制编码内容中的换行。

在我看来,您有几个选择:

  1. 删除编码后的换行。
  2. 使用不插入换行符的其他 base64 编码器。