unit EncryptService;

interface

uses Classes, TypeUnit, SysUtils;


type
  {加密发送的数据}
  TScEncryptService = class
  private
    function EncryptInteger(Value: Integer): string;
    function DeEncryptInteger(Value: string): Integer;
  public
    {服务数据加密}
    function EncryptString(const Value: string): string;
    {解密客户数据 IP + 版本类型 + Action}
    function DeEncryptString(const Value: string; AInfoRec: PInfoRecd): string;

  end;

implementation


{ TScEncryptService }

function TScEncryptService.DeEncryptInteger(Value: string): Integer;
begin

end;

function TScEncryptService.DeEncryptString(const Value: string; AInfoRec: PInfoRecd): string;
var
  sText: string;
  iLen: Integer;
begin
  if SameText(Value, '') then
  begin
    AInfoRec.Action := -1;
    Exit;
  end;

  sText := Copy(Value, 2, 2);
  iLen := StrToInt(sText);
  AInfoRec^.IP := Copy(Value, 5, iLen);

  sText := Copy(Value, 21, 2);
  AInfoRec^.User := StrToInt(sText);
  
  sText := Copy(Value, 23, 1);
  AInfoRec.Action := StrToInt(sText);
end;

function TScEncryptService.EncryptInteger(Value: Integer): string;
begin

end;

function TScEncryptService.EncryptString(const Value: string): string;
var
  I, iValue: Integer;
begin
  Randomize;
  SetLength(Result, Length(Value) + 5);
  iValue := Random(26) + 65;
  Result[1] := Chr(iValue);
  iValue := Random(26) + 65;
  Result[2] := Chr(iValue);
  iValue := Random(26) + 65;
  Result[3] := Chr(iValue);

  for I := 1 to Length(Value) do
  begin
    Result[I + 3] := Value[I];
  end;
  I := 3 + Length(Value);

  iValue := Random(26) + 65;
  Result[I + 1] := Chr(iValue);
  iValue := Random(26) + 65;
  Result[I + 2] := Chr(iValue);
end;

end.
