已知一个字符解密函数如下,请写出相应的加密函数,高手请出招。function decrypt(key:word;str: string): string;
var
  i: integer;
  j, tt, tt2: word;
  tmp: string;
  tmpword: array of word;begin
  tt2 := key; 
  setlength(tmpword, length(str) div 2);
  for i := 0 to length(tmpword) - 1 do
  begin
    tmpword[i] := strtoint('$' + str[i * 2 + 1] + str[i * 2 + 2]);
  end;
  for i := 0 to length(tmpword) - 1 do
  begin
    tt := tt2;
    tt := tt shr 8;
    j := tmpword[i] xor tt;
    if j > 0 then
    begin
      tmp := tmp + char(j);
      tt := tt2 + tmpword[i];
      j := tt * $0B;
      j := j + $0C;
      tt2 := j;
    end
    else
    begin
      j := tmpword[i];
      tmp := tmp+char(j);
      j := tt2 * $0B;
      j := j + $0C;
      tt2 := j;
    end;  end;  result := tmp;
end;

解决方案 »

  1.   

    仓促写了一个函数,你先试一下。(测试数据:key==256 , 解密前: 4444)
    其中,一些变量我是照你的题目复制而来,用得可能有些乱;再就是,还没有写“位移并异或”后<0的处理,所以,请仅测试4位十进数密码(加密后,即加密前2位)function Tform1.enCrypt(key:word; str: string): string;
    var
      i,LenStr: integer;
      tt, tt2: word;
      tmp: string;
      tmpword: array of word;
    begin
      tt2 := key;
      LenStr := length(str);
      setlength(tmpword, LenStr);
      for i := 0 to LenStr-1 do
      begin
        tt := tt2;
        tt := tt shr 8;
        tmpword[i] := ord(str[i+1]) xor tt;
        tt := tt2 + tmpword[i];
        tt := tt * $0B;
        tt := tt + $0C;
        tt2 := tt;
      end;
      tmp:='';
      for i:=0 to LenStr-1 do
          tmp:= tmp + inttohex(tmpword[i],2);
      result := tmp;
    end;
      

  2.   

    function encrypt(key: Word; str: string): string;
    var
      i: Integer;
      j, tt: Word;
    begin
      for I := 1 to Length(str) do
      begin
        tt := key;
        tt := tt shr 8;
        j := Byte(str[I]) xor tt;
        if j > 0 then
        begin
          Result := Result + Format('%.2x', [j]);
          tt := key + j;
          j := tt * $0B;
        end
        else
        begin
          j := Byte(str[I]);
          Result := Result + Format('%.2x', [j]);
          j := key * $0B;
        end;
        j := j + $0C;
        key := j;
      end;
    end;  for i := 0 to length(tmpword) - 1 do
      begin
        tmpword[i] := strtoint('$' + str[i * 2 + 1] + str[i * 2 + 2]);
      end;
    上面代码可以分析出加密后是十六进制字符串  tt := tt shr 8;
    之后分析tt掩码的计算
      

  3.   

    function enCrypt(key:word; str: string): string;
    var
      i,LenStr: integer;
      tt, tt2: word;
      tmp: string;
      tmpword: array of word;
    begin
      tt2 := key;
      LenStr := length(str);
      setlength(tmpword, LenStr);
      for i := 0 to LenStr-1 do
      begin
        tt := tt2;
        tt := tt shr 8;
        tmpword[i] := ord(str[i+1]) xor tt;
        if i<4 then
        begin
        tt := tt2 + tmpword[i];
        tt := tt * $0B;
        end
        else
        begin
          tt := tt2 * $0B;
        end;
        tt := tt + $0C;
        tt2 := tt;
      end;
      tmp:='';
      for i:=0 to LenStr-1 do
          tmp:= tmp + inttohex(tmpword[i],2);
      result := tmp;
    end;
      

  4.   

    强烈鄙视问题解决后不结贴的人!
    强烈鄙视技术问题解决后把贴子转移到非技术区的人!
    鄙视你们!http://community.csdn.net/Expert/topic/5216/5216675.xml?temp=.9262659