Avoir une explication sur uen portion de code

cs_davidc Messages postés 4 Date d'inscription mardi 29 avril 2003 Statut Membre Dernière intervention 25 janvier 2009 - 20 janv. 2009 à 23:36
cs_davidc Messages postés 4 Date d'inscription mardi 29 avril 2003 Statut Membre Dernière intervention 25 janvier 2009 - 25 janv. 2009 à 00:15
Bonsoir, je désirerais avoir une explication assez détaillée pour la portion de code en rouge  Merci:

function TForm1.DecodeMSKey(const HexSrc: array of byte): string;
const  StartOffset: integer $34;      // Offset 34 Array[52]  EndOffset:   integer $34 + 15; // Offset 34 + 15(Bytes) Array[64]
  Digits: array[0..23] of char =
    ('B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'M', 'P', 'Q', 'R', 'T',
    'V', 'W', 'X', 'Y', '2', '3', '4', '6', '7', '8', '9');
  DecodedLen: integer = 29;       // Length of Decoded Product Key
  EncodedLen: integer = 15;
  // Length of Encoded Product Key in Bytes (An total of 30 in chars)
var
  HexDigitalPID: array of cardinal;
  Des:   array[0..30] of char; // Length of Decoded Product Key + 1
  I:     integer;
  N:     integer;
  HN:    cardinal;
  Value: cardinal;
begin
  Result := '';
  SetLength(HexDigitalPID, DecodedLen);
  for I := StartOffset to EndOffset do
    HexDigitalPID[I - StartOffSet] := HexSrc[I];


  // SetLength(Des, DecodedLen + 1);


  for I := DecodedLen - 1 downto 0 do
    if (((I + 1) mod 6) = 0) then
      Des[I] := '-'
    else
    begin
      HN := 0;
      for N := EncodedLen - 1 downto 0 do
      begin
        Value := (HN shl 8) or HexDigitalPID[N];
        HexDigitalPID[N] := Value div 24;
        HN    := Value mod 24;
      end;
      Des[I] := Digits[HN];
    end;
  Des[DecodedLen] := Chr(0);


  //  For I := 0 To Length(Des) Do
  //    begin
  //     Result := Result + Des[I];
  //    end;
  Result := StrPas(Des);
end;

3 réponses

f0xi Messages postés 4205 Date d'inscription samedi 16 octobre 2004 Statut Modérateur Dernière intervention 12 mars 2022 35
22 janv. 2009 à 18:40
function DecodeMSKey(const HexSrc: array of byte): string;
const
  StartOffset = $34;              // Offset 0x34        (52)
  EndOffset   = StartOffset + 15; // Offset 0x34 + 0x0F (67)
  DecodedLen  = 29;               // Length of Decoded Product Key
  EncodedLen  = 15;               // Length of Encoded Product Key in Bytes (An total of 30 in chars)
  Digits      : array[0..23] of char ='BCDFGHJKMPQRTVWXY2346789';
var
  HexDigitalPID : array[0..EncodedLen] of cardinal;
  pRes          : PChar; // Length of Decoded Product Key + 1
  I, N          : integer;
  HN, Value     : cardinal;
begin
  SetLength(Result, DecodedLen);
  pRes := PChar(Result);
  { result := '                             ';
  }
 
  for I := StartOffset to EndOffset do            
    HexDigitalPID[I-StartOffSet] := HexSrc[I];  { I 52 to 67 15 iterations
    Hardcoded :
     HexDigitalPID[0]  := HexSrc[StartOffset+0]
     HexDigitalPID[1]  := HexSrc[StartOffset+1]
     HexDigitalPID[2]  := HexSrc[StartOffset+2]
     HexDigitalPID[3]  := HexSrc[StartOffset+3]
     HexDigitalPID[4]  := HexSrc[StartOffset+4]
     HexDigitalPID[5]  := HexSrc[StartOffset+5]
     HexDigitalPID[6]  := HexSrc[StartOffset+6]
     HexDigitalPID[7]  := HexSrc[StartOffset+7]
     HexDigitalPID[8]  := HexSrc[StartOffset+8]
     HexDigitalPID[9]  := HexSrc[StartOffset+9]
     HexDigitalPID[10] := HexSrc[StartOffset+10]
     HexDigitalPID[11] := HexSrc[StartOffset+11]
     HexDigitalPID[12] := HexSrc[StartOffset+12]
     HexDigitalPID[13] := HexSrc[StartOffset+13]
     HexDigitalPID[14] := HexSrc[StartOffset+14]
  }
      { I :28 downto 0 29 iterations
  }
  for I := DecodedLen-1 downto 0 do
  begin
    case I of
      23, 17, 11, 5 : pRes[I] := '-';
                     { result = '     -     -     -     -     '
                     }
      else
      begin
        HN := 0;
        for N := EncodedLen-1 downto 0 do
        begin
          Value            := (HN shl 8) or HexDigitalPID[N];
          HexDigitalPID[N] := Value div 24;
          HN               := Value mod 24;
          { pour cas HexDigitalPid[N] = 1
            N= 14; Value= 1;    HexDigitalPID[14]= 0;   HN= 1
            N= 13; Value= 257;  HexDigitalPID[13]= 10;  HN= 17
            N= 12; Value= 4353; HexDigitalPID[12]= 181; HN= 9
            N= 11; Value= 2305; HexDigitalPID[11]= 96;  HN= 1
            N= 10; Value= 257;  HexDigitalPID[10]= 10;  HN= 17
            N= 9;  Value= 4353; HexDigitalPID[9]= 181;  HN= 9
            N= 8;  Value= 2305; HexDigitalPID[8]= 96;   HN= 1
            N= 7;  Value= 257;  HexDigitalPID[7]= 10;   HN= 17
            N= 6;  Value= 4353; HexDigitalPID[6]= 181;  HN= 9
            N= 5;  Value= 2305; HexDigitalPID[5]= 96;   HN= 1
            N= 4;  Value= 257;  HexDigitalPID[4]= 10;   HN= 17
            N= 3;  Value= 4353; HexDigitalPID[3]= 181;  HN= 9
            N= 2;  Value= 2305; HexDigitalPID[2]= 96;   HN= 1
            N= 1;  Value= 257;  HexDigitalPID[1]= 10;   HN= 17
            N= 0;  Value= 4353; HexDigitalPID[0]= 181;  HN= 9
          }
        end;
        pRes[I] := Digits[HN];
        { result = 'PPPPP-PPPPP-PPPPP-PPPPP-PPPPP'
        }
      end;
    end;
    inc(pRes);
  end;
end;




<hr size="2" width="100%" />
0
cs_davidc Messages postés 4 Date d'inscription mardi 29 avril 2003 Statut Membre Dernière intervention 25 janvier 2009
22 janv. 2009 à 18:50
Merci beaucoup pour cette décortication du code je vais le regarder à tête reposée c'est génial. merci encore
0
cs_davidc Messages postés 4 Date d'inscription mardi 29 avril 2003 Statut Membre Dernière intervention 25 janvier 2009
25 janv. 2009 à 00:15
Bonsoir, j'ai bien regardé votre explication et je ne comprends pas tres biens comment se calcule le Value := (HN shl 8) or HexDigitalPID[N]; en effet comment  calculerait-on avec une HexDigitalPID = 01 CC 06 C7 A0 5D 84 2A 87 C1 EB CC 75 04 80, valeurs hexadécimale ? Merci
0
Rejoignez-nous