RC4 in Delphi and C?



我已经设法将RC4实现从PolarSSL移植到delphi,因为我需要2个应用程序(C和delphi)之间的加密通信,但问题是,加密的数据永远不一样,两个代码加密和解密数据自己成功,但不是由另一个加密的数据。

下面是两个代码:

C代码(取自PolarSSL)

typedef struct
{
    int x;                      /*!< permutation index */
    int y;                      /*!< permutation index */
    unsigned char m[256];       /*!< permutation table */
}
arc4_context;
void arc4_setup(arc4_context *ctx, unsigned char *key, int keylen)
{
    int i, j, k, a;
    ctx->x = 0;
    ctx->y = 0;
    for( i = 0; i < 256; i++ ) ctx->m[i] = (unsigned char) i;
    j = k = 0;
    for( i = 0; i < 256; i++, k++ )
    {
        if( k >= keylen ) k = 0;
        a = ctx->m[i];
        j = ( j + a + key[k] ) & 0xFF;
        ctx->m[i] = ctx->m[j];
        ctx->m[j] = (unsigned char) a;
    }
    return;
}
void arc4_crypt( arc4_context *ctx, unsigned char *buf, int buflen )
{
    int i, x, y, a, b;
    unsigned char m[256];
    x = ctx->x;
    y = ctx->y;
    for (i = 0; i < 256; i++) m[i] = ctx->m[i];
    for( i = 0; i < buflen; i++ )
    {
        x = ( x + 1 ) & 0xFF; a = m[x];
        y = ( y + a ) & 0xFF; b = m[y];
        m[x] = (unsigned char) b;
        m[y] = (unsigned char) a;
        buf[i] = (unsigned char)
            ( buf[i] ^ m[(unsigned char)( a + b )] );
    }
    return;
}

My Delphi Code:

type
  arc4_context = packed record
    x, y: integer;
    m: array[0..255] of byte;
  end;
procedure arc4_setup(var ctx: arc4_context; key: PChar; keylen: Integer);
var
 i, j, k, a: Integer;
begin
 ctx.x := 0;
 ctx.y := 0;
 for i := 0 to 255 do ctx.m[i] := Byte(i);
 j := 0;
 k := 0;
 for i := 0 to 255 do
 begin
   if (k >= keylen) then k := 0;
   a := ctx.m[i];
   j := (j + a + Byte(key[k])) and $FF;
   ctx.m[i] := ctx.m[j];
   ctx.m[j] := a;
   Inc(k);
 end;
end;

procedure arc4_crypt(ctx:arc4_context; var buf:string; buflen:integer);
var
 i, x, y, a, b: Integer;
 m: array [0..255] of byte;
begin
 x := ctx.x;
 y := ctx.y;
 for i := 0 to 255 do m[i] := ctx.m[i];
 i := 0;
 while (i < buflen) do
 begin
  x := (x + 1) and $FF;
  a := m[x];
  y := (y + a) and $FF;
  b := m[y];
  m[x] := b;
  m[y] := a;
  buf[i+1] := Char(Byte(buf[i+1]) xor Byte(m[a + b]));
  inc(i);
 end
end;

我(终于)发现了两个代码之间的差异。

Pascal翻译的下面一行是不正确的:

buf[i+1] := Char(Byte(buf[i+1]) xor Byte(m[a + b]));

C版本为:

buf[i] = (unsigned char) ( buf[i] ^ m[(unsigned char)( a + b )] );

注意,a + b被截断为单个unsigned char,而上面的Pascal版本是m[a + b],因此a + b的索引可以超过255。

你应该把这行翻译成:

buf[i+1] := chr(ord(buf[i+1]) xor ord(m[Byte(a+b)]));

我已经改为使用Chrord,这是化妆品的变化,但我觉得他们更干净。实质性的变化是在m[Byte(a+b)]中,我将a+b添加到字节数据类型的上下文中。

很明显,这个错误会导致数组m的越界访问。如果您在运行时启用了范围检查,则该错误将立即突出显示。我再怎么强调Delphi的范围检查功能的价值也不为过。

建议:在处理密钥之后,但在加密任何数据之前,查看两个系统上m[]数组的内容。显然这两个应该是相同的。如果不是,那么问题出在密钥处理上。

您可能还希望对两个不同的输出进行异或,以查看是否出现任何可能指向问题的模式。

下面是该算法的delphi实现,翻译自。net:

unit uRC4;
interface
uses Windows;
type
  TuRC4 = class
    public
      class function RC4(data, key:string):string;
  end;
implementation
class function TuRC4.RC4(data, key:string):string;
var
  x, y, j: Integer;
  box: array[0..255] of Integer;
  i: Integer;
  s: String;
begin
    for i := 0 to 255 do
      begin
        box[i] := i;
      end;
    for i := 0 to 255 do
      begin
        j := (Ord(key[i Mod Length(key) + 1]) + box[i] + j) Mod 256;
        x := box[i];
        box[i] := box[j];
        box[j] := x;
      end;
    for i := 0 to Length(data)-1 do
      begin
        y := i Mod 256;
        j := (box[y] + j) Mod 256;
        x := box[y];
        box[y] := box[j];
        box[j] := x;
        s := Char(Ord(data[i + 1]) xor box[(box[y] + box[j]) Mod 256]);
        Result := Concat(Result, s);
      end;
end;
end.

为什么要重新发明轮子?*

我知道DCPCrypt支持RC4.

*)允许用于学术目的

编辑

最新更新