传输加密RawByteString
为 UTF-8 字符串的二进制文件是完全错误的,无论速度有多快。使用 Indy 10 传输二进制数据的最佳方式是使用TIdBytes
orTStream
代替。两者IOHandler
都有读/写方法,例如:
var
rbs: RawByteString;
begin
rbs := EncryptString(AInput, DEFAULT_ENCRYPTION_KEY);
AContext.Connection.IOHandler.Write(Int32(Length(rbs)));
AContext.Connection.IOHandler.Write(RawToBytes(PAnsiChar(rbs)^, Length(rbs)));
...
end;
var
buf: TIdBytes;
rbs: RawByteString;
Output: string;
begin
IdTCPClient1.IOHandler.ReadBytes(buf, IdTCPClient1.IOHandler.ReadInt32);
SetLength(rbs, Length(buf));
BytesToRaw(buf, PAnsiChar(rbs)^, Length(rbs));
Output := DecryptString(rbs, DEFAULT_ENCRYPTION_KEY);
...
end;
或者:
var
rbs: RawByteString;
strm: TIdMemoryBufferStream;
begin
rbs := EncryptString(AInput, DEFAULT_ENCRYPTION_KEY);
strm := TIdMemoryBufferStream.Create(PAnsiChar(rbs), Length(rbs));
try
AContext.Connection.IOHandler.LargeStream := False;
AContext.Connection.IOHandler.Write(strm, 0, True);
finally
strm.Free;
end;
...
end;
var
strm: TMemoryStream;
rbs: RawByteString;
Output: string;
begin
strm := TMemoryStream.Create;
try
IdTCPClient1.IOHandler.LargeStream := False;
IdTCPClient1.IOHandler.ReadStream(strm, -1, False);
SetLength(rbs, strm.Size);
Move(strm.Memory^, PAnsiChar(rbs)^, Length(rbs));
finally
strm.Free;
end;
{ alternatively:
SetLength(rbs, IdTCPClient1.IOHandler.ReadInt32);
strm := TIdMemoryBufferStream.Create(PAnsiChar(rbs), Length(rbs));
try
IdTCPClient1.IOHandler.ReadStream(strm, Length(rbs), False);
finally
strm.Free;
end;
}
Output := DecryptString(rbs, DEFAULT_ENCRYPTION_KEY);
...
end;