已知公式如下:
a := (((b xor $eb) and $f0) shr 2) xor ((b xor $eb) and 3);现在在得到a值的情况下,如下得到b值?

解决方案 »

  1.   

    ((b xor $eb) and $f0) shr 2) xor ((b xor $eb) and 3);
    (((B XOR  11101011) AND 11110000) SHR 2) XOR ((B XOR 11101011) AND 00000011);
    ==>
    (((B AND 11110000) XOR 11100000) SHR 2) XOR ((B AND 3) XOR (11101011 AND 00000011))
    ==>
    ((High(B) XOR 1110) SHR 2) XOR ( Low(B,2) XOR 11)
    ==>
    (High(B,2) AND 11) XOR (Low(B,2) XOR 11)
    ==>
    (High(B,2) AND 11) XOR (NOT Low(B,2))
    ==>
    a:=(High(B,2) AND 11) XOR (NOT Low(B,2));
    很显然,函数a的值只与B的高2位和低2位有关,中间4位可以取任意结果,所以函数a不是满射函数,所以a没有反函数。也就是无法通过存在a的情况下,得到b的值
      

  2.   

    这个题应是无解。因为a和b不是一一对应的,而是多对一的。
    如:
      a=$e5 和a=$e1时,b均等于$02。
    实际上,每个b对应4个a
      a=0000XX00
    X是任意值,0是确定的数。即,第3位和第4位是任意的。
      

  3.   

    支持 SuanAddMiao(算苗) 的看法没条件限制的话,本方程没唯一解的
      

  4.   

    我实际上是手上有一段加密的函数。要根据它写出解密的函数。贴出来:
    function Encode(Source: string): string;
    var
      i,outCount,bCount: integer;
      b1,b4: Byte;
      sByteArray,dByteArray : array of Byte;
    begin
      //将输入字符串转换为字节数组
      SetLength(sByteArray, Length(Source));
      Move(Source[1], sByteArray[0], Length(Source));
      SetLength(dByteArray, 0);  outCount := 0;
      bCount := 0;
      b1 := 0;
      b4 := 0;
      for i := 0 to Length(sByteArray)-1 do
      begin
        b1 := (sByteArray[i]) xor $eb;                  //将每个字节与$eb异或
        if bCount = 2 then
        begin
          SetLength(dByteArray, Length(dByteArray)+2);
          dByteArray[outCount] := (b1 and $3f)+$3b;
          outCount := outCount + 1;
          b4 := b4 xor ((b1 shr 2) and $30);
          dByteArray[outCount] := b4 + $3b;
          outCount := outCount + 1;
          bCount := 0;
          b4 := 0;
        end
        else
        begin
          SetLength(dByteArray, Length(dByteArray)+1);
          dByteArray[outCount] := (((b1 and $f0) shr 2) xor (b1 and 3))+$3b;
          outCount := outCount + 1;
          b4 := b4 shl 2;
          b4 := b4 xor ((b1 shr 2) and 3);
          bCount := bCount +1;
        end;
      end;
      if bCount <> 2 then
      begin
        SetLength(dByteArray, Length(dByteArray)+1);
        dByteArray[outCount] := b4 + $3b;
        outCount := outCount + 1;
      end;  for i := 0 to Length(dByteArray)-1 do
      begin
        Result := Result + Chr(dByteArray[i]);
      end;
    end;怎么写出解密的函数。并且肯定是可以解密的。
      

  5.   

    继续推导:
    a:=(High(B,2) AND 11) XOR (NOT Low(B,2));
    ==>Low(B,2):=NOT ((High(B,2) AND 11) XOR A)
    ==>(High(B,2) AND 11):=(NOT Low(B,2)) XOR A
    ==>High(B,2):=(NOT Low(B,2)) XOR A令 HIGH(B,2)=n, 其中n in 00..11
    ==>NOT LOW(B,2)=A XOR n
    ==>LOW(B,2)=NOT (A XOR N)
    ==>B=(N SHL 7) OR (NOT (A XOR N)) OR (M SHL 3),其中M in 0000..1111
    所以当A一定的时候,B有 Sizeof(00..11) * Sizeof(0000..1111)=2^2 * 2^4=2^6个
    但我怎么计算结果都是错的,我哪里推倒错了那?
      

  6.   

    郁闷:
    A=(High(B,2) AND 11) XOR (NOT Low(B,2))
    B=(N SHL 7) OR (NOT (A XOR N)) OR (M SHL 3)
    将B带入A中:
    A=(High((N SHL 7) OR (NOT (A XOR N)) OR (M SHL 3),2) AND 11) XOR (NOT Low(
    (N SHL 7) OR (NOT (A XOR N)) OR (M SHL 3),2))
    ==>
    A=(N AND 11) XOR (NOT( NOT (A XOR N)))
    ==>
    A=(N) XOR(A XOR N)
    ==>
    A=(A XOR N) XOR N
    ==>
    A=A
    所以我推倒出来的反函数是正确的。
      

  7.   

    Eastunfail(龙子龙孙)==(恶鱼杀手):
    可以写成dlephi源码吗?看的我一愣一愣的。
      

  8.   

    type TTwoBits   =0..3;
    type TFourBits  =0..15;
    type TDigitRange=0..8;
    function High(b:Byte;digit:TDigitRange):Byte;
    begin
      Result:= b shr (8-digit);
    end;
    function Low(b:Byte;digit:TDigitRange):Byte;
    begin
      Result:= (b shr digit shl digit xor b);
    end;
    function b(a:byte;n:TTwoBits;m:TFourBits):byte;
    begin
     Result:=(N SHL 7) OR (NOT (A XOR N)) OR (M SHL 3)
    end;
    function a(b:byte):byte;
    begin
      Result:= High(B,2) XOR (NOT Low(B,2))
    end;b函数则是已知a求b,根据理论推倒,n和m可以为任意值。
    a函数是从b到a的简化过程,是从你给的公式推倒出来的
      

  9.   

    加密机制我已经搞清楚了,在这里说不清楚。但你的那个加密程序在“<>2”那段有点问题,按那样算,就象我第一个贴子那样,明文与密文是多对一的关系,我把它也改了,但不知道你是不是确实需要这样?如果确实是那样的话,我还可以再改(看能不能帮你不丢工作)。代码如下:
    function Encode(Source: string): string;  //加密
    var
      i,outCount,bCount: integer;
      b1,b4: Byte;
      sByteArray,dByteArray : array of Byte;
    begin
      SetLength(sByteArray, Length(Source));
      Move(Source[1], sByteArray[0], Length(Source));
      SetLength(dByteArray, 0);
      outCount := 0;
      bCount := 0;
      b4 := 0;
      for i := 0 to Length(sByteArray)-1 do
        begin
          b1 := (sByteArray[i]) xor $eb; //将每个字节与$eb异或
          if bCount = 2 then
            begin
              SetLength(dByteArray, outCount+2);
              dByteArray[outCount] := (b1 and $3f)+$3b;
              outCount := outCount + 1;
              b4 := b4 xor ((b1 shr 2) and $30);
              dByteArray[outCount] := b4 + $3b;
              outCount := outCount + 1;
              bCount := 0;
              b4 := 0;
            end
          else
            begin
              SetLength(dByteArray, outCount+1);
              dByteArray[outCount] := (((b1 and $f0) shr 2) xor (b1 and 3))+$3b;
              outCount := outCount + 1;
              b4 := b4 shl 2;
              b4 := b4 xor ((b1 shr 2) and 3);
              bCount := bCount +1;
            end;
        end; //end for
    {  if bCount <> 2 then  //此段改为Case语句
        begin
          SetLength(dByteArray, outCount+1); //length改为outCount
          dByteArray[outCount] := b4 + $3b;
          outCount := outCount + 1;
        end;}      //改为Case
      Case bCount of
        1:
          begin
            SetLength(dByteArray, outCount+1);
            dByteArray[outCount] := b4 + $3b;
            outCount := outCount + 1;
          end;
        2:
          begin
            SetLength(dByteArray, outCount+1);
            dByteArray[outCount] := b4 + $4b;
            outCount := outCount + 1;
          end;
      end;
      Result:='';
      for i := 0 to outCount-1 do
        begin
          Result := Result + Chr(dByteArray[i]);
        end;
    end;procedure mCode(var i,j:Integer; sByteArray:array of Byte;var dByteArray:array of Byte);
    var
      b1,b2,b3,temp: Byte;
    begin
      temp:=sByteArray[i]-$3b;
      b1:=(((temp and $3c) shl 2) + (temp and $03));
      temp:=sByteArray[i+1]-$3b;
      b2:=(((temp and $3c) shl 2) + (temp and $03));
      temp:=sByteArray[i+2]-$3b;
      b3:=temp;
      temp:=sByteArray[i+3]-$3b;
      b1:=b1 xor (temp and $0c) xor $eb;
      b2:=b2 xor ((temp and $03) shl 2) xor $eb;
      b3:=b3 xor ((temp and $30) shl 2) xor $eb;
      dByteArray[j]:=b1;
      dByteArray[j+1]:=b2;
      dByteArray[j+2]:=b3;
      inc(i,4);
      inc(j,3);
    end;function Decode(Source: string): string;     //解密
    var
      i,j,mCount: integer;
      b1,b2,temp: Byte;
      sByteArray,dByteArray : array of Byte;
    begin
      mCount:=Length(Source);
      SetLength(sByteArray, mCount);
      Move(Source[1], sByteArray[0], mCount);
      SetLength(dByteArray, 0);
      if (mCount mod 4) =0 then
        begin
          i:=0;
          j:=0;
          while i <= (mCount-4) do
            begin
              SetLength(dByteArray,j+3);
              mCode(i,j,sByteArray,dByteArray);
            end;
        end
      else
        begin
          i:=0;
          j:=0;
          if (sByteArray[Length(Source)-1]>=$4b) then
            begin
              while i< mCount-3 do
                begin
                  SetLength(dByteArray,j+3);
                  mCode(i,j,sByteArray,dByteArray);
                end;
              temp:=sByteArray[i]-$3b;
              b1:=(((temp and $3c) shl 2) + (temp and $03));
              temp:=sByteArray[i+1]-$3b;
              b2:=(((temp and $3c) shl 2) + (temp and $03));
              temp:=sByteArray[i+2]-$3b;
              b1:=b1 xor (temp and $0c) xor $eb;
              b2:=b2 xor ((temp and $03) shl 2) xor $eb;
              SetLength(dByteArray,j+2);
              dByteArray[j]:=b1;
              dByteArray[j+1]:=b2;
            end
          else
            begin
              while i< mCount-2 do
                begin
                  SetLength(dByteArray,j+3);
                  mCode(i,j,sByteArray,dByteArray);
                end;
              temp:=sByteArray[i]-$3b;
              b1:=(((temp and $3c) shl 2) + (temp and $03)) xor $eb;
              temp:=sByteArray[i+1]-$3b;
              b1:=b1 xor (temp shl 2);
              SetLength(dByteArray,j+1);
              dByteArray[j]:=b1;
            end;
        end;
      for i := 0 to Length(dByteArray)-1 do
        begin
          Result := Result + Chr(dByteArray[i]);
        end;
    end;
      

  10.   

    错了错了。我上司给我代码错了。
    就是你认为那个‘<>2’的地方,原来是'<>0'。原来是自己不小心搞错了。
    呵呵。可以帮我再改改你的解密算法么?
      

  11.   

    to: SuanAddMiao(算苗) 
    这次应该是唯一的了。if bCount <> 0 then  //此段由bcount <> 2改为0
        begin
          SetLength(dByteArray, outCount+1); //length改为outCount
          dByteArray[outCount] := b4 + $3b;
          outCount := outCount + 1;
        end
      

  12.   

    确实是,解码为:
    function Decode(Source: string): string;     //解密
    var
      i,j,mCount: integer;
      b1,b2,temp: Byte;
      sByteArray,dByteArray : array of Byte;
    begin
      mCount:=Length(Source);
      SetLength(sByteArray, mCount);
      Move(Source[1], sByteArray[0], mCount);
      SetLength(dByteArray, 0);
      if (mCount mod 4) =0 then
        begin
          i:=0;
          j:=0;
          while i <= (mCount-4) do
            begin
              SetLength(dByteArray,j+3);
              mCode(i,j,sByteArray,dByteArray);
            end;
        end
      else if ((mCount+1) mod 4) =0 then
        begin
          i:=0;
          j:=0;
          while i< mCount-3 do
            begin
              SetLength(dByteArray,j+3);
              mCode(i,j,sByteArray,dByteArray);
            end;
          temp:=sByteArray[i]-$3b;
          b1:=(((temp and $3c) shl 2) + (temp and $03));
          temp:=sByteArray[i+1]-$3b;
          b2:=(((temp and $3c) shl 2) + (temp and $03));
          temp:=sByteArray[i+2]-$3b;
          b1:=b1 xor (temp and $0c) xor $eb;
          b2:=b2 xor ((temp and $03) shl 2) xor $eb;
          SetLength(dByteArray,j+2);
          dByteArray[j]:=b1;
          dByteArray[j+1]:=b2;
        end
      else
        begin
          i:=0;
          j:=0;
          while i< mCount-2 do
            begin
              SetLength(dByteArray,j+3);
              mCode(i,j,sByteArray,dByteArray);
            end;
          temp:=sByteArray[i]-$3b;
          b1:=(((temp and $3c) shl 2) + (temp and $03));
          temp:=sByteArray[i+1]-$3b;
          b1:=b1 xor (temp shl 2)  xor $eb;
          SetLength(dByteArray,j+1);
          dByteArray[j]:=b1;
        end;
      for i := 0 to Length(dByteArray)-1 do
        begin
          Result := Result + Chr(dByteArray[i]);
        end;
    end;