怎样对一组字符串加密让一个字符串的加密结果是有英文或数字组成
而不是特殊字符?

解决方案 »

  1.   

    你试试,加密后的字符串是英文和数字的unit UMain;
    interface
    uses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls,math;
    Const
      MIN_ASC = 32;      //Space.
      MAX_ASC = 126;     //~.
      NUM_ASC = MAX_ASC - MIN_ASC + 1;
    type
      TForm1 = class(TForm)
        Button1: TButton;
        Label1: TLabel;
        Edit1: TEdit;
        Label2: TLabel;
        Edit2: TEdit;
        Edit3: TEdit;
        Label3: TLabel;
        Button2: TButton;
        Label4: TLabel;
        Edit4: TEdit;
        Button3: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
      private
        function NewEncrypt(const Passwd, key: String): String;
        function NewDecrypt(const Cryptwd, key: String): String;
        function StringToInteger(const str: String): Longint;
        function IntToDigit(mScale: Byte; mInt: Integer): string;
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation
    const 
      cScaleChar = '0123456789abcdefghijklmnopqrstuvwxyz';
    {$R *.DFM}function EncryPublicKey(const PublicKey : String): LongInt;     //对密钥的加密  function PowerOfTwo(const Power : integer):LongInt;           //计算2的N次方
      var
        I : Integer;
        TmpInt : LongInt;
      begin
        TmpInt := 1;
        if Power = 0 then                                           //幂为0时,返回1
          PowerOfTwo := 1
        else begin                                                  //幂非0时,利用循环计算2的N次方
          For I := 1 to Power do
          begin
            TmpInt := TmpInt * 2;
          end;
          PowerOfTwo := TmpInt;
        end;
      end;var
      TmpValue  : LongInt;                                          //加密过程的暂存变量
      TmpKey  : LongInt;                                            //密钥中各位对应的ASCII码
      ShiftKeyOne : LongInt;                                        //转换因子
      ShiftKeyTwo : LongInt;                                        //转换因子
      I : Integer;                                                  //循环变量
      KeyLength : Integer;                                          //密钥长度
      tmpstr : String;                                              //密钥中各位的暂存变量
    begin
      KeyLength := Length(PublicKey);                               //计算密钥长度
      ShiftKeyOne := 0;                                             //转换因子初值
      ShiftKeyTwo := 0;                                             //转换因子初值
      TmpValue := 0;                                                //暂存变量初值  for I := 1 to KeyLength do
      begin
        tmpstr := Copy(PublicKey,i,1);
        TmpKey := Ord(tmpstr[1]);                                   //按位取密钥并转化为ASCII值
        TmpValue := TmpValue Xor (TmpKey * PowerOfTwo(ShiftKeyOne));
        TmpValue := TmpValue Xor (TmpKey * PowerOfTwo(ShiftKeyTwo));//加密过程    ShiftKeyOne := (ShiftKeyOne + 7) Mod 19;
        ShiftKeyTwo := (ShiftKeyTwo + 13) Mod 23;                   //转换因子的变化
      end;
      EncryPublicKey := TmpValue;                                   //加密后的密钥
    end;function CreatePublicKey:String;
    var
      I : Integer;
      Seed : Integer;
      TmpKey : String;
    begin
      Randomize;
      Seed := Random(24);                                           //产生一个随机数,确定密钥的位数
      if Seed < 8 then Seed := Seed + 8;                            //确保密钥至少要有8位  for I := 1  to Seed do
      begin
        if random(126) <> 0 then
          TmpKey := TmpKey + Chr(Random(126))                       //产生密钥
        else
          TmpKey := TmpKey + Chr(Random(126))
      end;
      CreatePublicKey := TmpKey;                                    //返回密钥
    end;
    function RandomNumber(const RandomSeed :String):Double;         //产生随机数的过程
    var
      I : Integer;
      EncryKey : Double;
    begin
      EncryKey := EncryPublicKey(RandomSeed);                       //获得加密后密钥
      I := 1;  while EncryKey > 1.5 do                                       //利用开方的办法将加密后的密钥
      begin
        if (EncryKey - I) > 0 then
          EncryKey := EncryKey - 1;                                 //转变为小于1的随机数    EncryKey := Sqrt(EncryKey);
        I := I + 5;
      end;  RandomNumber := EncryKey - 1;                                 //返回随机数
    end;function EncryptPassword(const PublicKey,SrcString: String):String;   //加密函数
    var
      I : Integer;                                                        //循环变量
      TmpValue : Integer;                                                 //加密过程中的中间变量
      SrcStrlen : Integer;                                                //密码长度
      Offset : LongInt;                                                   //加密过程中的参量
      RndNum : Double;                                                    //随机数
      TmpStr : String;                                                    //密码中各位的暂存变量
      DestString : String;                                                //加密后的密码暂存量
    begin
      RndNum := RandomNumber(PublicKey);                                  //根据密钥计算随机数  SrcStrlen := Length(SrcString);                                     //计算密码长度  For i := 1 To SrcStrlen do                                          //对密码逐位进行加密
      begin                                                               //取得密码各位
        Tmpstr := Copy(SrcString,i,1);                                    //密码各位转换为ASCII码
        TmpValue := Ord(Tmpstr[1]);
        If (TmpValue >= MIN_ASC) And (TmpValue <= MAX_ASC) Then           //加密过程
        begin
          TmpValue := TmpValue - MIN_ASC;
          Offset := round((NUM_ASC + 1) * RndNum);
          TmpValue := ((TmpValue + Offset) Mod NUM_ASC);
          TmpValue := TmpValue + MIN_ASC ;
          DestString := DestString + Chr(TmpValue);                       //形成加密后的字符串
        End
      end;  EncryptPassword := DestString;                                      //返回加密后的字符串
    end;
      

  2.   

    function DecryptPassword(const PublicKey,SrcString:String):String;
    var
      I : Integer;                                                        //循环变量
      TmpValue : Integer;                                                 //解密过程中的中间变量
      SrcStrlen : Integer;                                                //加密密码长度
      Offset : LongInt;                                                   //解密过程中的参量
      RndNum : Double;                                                    //随机数
      TmpStr : String;                                                    //加密密码中各位的暂存变量
      DestString : String;                                                //解密后的密码暂存量
    begin
      RndNum := RandomNumber(PublicKey);                                  //根据密钥计算随机数
                                                                          //这里产生的随机数和加密时的随机数是相同的
      SrcStrlen := Length(SrcString);                                     //计算加密后密码的长度  For i := 1 To SrcStrlen do                                          //对加密后的密码进行逐位解密计算
      begin
        TmpStr := copy(SrcString,i,1);                                    //取得加密后密码的各位
        TmpValue := Ord(tmpstr[1]);                                       //计算加密后密码各位的ASCII码    If (TmpValue >= MIN_ASC) And (TmpValue <= MAX_ASC) Then           //解密过程
        begin
          TmpValue := TmpValue - MIN_ASC;
          Offset := Round((NUM_ASC + 1) * RndNum);
          TmpValue := ((TmpValue - offset) Mod NUM_ASC);      If TmpValue < 0 Then TmpValue := TmpValue + NUM_ASC;
          TmpValue := TmpValue + MIN_ASC ;
          DestString := DestString + Chr(TmpValue);                       //形成解密后的字符串
        End
      end;  DecryptPassword := DestString;                                      //返回解密后的字符串
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
    //  Edit3.Text := EncryptPassword(Edit2.text,Edit1.text);
      edit3.Text := NewEncrypt(Edit1.Text,Edit2.Text)
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
      Edit4.Text := NewDecrypt(Edit3.Text,Edit2.Text);
    end;procedure TForm1.Button3Click(Sender: TObject);
    begin
      Edit2.Text := CreatePublicKey;
    end;function TForm1.StringToInteger(const str : String):LongInt;
    var
      I : integer;
      tmp : string;
      Numstr : string;
    begin
      tmp := '';
      numstr := '';
      for I := 1 to length(str) do
      begin
        tmp := str[I];    if (ord(tmp[1]) >= 49) and (ord(tmp[1]) <=57 ) then
          Numstr := Numstr + tmp
        else begin
          break;
        end;
      end;  StringToInteger := StrToInt64(Numstr);
    end;function Tform1.IntToDigit(mScale: Byte; mInt: Integer): string; 
    var 
      I, J: Integer; 
    begin 
      Result := ''; 
      I := mInt; 
      while (I >= mScale) do begin
        J := I mod mScale; 
        Result := Copy(cScaleChar, Succ(J), 1) + Result; 
        I := I div mScale; 
      end; 
      Result := Copy(cScaleChar, Succ(I), 1) + Result; 
    end;function TForm1.NewEncrypt(const Passwd,key : String):String;
    var
      tmpstr,tmpkey,enc_str : String;
      tmp1,tmp2 : integer;
      I,j,salt: longint;
      modu,enc_chr : longint;
      strsalt : string;begin
      Randomize;
      salt := round(random * 1000000000) mod 1000000000;
      modu := round(power(2, 31) - 1);  for i := 1 to length(passwd) do
      begin
        J := I;
        while J > length(key) do J := J - length(key);
          tmpkey := copy(key,J,1);    tmpstr := copy(passwd,i,1);    tmp1 := floor(( salt / modu) * 255) + ord(tmpkey[1]);
        tmp2 := ord(tmpstr[1]);    enc_chr := tmp1 xor tmp2;
        enc_chr := enc_chr xor Ord(tmpkey[1]);    if enc_chr < 16 then
          enc_str := enc_str + '0' + IntToDigit(16, enc_chr)
        else enc_str := enc_str + IntToDigit(16,enc_chr);
      end;  strsalt := IntToDigit(16,salt);  while (length(strsalt) < 9) do strsalt := '0' + strsalt;
      enc_str := enc_str + strsalt;  NewEncrypt := enc_str;
    end;function TForm1.NewDecrypt(const Cryptwd,key : String):String;  function hexToInt(const hexStr : String):LongInt;
      var
        i,j,k : integer;
        tmpvalue : LongInt;
        tmpStr : String;
      begin
        tmpStr := hexStr;
        tmpvalue := 0;
        While tmpstr[1] = '0' do tmpStr := copy(tmpStr,2,length(tmpstr)-1);    k := 1;
        for i := length(tmpstr) downto 1 do
        begin
          j := pos(tmpStr[i],cScaleChar) - 1;
          tmpvalue := tmpvalue + round(power(16,k-1)) * j;
          k := k + 1;
        end;
        hexToInt := tmpvalue;
      end;var
      I,J : integer;
      modu : LongInt;
      salt : LongInt;
      enc_chr : integer;
      cryptStr : String;
      cryptRnd : String;
      enc_str : String;
      tmp1,tmp2 : LongInt;
      tmpkey : String;
    begin
      modu := round(power(2, 31) - 1);
      cryptRnd := Copy(cryptWd,length(cryptWd) - 8,9);
      cryptStr := Copy(cryptWd,1,length(cryptWd) - 9);;
      salt := hexToInt(cryptRnd);  I := 0;
      while i <= length(cryptStr) - 1 do
      begin
        j := round(i / 2);
        while (j >= length(Key)) do
          j :=  j - length(key);    tmpkey := copy(key,j + 1,1);
        tmp1 := hexToInt(Copy(cryptStr,I + 1,2));
        tmp2 := tmp1 xor ord(tmpkey[1]);    enc_chr := tmp2 xor (floor((salt / modu) * 255) + ord(tmpkey[1]));
        enc_str := enc_str + chr(enc_chr);
        I := I + 2;
      end;
      NewDecrypt := enc_str;
    end;
    end.
      

  3.   

    也可用MD5加密。当MD5加密后的密码不能解密。