S
S
s14e2016-10-14 18:52:05
Delphi
s14e, 2016-10-14 18:52:05

Code from Delphi to C++!?

There is a simple delphi code, the essence is - it encrypts the serial number of the disk with an already tested and working function:

const
  SEED_SIZE = 16;
  SEED:  array[0..SEED_SIZE - 1] of Byte = (
    $51, $2F, $F6, $AE, $46, $8D, $DE, $5D, $D5, $73, $0A, $6D, $12, $56, $83, $78
  );

  function xordata(InStr: RawByteString; InSize: Integer): RawByteString;
  var
    i, j: Integer;
    bt: byte;
  begin
    SetLength(Result, InSize);
    j := 0;
    for i := 0 to InSize - 1 do
    begin
      if j = 16 then j := 0;
      bt := Byte(InStr[i + 1]) xor SEED[j];
      bt := bt xor i;
      Byte(Result[i + 1]) := bt;
      inc(j);
    end;
  end;

xordata(SERIAL_NUMBER, SERIAL_NUMBER_LENGTH);

It is necessary to correctly translate this solution into C ++. Gentlemen, experts, I ask you to help, how to correctly and briefly write such code?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mercury13, 2016-10-14
@Mercury13

InStr/InSize is a bit redundant by Delphi standards and can be converted to const char* data / size_t length.
The result is std::string, of course. Or std::vector<char>. What instead of SetLength - read the doc.
InStr[i + 1] → data[i]. The remaining arrays are numbered from zero, everything is as it was.
Inc(j) → ++j.
It seems to me that this code was mechanically rewritten from C to Pascal and it will not be difficult to rewrite it back.
There is also the magic number 16 - guess what to replace it with. By the way, don't smear the two lines associated with j.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question