结转数组中递增的字节

Carry over incrementing bytes in an array

我正在测试我们的加密被破解的难易程度(或速度)。加密密钥是一个字节数组(可以是任何可变长度)。因此,从一个全为 0 的数组开始,我需要从第一个字节开始每次递增一个字节。当任何字节达到其最大值并返回到 0 时,数组中的下一个字节需要递增(结转)。

如果数组是固定长度的短数组会很简单 - 但我不知道如何在可变数组长度中这样做。

线程内声明的变量:

FKey: array of Byte;

每次线程迭代后,它调用此过程将密钥递增一个字节:

procedure TCrackThread.NextKey;
begin
  //Increment first byte
  //If max, reset to 0 and increment next byte
  //Recursively check the same for each following byte in array
end;

我如何递增(从第一个开始)字节并转移到这个可变长度数组中的下一个字节?

此代码段将增加第一个元素,并将继续对数组元素执行此操作,只要它们是 255。如果是这样,它们将重置为零。一旦条件不满足或索引达到最大值,例程将立即停止。

var
  k: Integer;
...
if (Length(fKey) > 0) then begin
  k := 0;
  Inc(fKey[k]);
  while (fKey[k] = 255) do begin
    fKey[k] := 0;
    Inc(k);
    if (k >= Length(fKey)) then
      break;
    Inc(fKey[k]);
  end;
end;

这会将数组 254,0,0 转换为 0,1,0

如果你想让一个进位在第一个增量之前波动, 此代码将执行此操作:

procedure Next;
var
  k: Integer;
  carry: Boolean;
begin
  if (Length(fKey) > 0) then begin
    k := 0;
    repeat
      carry := (fKey[k] = 255);
      if carry then begin
        fKey[k] := 0;
        Inc(k);
        if (k >= Length(fKey)) then
          break;
      end
      else
        Inc(fKey[k]);
    until not carry;
  end;
end;

这会将 255,255,0 转换为 0,0,1。