有些数据存到数据库里时是经过加密后的数据,比如:密码。关于数据加密的算法有哪些以及如何解码正确取出这些数据呢?
能提供一段程序跟大伙共享吗?
谢谢了。

解决方案 »

  1.   

    转前人一个算法
    {**************************************************
    **
    **  解密过程
    **
    ****************************************************}
    function password_out(S : string) : shortstring; stdcall;  //將轉換后的密碼轉換
    type
        PWORD = ^WORD;
    var
        Buffer : variant;
        PW  : shortstring;
        P   : PWORD;
        I   : Integer;
        V   : Integer;
    begin
        PW := '                                                                                                                                                                ';
        P := PWORD(@PW[0]);
        I := 1;
        while I <= Length(S) do begin
            Buffer := Copy(S, I, 5);
            I   := I + 5;
            V   := StrToInt(Buffer) - 34567;
            P^  := V;
            Inc(P);
        end;
        Result := pw;
    end;
    {**************************************************
    **
    **  加密过程
    **
    ****************************************************}
    function password_in(S : string) :shortstring; stdcall; //將字符轉換成密碼
    type
        PWORD = ^WORD;
    var
        Len    : Integer;
        I      : Integer;
        V      : DWORD;
        P      : PChar;
        Buffer : string[255];
        a:string[100];
    //    b:string[100];
    begin
        Buffer := S;
        Len := Length(Buffer) + 1;
        if (Len mod 2) <> 0 then
            Inc(Len);
        if Len < 10 then
            Len := 10;
        I := Length(Buffer);
        if I = 0 then
            Buffer := IntToStr(GetTickCount)
        else
            while Length(Buffer) < 10 do
                Buffer := Buffer + Buffer;
       SetLength(Buffer, I);
       P := PChar(@Buffer[0]);
        for I := 1 to Len div 2 do begin
            V := 34567 + PWORD(P)^;
            P := P + 2;
            a:=a+ Format('%5.5d', [V]);
        end;
     Result := a ;
    end;
      

  2.   

    再提供一个很简单的加、解密的过程
    function Decrypt(const s:string;key:integer):string; //key=1时为加密,0为解密
    var
       I:Integer;
    begin
       Result:='';
       case key of
       1:  //加密
        begin
          for i:=1 to length(s) do
             result := result+chr(ord(s[i]) xor i xor 69);
          result := result + char(69);
        end;
       0:  //解密
        begin
          for i:=1 to length(s) - 1 do
             result := result+chr(ord(s[i]) xor i xor 69);
        end;
       end;
    end;