求加密解密Function,要求用'0'..'9'或'a'..'z'或'A'..'Z'加密,结果字符在其范围内('0'..'9'或'a'..'z'或'A'..'Z'),被加密的字符串有20至30个字符.

解决方案 »

  1.   

    如:被加密字符串"123456789aaaaaFFFFFFsdfsdfFDF",加密后变为"sdf25123ervDSeGFs52sdf".
      

  2.   

    a->b
    b->c
    z->a
    不就可以了吗?
    呵呵,我这个是简单了点。
      

  3.   

    //范围是可显示字符
    function Encrypt(S:String;Key:Word):String;
    var             { 加密子程序 }
      I,j:Integer;                                    // 用于循环
    begin
      Result:=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;
      S:=Result;                                      // 保存结果
      Result:='';                                     // 清除结果
      for I:=1 to Length(S) do                        // 对加密结果进行转换
      begin
        j:=Integer(s[i]);                             // 提取字符
        Result:=Result+Char(65+(j div 26))+Char(65+(j mod 26));
      end;
    end;
    function Decrypt(S:String;Key:Word):String;
    var             { 解密子函数 }
      I,j:Integer;
    begin
      Result:='';                                     // 清除结果
      for I:=1 to (length(S) div 2) do
      begin
        j:=(Integer(S[2*i-1])-65)*26;
        j:=j+(Integer(S[2*i])-65);
        Result:=Result+Char(j);
      end;
      S:=Result;
      for I:=1 to Length(S) do
      begin
        Result[I]:=Char(byte(S[I]) xor (Key shr 8));
        Key:=(byte(S[I])+Key)*C1+C2;
      end;
    end;
      

  4.   

    建议楼主做一个简单的算法
    字符1 + x = 字符2
    这个x可以和字符串的长度相关,比如长度 mod 长度/10
    具体算法你自己设计了。
      

  5.   

    torry上面成把抓其实用一个IntToHex(Ord(Char), 2)可以解决很多问题的。
      

  6.   

    library msgcode;{ Important note about DLL memory management: ShareMem must be the
      first unit in your library's USES clause AND your project's (select
      Project-View Source) USES clause if your DLL exports any procedures or
      functions that pass strings as parameters or function results. This
      applies to all strings passed to and from your DLL--even those that
      are nested in records and classes. ShareMem is the interface unit to
      the BORLNDMM.DLL shared memory manager, which must be deployed along
      with your DLL. To avoid using BORLNDMM.DLL, pass string information
      using PChar or ShortString parameters. }uses
      SysUtils,
      Classes;var
      code1: array[48..122] of char;
      code2: array[48..122] of char;{$R *.res}procedure varinit1();
    begin
      code1[48] := chr(81);
      code1[49] := chr(112);
      code1[48] := chr(104);
      code1[49] := chr(117);
      code1[50] := chr(54);
      code1[51] := chr(73);
      code1[52] := chr(86);
      code1[53] := chr(105);
      code1[54] := chr(118);
      code1[55] := chr(55);
      code1[56] := chr(74);
      code1[57] := chr(87);
      code1[65] := chr(106);
      code1[66] := chr(119);
      code1[67] := chr(56);
      code1[68] := chr(75);
      code1[69] := chr(88);
      code1[70] := chr(107);
      code1[71] := chr(120);
      code1[72] := chr(76);
      code1[73] := chr(89);
      code1[74] := chr(108);
      code1[75] := chr(121);
      code1[76] := chr(57);
      code1[77] := chr(77);
      code1[78] := chr(90);
      code1[79] := chr(109);
      code1[80] := chr(65);
      code1[81] := chr(78);
      code1[82] := chr(97);
      code1[83] := chr(110);
      code1[84] := chr(122);
      code1[85] := chr(66);
      code1[86] := chr(79);
      code1[87] := chr(98);
      code1[88] := chr(67);
      code1[89] := chr(80);
      code1[90] := chr(99);
      code1[97] := chr(111);
      code1[98] := chr(48);
      code1[99] := chr(68);
      code1[100] := chr(81);
      code1[101] := chr(100);
      code1[102] := chr(112);
      code1[103] := chr(49);
      code1[104] := chr(70);
      code1[105] := chr(83);
      code1[106] := chr(102);
      code1[107] := chr(114);
      code1[108] := chr(51);
      code1[109] := chr(71);
      code1[110] := chr(84);
      code1[111] := chr(103);
      code1[112] := chr(115);
      code1[113] := chr(52);
      code1[114] := chr(72);
      code1[115] := chr(85);
      code1[116] := chr(116);
      code1[117] := chr(53);
      code1[118] := chr(113);
      code1[119] := chr(69);
      code1[120] := chr(82);
      code1[121] := chr(101);
      code1[122] := chr(50);
    end;procedure Varinit2();
    begin
      code2[81] := chr(48);
      code2[112] := chr(49);
      code2[104] := chr(48);
      code2[117] := chr(49);
      code2[54] := chr(50);
      code2[73] := chr(51);
      code2[86] := chr(52);
      code2[105] := chr(53);
      code2[118] := chr(54);
      code2[55] := chr(55);
      code2[74] := chr(56);
      code2[87] := chr(57);
      code2[106] := chr(65);
      code2[119] := chr(66);
      code2[56] := chr(67);
      code2[75] := chr(68);
      code2[88] := chr(69);
      code2[107] := chr(70);
      code2[120] := chr(71);
      code2[76] := chr(72);
      code2[89] := chr(73);
      code2[108] := chr(74);
      code2[121] := chr(75);
      code2[57] := chr(76);
      code2[77] := chr(77);
      code2[90] := chr(78);
      code2[109] := chr(79);
      code2[65] := chr(80);
      code2[78] := chr(81);
      code2[97] := chr(82);
      code2[110] := chr(83);
      code2[122] := chr(84);
      code2[66] := chr(85);
      code2[79] := chr(86);
      code2[98] := chr(87);
      code2[67] := chr(88);
      code2[80] := chr(89);
      code2[99] := chr(90);
      code2[111] := chr(97);
      code2[48] := chr(98);
      code2[68] := chr(99);
      code2[81] := chr(100);
      code2[100] := chr(101);
      code2[112] := chr(102);
      code2[49] := chr(103);
      code2[70] := chr(104);
      code2[83] := chr(105);
      code2[102] := chr(106);
      code2[114] := chr(107);
      code2[51] := chr(108);
      code2[71] := chr(109);
      code2[84] := chr(110);
      code2[103] := chr(111);
      code2[115] := chr(112);
      code2[52] := chr(113);
      code2[72] := chr(114);
      code2[85] := chr(115);
      code2[116] := chr(116);
      code2[53] := chr(117);
      code2[113] := chr(118);
      code2[69] := chr(119);
      code2[82] := chr(120);
      code2[101] := chr(121);
      code2[50] := chr(122);
    end;function code(txt: string): pchar; stdcall;
    var
      i, l: integer;
      _txt: string;
    begin
      Varinit1();
      _txt := '';
      i := 1;
      while i <= Length(txt) do
        begin
          l := Integer(txt[i]);
          if l >= 128 then //若為雙字節則不轉換
            begin
              _txt := _txt + txt[i] + txt[i + 1];
              i := i + 2;
            end
          else
            begin
              if (l >= 48) and (l <= 57) or (l >= 65) and (l <= 90) or (l >= 97) and (l <= 122) then //若為數字、大小寫字母則轉換
                _txt := _txt + code1[l]
              else
                _txt := _txt + txt[i];
              i := i + 1;
            end;
        end;
      Result := Pchar(_txt + #0);
    end;function uncode(txt: string): pchar; stdcall;
    var
      i, l: integer;
      _txt: string;
    begin
      Varinit2();
      _txt := '';
      i := 1;
      while i <= Length(txt) do
        begin
          l := Integer(txt[i]);
          if l >= 128 then
            begin
              _txt := _txt + txt[i] + txt[i + 1];
              i := i + 2;
            end
          else
            begin
              if (l >= 48) and (l <= 57) or (l >= 65) and (l <= 90) or (l >= 97) and (l <= 122) then
                _txt := _txt + code2[l]
              else
                _txt := _txt + txt[i];
              i := i + 1;
            end;
        end;
      Result := pchar(_txt + #0);
    end;exports
      code index 1,
      uncode index 2;beginend.
      

  7.   

    我 们 需 要 两 个 函 数: ---- 1、GetPassword - 读 取 用 户 的 输 入, 并 判 断 口 令 的 正 确 性 和 用 户 的 级 别。 function GetPassword : Boolean;
     {根据返回值来判断是否输入了正确口令}
     var
        PasswordForm: TPasswordForm;{口令登录屏幕}
     begin
        Result := False;
        PasswordForm := TPasswordForm.Create(Self);
        if Passwordform.TablePara.fields[0].asstring = '' then
      {如果没有任何口令则不启动口令登录屏幕}
         begin
            Result := True;
            PasswordForm.Free;
        end
        else
          try
          with PasswordForm do
       begin
                if ShowModal = mrOK then
                    if EditInputPass.Text <  >
        TablePara.Fields[3].AsString  then
                          Application.MessageBox('密码错误!',
             '错误',
            mb_OK + mb_iconstop)
                          else
                       begin
                         Result := True;
                         Table1.Close;
                            end;
        finally
          PasswordForm.Free;
        end;
     end;
     2、SetPassword -设置用户级别和登录口令。
         function SetPassword : Boolean;
        var
         Chgpassform : TChgpassform;
        begin
           Result := False;
           Chgpassform := TChgpassform.Create(Self);
      if Chgpassform.ShowModal = mrOK then
      begin
        With TablePara do
          beign
       {写入库中}
       Edit;
       Fields[2].AsString := EditPasswordID.Text;
       Post;
       Close;
          end;
          Result := True;
          Chgpassform.free;
      end;
        end;
    ---- ( 注: 限 于 篇 幅, 以 上 均 为 函 数 程 序 示 意, 若 直 接 运 行 可 能 有 误。)
      

  8.   

    加密后的字符不在('0'..'9'或'a'..'z'或'A'..'Z')范围内也不要紧,用BASE64编码不就行了