function   creatnum(no:integer;num:integer):integer; 
var 
      str1,str2,str3,str   :   string; 
      i,j,titnum   :   integer; 
//.......................................................................... 
      function   encrypt3(num:string):string; 
              var 
                  res:string; 
                  bita:string; 
                  abit:integer; 
                  i:integer; 
              begin 
                  bita:=num; 
                  res:=''; 
                  for   i:=1   to   16   do 
                        begin 
                        abit:=strtoint(bita[i]); 
                        case   abit   of 
                            0:   res:=res+'2'; 
                            1:   res:=res+'7'; 
                            2:   res:=res+'9'; 
                            3:   res:=res+'4'; 
                            4:   res:=res+'1'; 
                            5:   res:=res+'6'; 
                            6:   res:=res+'3'; 
                            7:   res:=res+'8'; 
                            8:   res:=res+'0'; 
                            9:   res:=res+'5'; 
                        end; 
                        end; 
                  encrypt3:=res; 
          end; 
          function   encrypt2(num:string):string; 
              var 
                    res1,res2:string; 
                    asint,bsint:array[1..16]of   integer; 
                    i:integer; 
              begin 
                  res1:=''; 
                  res2:=''; 
                  for   i:=1   to   16   do   asint[i]:=strtoint(num[i]); 
                  bsint[1]:=asint[1]; 
              res1:=res1+inttostr(bsint[1]); 
              for   i:=2   to   16   do 
                    begin 
                    if   asint[i]> =bsint[i-1]   then   bsint[i]:=asint[i]-bsint[i-1]     else       bsint[i]:=10+asint[i]-bsint[i-1]; 
                    res1:=res1+inttostr(bsint[i]); 
                    end;               for   i:=1   to   16   do   asint[i]:=strtoint(res1[17-i]); 
                bsint[1]:=asint[1]; 
                res2:=res2+inttostr(bsint[1]); 
              for   i:=2   to   16   do 
                    begin 
                    if   asint[i]> =bsint[i-1]   then   bsint[i]:=asint[i]-bsint[i-1]     else       bsint[i]:=10+asint[i]-bsint[i-1]; 
                    res2:=inttostr(bsint[i])+res2; 
                    end; 
              encrypt2:=res2; 
          end;           function   encrypt1(num:string):string; 
              var 
                res:string; 
                bita:string; 
                abit:integer; 
                i:integer; 
            begin 
                bita:=num; 
                res:=''; 
                for   i:=1   to   16   do 
                    begin 
                    abit:=strtoint(bita[i]); 
                    case   abit   of 
                        0:   res:=res+'7'; 
                        1:   res:=res+'4'; 
                        2:   res:=res+'8'; 
                        3:   res:=res+'1'; 
                        4:   res:=res+'6'; 
                        5:   res:=res+'0'; 
                        6:   res:=res+'3'; 
                        7:   res:=res+'9'; 
                        8:   res:=res+'2'; 
                        9:   res:=res+'5'; 
                end; 
                end; 
              encrypt1:=res; 
          end;         function   encrypt4(num:string):string; 
            var 
              i,j,sum,due:integer; 
              aint,bint,cint:array[1..16]of   integer; 
            res:string; 
          begin 
            sum:=0; 
            for   i:=1   to   16   do 
              begin 
              sum:=sum+strtoint(num[i]); 
              aint[i]:=strtoint(num[i]); 
              end; 
        for   i:=1   to   16   do 
              begin 
              due:=sum   mod(17-i); 
              bint[i]:=due+1; 
              cint[i]:=bint[i]; 
              sum:=sum-aint[i]; 
              end; 
        for   i:=2   to   16   do 
              begin 
              for   j:=1   to   i-1   do 
                    begin 
                    if   cint[i]> =bint[i-j]   then   cint[i]:=cint[i]+1; 
                    end; 
              end; 
        for   i:=1   to   16   do 
              begin 
              j:=cint[i]; 
              bint[j]:=aint[i]; 
              end; 
          res:=''; 
        for   i:=1   to   16   do   res:=res+inttostr(bint[i]); 
        encrypt4:=res; 
        end; 
begin 
  try 
      Rewrite(atextfile); 
      str1:=inttostr(no); 
      while   length(str1) <4   do   str1:='0'+str1; 
      str2:=''; 
      str3:='1199';       titnum:=num;       for   i:=1   to   titnum   do 
            begin 
            str2:=inttostr(i); 
            while   length(str2) <8   do   str2:='0'+str2; 
            str:=str1+str2+str3; 
            str:=encrypt4(encrypt3(encrypt2(encrypt1(str)))); 
            if   form1.ComboBox1.ItemIndex=0   then 
            begin 
            end 
            else   if   form1.ComboBox1.ItemIndex=1   then 
            begin 
                str:=str+str[3]; 
            end 
            else   if   form1.ComboBox1.ItemIndex=2   then 
            begin 
                str:=str+str[3]+   str[9]; 
            end 
            else   if   form1.ComboBox1.ItemIndex=3   then 
            begin 
                str:=str+str[3]+   str[9]+   str[12]; 
            end 
            else   if   form1.ComboBox1.ItemIndex=4   then 
            begin 
                str:=str+str[3]+   str[9]+   str[12]+   str[2]; 
            end 
            else   if   form1.ComboBox1.ItemIndex=5   then 
            begin 
                str:=str+str[3]+   str[9]+   str[12]+   str[2]+   str[6]; 
            end 
            else   if   form1.ComboBox1.ItemIndex=6   then 
            begin 
                str:=str+str[3]+   str[9]+   str[12]+   str[2]+   str[6]+   str[13]; 
            end 
            else   if   form1.ComboBox1.ItemIndex=7   then 
            begin 
                str:=str+str[3]+   str[9]+   str[12]+   str[2]+   str[6]+   str[13]+   str[5]; 
            end 
            else   if   form1.ComboBox1.ItemIndex=8   then 
            begin 
                str:=str+str[3]+   str[9]+   str[12]+   str[2]+   str[6]+   str[13]+   str[5]+   str[11]; 
            end 
            else   if   form1.ComboBox1.ItemIndex=-1   then 
            begin 
              
            end; 
            
            writeln(atextfile,str); 
            end; 
  finally 
  CloseFile(ATextFile); 
  end; end;

解决方案 »

  1.   

    function Dencrypt3(num:string):string;
    var
      i : integer;
    begin
      Result := '';
      for i := 1 to 16 do
        case strtoint(num[i]) of
          2: Result := Result + '0';
          7: Result := Result + '1';
          9: Result := Result + '2';
          4: Result := Result + '3';
          1: Result := Result + '4';
          6: Result := Result + '5';
          3: Result := Result + '6';
          8: Result := Result + '7';
          0: Result := Result + '8';
          5: Result := Result + '9';
        end;
    end;function Dencrypt1(num:string):string;
    var
      i : integer;
    begin
      Result := '';
      for i := 1 to 16 do
        case strtoint(num[i]) of
          7: Result := Result + '0';
          4: Result := Result + '1';
          8: Result := Result + '2';
          1: Result := Result + '3';
          6: Result := Result + '4';
          0: Result := Result + '5';
          3: Result := Result + '6';
          9: Result := Result + '7';
          2: Result := Result + '8';
          5: Result := Result + '9';
        end;
    end;
    function Dencrypt2(num:string):string;
    var
      asint, bsint: array[1..16] of integer;
      res         : string;
      i           : integer;
    begin
      for i := 1 to 16 do asint[i] := strtoint(num[17 - i]);
      bsint[1] := asint[1];
      res := inttostr(bsint[1]);
      for i := 2 to 16 do
      begin
        bsint[i] := (asint[i] + asint[i-1]) mod 10;
        res := inttostr(bsint[i]) + res;
      end;
      for i := 1 to 16 do asint[i]:=strtoint(res[i]);
      bsint[1] := asint[1];
      res := inttostr(bsint[1]);
      for i := 2 to 16 do
      begin
        bsint[i] := (asint[i] + asint[i-1]) mod 10;
        res := res + inttostr(bsint[i])
      end;
      Result := res;
    end;function   dencrypt4(num:   string):   string; 
    var 
        i,   j,   sum,   due,   k:   integer; 
        aint,   bint,   cint:   array[1..16]   of   integer; 
        res:   string; 
    begin 
        sum   :=   0; 
        for   i   :=   1   to   16   do 
        begin 
            aint[i]   :=   StrToInt(num[i]); 
            Inc(sum,   aint[i]); 
        end;     for   i   :=   1   to   16   do 
        begin 
            due   :=   sum   mod   (17   -   i); 
            bint[i]   :=   due   +   1; 
            cint[i]   :=   bint[i];         for   j   :=   1   to   i   -   1   do 
            begin 
                if   cint[i]   > =   bint[i   -   j]   then 
                    Inc(cint[i]); 
            end;         j   :=   cint[i]; 
            Dec(sum,   aint[j]); 
        end;     for   i   :=   1   to   16   do 
        begin 
            j   :=   cint[i]; 
            bint[i]   :=   aint[j]; 
        end;     res   :=   ''; 
        for   i   :=   1   to   16   do 
            res   :=   res   +   inttostr(bint[i]);     dencrypt4   :=   res; 
    end; 
      

  2.   

    encrypt4(encrypt3(encrypt2(encrypt1("0024000000111199"))))Dencrypt1(Dencrypt2(Dencrypt3(Dencrypt4("4709608507835926"))))每个解密函数都试试好象都正确,但是这样用就不对了,加密后是4709608507835926,但经过解密后就不是0024000000111199,怎么回事呢?
      

  3.   

    没发现楼主所描述的问题哇。
    function encrypt1(num:string):string;
    var
      res : string;
      bita: string;
      abit: integer;
      i   : integer;
    begin
      bita := num;
      res := '';
      for i := 1 to 16 do
      begin
        abit := strtoint(bita[i]);
        case abit of
            0: res := res + '7';
            1: res := res + '4';
            2: res := res + '8';
            3: res := res + '1';
            4: res := res + '6';
            5: res := res + '0';
            6: res := res + '3';
            7: res := res + '9';
            8: res := res + '2';
            9: res := res + '5';
        end;
      end;
      Result := res;
    end;function encrypt3(num:string):string;
    var
      res : string;
      bita: string;
      abit: integer;
      i   : integer;
    begin
      bita := num;
      res := '';
      for i := 1 to 16 do
      begin
        abit := strtoint(bita[i]);
        case abit of
            0: res := res + '2';
            1: res := res + '7';
            2: res := res + '9';
            3: res := res + '4';
            4: res := res + '1';
            5: res := res + '6';
            6: res := res + '3';
            7: res := res + '8';
            8: res := res + '0';
            9: res := res + '5';
        end;
      end;
      Result := res;
    end;function encrypt2(num:string):string;
    var
      res1, res2 : string;
      asint,bsint: array [1..16] of integer;
      i          : integer;
    begin
      for i := 1 to 16 do asint[i] := strtoint(num[i]);
      bsint[1] := asint[1];
      res1 := inttostr(bsint[1]);
      for i := 2 to 16 do
      begin
        if asint[i] >= bsint[i-1] then
          bsint[i] := asint[i] - bsint[i-1]
        else
          bsint[i] := 10 + asint[i] - bsint[i-1];
        res1 := res1 + inttostr(bsint[i]);
      end;  for i := 1 to 16 do asint[i]:=strtoint(res1[17-i]);
      bsint[1] := asint[1];
      res2 := inttostr(bsint[1]);
      for i := 2 to 16 do
      begin
        if asint[i] >= bsint[i-1] then
          bsint[i] := asint[i] - bsint[i-1]
        else
          bsint[i] := 10 + asint[i] - bsint[i-1];
        res2 := inttostr(bsint[i]) + res2;
      end;
      encrypt2:=res2;
    end;function encrypt4(num:string):string;
    var
      i,j,sum,due:integer;
      aint,bint,cint:array[1..16]of   integer;
      res:string;
    begin
      sum := 0;
      for i := 1 to 16 do
      begin
        sum := sum + strtoint(num[i]);
        aint[i] := strtoint(num[i]);
      end;
      for i := 1 to 16 do
      begin
        due := sum mod (17 - i);
        bint[i] := due + 1;
        cint[i] := bint[i];
        sum := sum - aint[i];
      end;
      for i := 2 to 16 do
      begin
        for j := 1 to i - 1 do
        begin
          if cint[i] >= bint[i-j] then cint[i] := cint[i] + 1;
        end;
      end;
      for i := 1 to 16 do
      begin
        j := cint[i];
        bint[j] := aint[i];
      end;
      res := '';
      for i := 1 to 16 do res := res + inttostr(bint[i]);
      Result := res;
    end;function encrypt2A(num:string):string;    //对encrypt2进行了精简
    var
      asint, bsint: array[1..16] of integer;
      res         : string;
      i           : integer;
    begin
      for i := 1 to 16 do asint[i] := strtoint(num[i]);
      bsint[1] := asint[1];
      res := inttostr(bsint[1]);
      for i := 2 to 16 do
      begin
        bsint[i] := (10 + asint[i] - bsint[i-1]) mod 10;
        res := res + inttostr(bsint[i]);
      end;
      for i := 1 to 16 do asint[i] := strtoint(res[17-i]);
      bsint[1] := asint[1];
      res := inttostr(bsint[1]);
      for i := 2 to 16 do
      begin
        bsint[i] := (10 + asint[i] - bsint[i-1]) mod 10;
        res :=  inttostr(bsint[i]) + res;
      end;
      Result := res;
    end;function encrypt4A(num:string):string;      //对encrypt4进行了精简
    var
      i,j,sum,due:integer;
      aint,bint,cint:array [1..16] of integer;
      res:string;
    begin
      sum := 0;
      for i := 1 to 16 do
      begin
        sum := sum + strtoint(num[i]);
        aint[i] := strtoint(num[i]);
      end;
      for i := 1 to 16 do
      begin
        due := sum mod (17 - i);
        bint[i] := due + 1;
        cint[i] := bint[i];
        sum := sum - aint[i];
      end;
      for i := 2 to 16 do
      begin
        for j := i - 1 downto 1 do
        begin
          if cint[i] >= bint[j] then cint[i] := cint[i] + 1;
        end;
      end;
      for i := 1 to 16 do
      begin
        j := cint[i];
        bint[j] := aint[i];
      end;
      res := '';
      for i := 1 to 16 do res := res + inttostr(bint[i]);
      Result := res;
    end;function Dencrypt2(num:string):string;
    var
      asint, bsint: array[1..16] of integer;
      res         : string;
      i           : integer;
    begin
      for i := 1 to 16 do asint[i] := strtoint(num[17 - i]);
      bsint[1] := asint[1];
      res := inttostr(bsint[1]);
      for i := 2 to 16 do
      begin
        bsint[i] := (asint[i] + asint[i-1]) mod 10;
        res := inttostr(bsint[i]) + res;
      end;
      for i := 1 to 16 do asint[i]:=strtoint(res[i]);
      bsint[1] := asint[1];
      res := inttostr(bsint[1]);
      for i := 2 to 16 do
      begin
        bsint[i] := (asint[i] + asint[i-1]) mod 10;
        res := res + inttostr(bsint[i])
      end;
      Result := res;
    end;function Dencrypt3(num:string):string;
    var
      i : integer;
    begin
      Result := '';
      for i := 1 to 16 do
        case strtoint(num[i]) of
          2: Result := Result + '0';
          7: Result := Result + '1';
          9: Result := Result + '2';
          4: Result := Result + '3';
          1: Result := Result + '4';
          6: Result := Result + '5';
          3: Result := Result + '6';
          8: Result := Result + '7';
          0: Result := Result + '8';
          5: Result := Result + '9';
        end;
    end;function Dencrypt1(num:string):string;
    var
      i: integer;
    begin
      Result := '';
      for i := 1 to 16 do
        case strtoint(num[i]) of
          7: Result := Result + '0';
          4: Result := Result + '1';
          8: Result := Result + '2';
          1: Result := Result + '3';
          6: Result := Result + '4';
          0: Result := Result + '5';
          3: Result := Result + '6';
          9: Result := Result + '7';
          2: Result := Result + '8';
          5: Result := Result + '9';
        end;
    end;function Dencrypt4(num: string): string;
    var
      i, j, sum, due, k: integer;
      aint, bint, cint: array[1..16] of integer;
      res: string;
    begin
      sum := 0;
      for i := 1 to 16 do
      begin
        aint[i] := StrToInt(num[i]);
        Inc(sum, aint[i]);
      end;  for i := 1 to 16 do
      begin
        due := sum mod (17 - i);
        bint[i] := due + 1;
        cint[i] := bint[i];
        for j := 1 to i - 1 do
        begin
          if cint[i] >= bint[i - j] then Inc(cint[i]);
        end;
        j := cint[i];
        Dec(sum, aint[j]);
      end;  for i := 1 to 16 do
      begin
        j := cint[i];
        bint[i] := aint[j];
      end;
      res := '';
      for i := 1 to 16 do res := res + inttostr(bint[i]);
      Result := res;
    end;procedure TForm1.Button1Click(Sender: TObject);
    const
      S = '0024000000111199';
    var
      a,b : String;
    begin
      a := encrypt4(encrypt3(encrypt2(encrypt1(S))));
      b := Dencrypt1(Dencrypt2(Dencrypt3(Dencrypt4(a))));
      ShowMessage (S + ' -> ' + a + ' ->' + b);
    end;