如题

解决方案 »

  1.   

    RC4 算法是RSA公司的,不过没有注册版权。
    不知谁有详细的原理说明。
      

  2.   

    #include <stdio.h>#define buf_size 1024typedef struct rc4_key
    {      
       unsigned char state[256];       
       unsigned char x;        
       unsigned char y;
    } rc4_key;#define swap_byte(x,y) t = *(x); *(x) = *(y); *(y) = tvoid prepare_key(unsigned char *key_data_ptr, int key_data_len, rc4_key *key)
    {
      int i;
      unsigned char t;
      unsigned char swapByte;
      unsigned char index1;
      unsigned char index2;
      unsigned char* state;
      short counter;  state = &key->state[0];
      for(counter = 0; counter < 256; counter++)
      state[counter] = counter;
      key->x = 0;
      key->y = 0;
      index1 = 0;
      index2 = 0;
      for(counter = 0; counter < 256; counter++)
      {
        index2 = (key_data_ptr[index1] + state[counter] + index2) % 256;
        swap_byte(&state[counter], &state[index2]);
        index1 = (index1 + 1) % key_data_len;
      }
    }void rc4(unsigned char *buffer_ptr, int buffer_len, rc4_key *key)
    {
      unsigned char t;
      unsigned char x;
      unsigned char y;
      unsigned char* state;
      unsigned char xorIndex;
      short counter;  x = key->x;
      y = key->y;
      state = &key->state[0];
      for(counter = 0; counter < buffer_len; counter++)
      {
        x = (x + 1) % 256;
        y = (state[x] + y) % 256;
        swap_byte(&state[x], &state[y]);
        xorIndex = (state[x] + state[y]) % 256;
        buffer_ptr[counter] ^= state[xorIndex];
      }
      key->x = x;
      key->y = y;
    }int main(int argc, char* argv[])
    {
      char seed[256];
      char data[512];
      char buf[buf_size];
      char digit[5];
      int hex, rd,i;
      int n;
      rc4_key key;  if (argc < 2)
      {
        fprintf(stderr,"%s key <in >out\n",argv[0]);
        exit(1);
      }
      strcpy(data,argv[1]);
      n = strlen(data);
      if (n&1)
      {
        strcat(data,"0");
        n++;
      }
      n/=2;
      strcpy(digit,"AA");
      for (i=0;i<n;i++)
      {
        digit[2] = data[i*2];
        digit[3] = data[i*2+1];
        sscanf(digit,"%x",&hex);
        seed[i] = hex;
      }  prepare_key(seed,n,&key);
      rd = fread(buf,1,buf_size,stdin);
      while (rd>0)
      {
        rc4(buf,rd,&key);
        fwrite(buf,1,rd,stdout);
        rd = fread(buf,1,buf_size,stdin);
      }
    }
      

  3.   

    The RC5 encryption algorithm is a new fast symmetric block cipher 
    developed by Ron Rivest for RSA Laboratories. Intended as a possible 
    successor to the Data Encryption Standard, RC5 has a variable word 
    size, a variable number of rounds, and a variable-length secret key, 
    so the user can explicitly manipulate the trade-off between higher 
    speed and higher security. RC5 is suitable for hardware or software 
    implementations; the encryption and decryption algorithms are 
    exceptionally simple. A novel feature of RC5 is the heavy use of 
    data-dependent rotations.
      

  4.   

    谢谢crossbow(La Vida Es Amor) 
    有没有原理说明?
      

  5.   

    VB版rc4算法Public Sub main()
    Dim key As String
    For i = 1 To 16
    Randomize
    key = key & Chr(Rnd * 255)
    Next i
    MsgBox RC4(RC4("Welcome To Plindge Studio!", key), key)
    End Sub
    Public Function RC4(inp As String, key As String) As String
    Dim S(0 To 255) As Byte, K(0 To 255) As Byte, i As Long
    Dim j As Long, temp As Byte, Y As Byte, t As Long, x As Long
    Dim Outp As String For i = 0 To 255
    S(i) = i
    Nextj = 1
    For i = 0 To 255
    If j > Len(key) Then j = 1
    K(i) = Asc(Mid(key, j, 1))
    j = j + 1
    Next ij = 0
    For i = 0 To 255
    j = (j + S(i) + K(i)) Mod 256
    temp = S(i)
    S(i) = S(j)
    S(j) = temp
    Next ii = 0
    j = 0
    For x = 1 To Len(inp)
    i = (i + 1) Mod 256
    j = (j + S(i)) Mod 256
    temp = S(i)
    S(i) = S(j)
    S(j) = temp
    t = (S(i) + (S(j) Mod 256)) Mod 256
    Y = S(t)Outp = Outp & Chr(Asc(Mid(inp, x, 1)) Xor Y)
    Next
    RC4 = Outp
    End Function 
      

  6.   

    这是我自己编的RC4的Delphi实现。
    加密再解密与源码不同,真是郁闷,请高手帮我看看由什么问题???-----------------------------------------------------------------------------
    Unit RC4;interfaceuses SysUtils, Classes;type
      TRC4 = class(TObject)
      Private
        S_Box: array[Low(Byte)..High(Byte)] of Byte;      {类RC4算法S-盒}
        R_Box: array[Low(Byte)..High(Byte)] of Byte;      {密文或明文数组}
        Key: String;                                      {类RC4算法密钥}
        Code: String;                                     {密文或明文}    procedure Set_Key;                                {类RC4算法初始化}
        procedure Set_Code;                               {计算密文或明文}
        function Get_Code():String;                       {获得密文或明文}
      public
        constructor Create;
        property sKey: String read Key write Key;
        property sCode: String read Get_Code write Code;
    end;
    implementation{-------------------------------------------------------------------------------
    类  名:TRC4
    方法名:Create
    返回值:无
    参  数:无
    功  能:构造器
    -------------------------------------------------------------------------------}
    constructor TRC4.Create;
    begin
    end;{-------------------------------------------------------------------------------
    类  名:TRC4
    方法名:Set_Key
    返回值:无
    参  数:无
    功  能:初始化S-盒(参考RC4算法)
    -------------------------------------------------------------------------------}
    procedure TRC4.Set_Key;
    var
      J,KeyLength: Word;
      I,SwapByte: Byte;
      K_Box: array[Low(Byte)..High(Byte)] of Byte;
    begin
      I := 0;
      J := 0;
      KeyLength := Length(Key);
      for I:= Low(Byte) to High(Byte) do begin
        S_Box[I] := I;
        K_Box[I] := Byte(Key[(I mod KeyLength) + 1]);
      end;
      for I := Low(Byte) to High(Byte) do begin
        J := (J + S_Box[I] + K_Box[I]) mod (High(Byte)+1);
        SwapByte := S_Box[I];
        S_Box[I] := S_Box[J];
        S_Box[J] := SwapByte;
      end;
    end;{Set_Key}{-------------------------------------------------------------------------------
    类  名:TRC4
    方法名:Set_Code
    返回值:无
    参  数:无
    功  能:计算密文或明文(参考RC4算法)
    -------------------------------------------------------------------------------}
    procedure TRC4.Set_Code;
    var
      I,J,T,SwapByte: Byte;
      X: WORD;
    begin
      Set_Key;  I := 0;
      J := 0;  for X := Low(Byte) to Length(Code)-1 do begin
        I := (I + 1) mod (High(Byte)+1);
        J := (J + S_Box[I]) mod (High(Byte)+1);
        SwapByte := S_Box[I];
        S_Box[I] := S_Box[J];
        S_Box[J] := SwapByte;
        T := (S_Box[I]+ S_Box[J]) mod (High(Byte)+1);
        R_Box[X] := Byte(Code[X]) xor S_Box[T];
      end;end;{Set_Code}{-------------------------------------------------------------------------------
    类  名:TRC4
    方法名:Get_Code
    返回值:密文或明文
    参  数:无
    功  能:获得密文或明文
    -------------------------------------------------------------------------------}
    function TRC4.Get_Code():String;
    var
      I: Byte;
    begin
      Set_Code;  for I := Low(Byte) to Length(Code)-1 do begin
          Result := Result + AnsiChar(R_Box[I]);
      end;
    end;{Get_Code}end.
      

  7.   

    已找到RC4算法的简单描述,请帮我看看上述Delphi实现有什么问题?
    算法如下:
    --------------------------------------------------------------------------------
    1、初始化
    原文:初始化S-盒也很容易。首先,进行线性填充:S[0]=1,S[1]=1,...,S[255]=255。然后用密钥填充另一个256字节数组,不断重复密钥直至填充到整个数组中:K[0],K[1],...,K[255]。
    将指针J设为0。然后:
        对于I=0至255
        J=(J+S[I]+K[I]) mod 256
        交换S[I]和S[J]
    2、加密或解密
    原文:它有一个8*8的S-盒:S[0],S[1],...,S[255]。所有项都是数字0到数字255的置换,并且这个置换是一个可变长度密钥的函数。它有两个计数器:I和J,初值为0。
    要产生一个随机字节,需要按下列步骤进行:
        I=(I+1) mod 256
        J=(J+S[I]) mod 256
        交换S[I]和S[J]
        T=(S[I]+S[J]) mod 256
        K=S[T]
        字节K与明文异或产生密文或者与密文异或产生明文。
    ----------------------------------------------------------------------------
      

  8.   

    呵呵,有Code的
    const
      RC4_BufSize = 16;class procedure TCipher_RC4.GetContext(var ABufSize, AKeySize, AUserSize: Integer);
    begin
      ABufSize := RC4_BufSize;
      AKeySize := 256;
      AUserSize := 256 * 2;
    end;class function TCipher_RC4.TestVector: Pointer;
    asm
             MOV   EAX,OFFSET @Vector
             RET
    @Vector: DB    02Dh,08Fh,0EEh,042h,087h,07Bh,0AEh,072h
             DB    0F8h,02Bh,08Ch,0A5h,012h,014h,0A8h,07Eh
             DB    07Eh,08Ch,0DBh,05Eh,096h,049h,06Ch,09Ch
             DB    0EEh,05Eh,020h,06Ch,07Ah,067h,002h,05Dh
    end;procedure TCipher_RC4.Encode(Data: Pointer);
    var
      D: PByteArray;
      B: PByte;
      X,S: Byte;
    begin
      D := User;
      B := Data;
      for X := 0 to RC4_BufSize -1 do
      begin
        Inc(FI);
        S := D[FI];
        Inc(FJ, S);
        D[FI] := D[FJ];
        D[FJ] := S;
        B^    := B^ xor D[(D[FI] + S) and $FF];
        Inc(B);
      end;
    end;procedure TCipher_RC4.Decode(Data: Pointer);
    begin
      Encode(Data);
    end;procedure TCipher_RC4.Init(const Key; Size: Integer; IVector: Pointer);
    var
      K: array[0..255] of Byte;
      D: PByteArray;
      I,J,S: Integer;
    begin
      InitBegin(Size);
      FI := 0;
      FJ := 0;
      J := 0;
      D := User;
      for I := 0 to 255 do
      begin
        D[I] := I;
        K[I] := PByteArray(@Key)[I mod Size];
      end;
      for I := 0 to 255 do
      begin
        J := (J + D[I] + K[I]) and $FF;
        S := D[I];
        D[I] := D[J];
        D[J] := S;
      end;
      InitEnd(IVector);
    {Save the Key}
      Move(D[0], D[256], 256);
      FSI := FI;
      FSJ := FJ;
      FillChar(K, SizeOf(K), 0);
    end;procedure TCipher_RC4.Done;
    begin
      inherited Done;
      FI := FSI;
      FJ := FSJ;
      Move(PByteArray(User)[256], User^, 256);
    end;