在delphibbs中找了一个XOR加密的例子,但是很奇怪,我
对同一个值的两个字符串使用这个XOR加密,居然结果不一样
请朋友们帮我看看,是错在哪里 ?谢谢!
const C1=42845;
      C2=22719;
...
procedure TForm1.Button1Click(Sender: TObject);
var
  str,str2:string;
begin
   str:='12345';
   str2:='12345';
   Memo1.Lines.Add(str);
   Memo1.Lines.Add(str2);
   str:=Encrypt(str,998);
   str2:=Encrypt(str2,998);
   Memo1.Lines.Add(str);
   Memo1.Lines.Add(str2);
   str:=DEcrypt(str,998);
   str2:=DEcrypt(str2,998);
   Memo1.Lines.Add(str);
   Memo1.Lines.Add(str2);
   //Memo1上的str与str2的处理结果居然不一样!
end;
function DEcrypt(S:String;Key:word):string;
var
   i:Integer;
   temp:word;
begin
   Setlength(Result,Length(S));
   For i:=1 to Length(s) do
      begin
      temp:=byte(Result[i]);
      Result[i]:=char(byte(S[i])xor(key shr 8));
      Key:=(temp+Key)*C1+C2;
      end;
end;
function Encrypt(Const S:String;Key:word):string;
var
   i:Integer;
begin
   Setlength(Result,Length(S));
   For i:=1 to Length(s) do
      begin
      Result[i]:=char(byte(S[i])xor(key shr 8));
      Key:=(byte(Result[i])+Key)*C1+C2;
      end;
end;

解决方案 »

  1.   

    我这个一定OK的
    function Encrypt(const str: string; Key1,Key2:word): string;
    var i:byte;  key: integer;
    begin   //encrypt string
      key:=CommonKey;
      result:=str;
      for i:=1 to length(str) do
        begin
          result[i]:=char(byte(str[i]) xor (key shr 8));
          key:=(byte(result[i])+key)*key1+key2;
        end;
    end;function Decrypt(const str: string; Key1,Key2:word): string;
    var i:byte; key: integer;
    begin  //decrypt string
      key:=CommonKey;
      result:=str;
      for i:= 1 to length(str) do
        begin
          result[i]:=char(byte(str[i]) xor (key shr 8));
          key:=(byte(str[i])+key)*key1+key2;
        end;
    end;
      

  2.   

    谢谢ly_liuyang,可是我想知道我原来那个错在哪里。
    再次感谢ly_liuyang.
      

  3.   

    ★我是初学者,想和大家交个朋友,我的yahoo ID是onlydelphi,我们用yahoo通聊吧!(推荐!
    请到:
    http://cn.yahoo.com 下载yahoo通,然后申请个免费yahoo电邮就行了)
    我的MSN是:[email protected]
    我感觉yahoo通比MSN好很多,不信你试试。
    请留下你的ID,谢谢!
      

  4.   

    function TMyClass.Crypt(s: string; Key: Word;
      const bEncrypt: boolean): string;  //将字符串加密解密,bEncrypt=True表示加密 False表示解密
    const
      SeedA = 56789; /// 常量,你可以修改
      SeedB = 54329; /// 常量,你可以修改
    var
      i: integer;
      ps, pr : ^byte;
    begin
      if bEncrypt then
        s := s+#0;
      SetLength(Result, Length(s));
      ps := @s[1];
      pr := @Result[1];
      for i := 1 to length(s) do
      begin
        pr^ := ps^ xor (Key shr 8);
        if bEncrypt then
          Key := (pr^ + Key) * SeedA + SeedB
        else
          Key := (ps^ + Key) * SeedA + SeedB;
        pr := pointer(integer(pr) + 1);
        ps := pointer(integer(ps) + 1);
      end;
    end;
      

  5.   

    Setlength(Result,Length(S));//here要学会跟踪调试代码,这是基础
      

  6.   

    unit Eds; {Encrypt & Decrypt String}interfaceuses
      SysUtils;const
      StartKey = 973;   {Start default key}
      MultKey   = 790314; {Mult default key}
      AddKey   = 23916; {Add default key}function EncryptString(s: string): string; //Encrypt to number
    function DecryptString(s: string): string; //Decrypt from numberfunction EncodeString(s: string): string; //Encrypt to character
    function DecodeString(s: string): string; //Decrypt from characterimplementation{$R-}
    {$Q-}
    function Encrypt(const InString: string; StartKey, MultKey, AddKey: Integer): string;
    var
      I : Byte;
    begin
      Result := '';
      for I := 1 to Length(InString) do
      begin
        Result := Result + CHAR(Byte(InString[I]) xor (StartKey shr 8));
        StartKey := (Byte(Result[I]) + StartKey) * MultKey + AddKey;
      end;
    end;function Decrypt(const InString: string; StartKey, MultKey, AddKey: Integer): string;
    var
      I : Byte;
    begin
      Result := '';
      for I := 1 to Length(InString) do
      begin
        Result := Result + CHAR(Byte(InString[I]) xor (StartKey shr 8));
        StartKey := (Byte(InString[I]) + StartKey) * MultKey + AddKey;
      end;
    end;
    {$R+}
    {$Q+}function EncodeString(s: string): string;
    begin
    Result := Encrypt(s, StartKey, MultKey, AddKey);
    end;function DecodeString(s: string): string;
    begin
    Result := Decrypt(s, StartKey, MultKey, AddKey);
    end;Function Int2Str(int1: Integer; Len: Integer): string;
    var
       i, j: integer;
    begin
    if Length(inttostr(int1)) >= Len then Result:=Inttostr(int1)
    else
      begin
    Result := '';
    i := Len - Length(IntToStr(Int1));
    for j := 1 to i do Result := Result + '0';
    Result := Result + IntToStr(Int1);
      end;
    end;function Char2ByteStr(s: string): string;
    var
    i: Byte;
    begin
    Result:='';
    for i := 1 to Length(s) do Result := Result + Int2Str(Byte(s[i]), 3);
    end;function Byte2CharStr(s: string): string;
    var
    i: Integer;
    begin
    i := 1;
    Result := '';
    if (Length(s) mod 3) = 0 then
      while i < Length(s) do
      begin
        Result := Result + Char(StrToInt(Copy(s, i, 3)));
        i := i + 3;
      end;
    end;function EncryptString(s: string): string;
    var
    years, months, days, hours, mins, secs, msec: Word;
    Sk, Mk, Ak: Longint;
    begin
    DecodeDate(Now, years, months, days);
    DecodeTime(Now, hours, mins, secs, msec);
    Sk := msec;
    if Sk < 256 then Sk:= Sk + 256;
    Mk := ((years - 1900) * 12 + months) * 30 + days + Sk * 10 + Sk;
    Ak := (23 * hours + mins) * 60 + secs + Sk * 10 + Sk;
    Result := Char2ByteStr(Encrypt(Int2Str(Sk, 3), StartKey, MultKey, AddKey)) +
    Char2ByteStr(Encrypt(Int2Str(Mk, 5), StartKey, MultKey, AddKey)) +
    Char2ByteStr(Encrypt(Int2Str(Ak, 5), StartKey, MultKey, AddKey)) +
    Char2ByteStr(Encrypt(s, Sk, Mk, Ak));
    end;function DecryptString(s: string): string;
    var
    Sk, Mk, Ak: Longint;
    begin
    Sk := StrToInt(Decrypt(Byte2CharStr(Copy(s, 1, 9)), StartKey, MultKey, AddKey));
    Mk := StrToInt(Decrypt(Byte2CharStr(Copy(s, 10, 15)), StartKey, MultKey, AddKey));
    Ak := StrToInt(Decrypt(Byte2CharStr(Copy(s, 25, 15)), StartKey, MultKey, AddKey));
    Result := Decrypt(Byte2CharStr(Copy(s, 40, Length(s)-39)), Sk, Mk, Ak);
    end;end.