最简单的就是吧你的密码每个字符与密钥进行XOR然后存入文件

解决方案 »

  1.   

    ///////////////////////////////////////////////////////////////////////////
    //文本简单的加密和解密
    //////////////////////////////////////////////////////////////////////////const
      cKey = '随便你了';function Encrypt(mStr: string; mKey: string): string;
    var
      I, J: Integer;
    begin
      J := 1;
      Result := '';
      for I := 1 to Length(mStr) do begin
        Result := Result + Char(Ord(mStr[I]) xor Ord(mKey[J]));
        if J + 1 <= Length(mKey) then
          Inc(J)
        else J := 1;
      end;
      {自己加步骤}
    end;function Decrypt(mStr: string; mKey: string): string;
    var
      I, J: Integer;
    begin
      J := 1;
      Result := '';
      {自己加步骤}
      for I := 1 to Length(mStr) do begin
        Result := Result + Char(Ord(mStr[I]) xor Ord(mKey[J]));
        if J + 1 <= Length(mKey) then
          Inc(J)
        else J := 1;
      end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      Memo2.Text := Decrypt(Encrypt(Memo1.Text, cKey), cKey);
    end;
      

  2.   

    //我再粘一下,哈哈...
    unit EncryptIt;interface
    USES
        Classes;
    const
         C1 = 52845;
         C2 = 22719;function Encrypt(const S: String; Key: Word): String;
    function Decrypt(const S: String; Key: Word): String;
    procedure EncryptFile(INFName, OutFName : String; Key : Word);
    procedure DecryptFile(INFName, OutFName : String; Key : Word);implementationfunction Encrypt(const S: String; Key: Word): String;
    var
       I: 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;
      end;function Decrypt(const S: String; Key: Word): String;
    var
       I: Integer;
    begin
      Result := S;
      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;
    procedure EncryptFile(INFName, OutFName : String; Key : Word);
    VAR
       MS, SS : TMemoryStream;
       X : Integer;
       C : Byte;
    begin
    MS := TMemoryStream.Create;
    SS := TMemoryStream.Create;
        TRY
           MS.LoadFromFile(INFName);
           MS.Position := 0;
           FOR X := 0 TO MS.Size - 1 DO
                 begin
                      MS.Read(C, 1);
                      C := (C xor (Key shr 8));
                      Key := (C + Key) * C1 + C2;
                      SS.Write(C,1);
                 end;
           SS.SaveToFile(OutFName);
        FINALLY
               SS.Free;
               MS.Free;
        end;
    end;procedure DecryptFile(INFName, OutFName : String; Key : Word);
    VAR
       MS, SS : TMemoryStream;
       X : Integer;
       C, O : Byte;
    begin
    MS := TMemoryStream.Create;
    SS := TMemoryStream.Create;
        TRY
           MS.LoadFromFile(INFName);
           MS.Position := 0;
           FOR X := 0 TO MS.Size - 1 DO
                 begin
                      MS.Read(C, 1);
                      O := C;
                      C := (C xor (Key shr 8));
                      Key := (O + Key) * C1 + C2;
                      SS.Write(C,1);
                 end;
           SS.SaveToFile(OutFName);
        FINALLY
               SS.Free;
               MS.Free;
        end;
    end;end.