unit ChangePass_profile;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons;

type
  TfrmChangePass_profile = class(TForm)
    gb2: TGroupBox;
    Label4: TLabel;
    Label2: TLabel;
    txtNewPass: TEdit;
    txtRType: TEdit;
    btnCancel: TBitBtn;
    btnReset: TBitBtn;

    procedure btnCancelClick(Sender: TObject);
    procedure Clear();
    procedure ResetPass();
    procedure btnResetClick(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
    id:String;
  end;

  type
   TWordTriple = Array[0..2] of Word;
    function FileEncrypt(InFile, OutFile: String; Key: TWordTriple): boolean;
    function FileDecrypt(InFile, OutFile: String; Key: TWordTriple): boolean;
    function TextEncrypt(const s: string; Key: TWordTriple): string;
    function TextDecrypt(const s: string; Key: TWordTriple): string;
    function MemoryEncrypt(Src: Pointer; SrcSize: Cardinal; Target: Pointer; TargetSize: Cardinal; Key:TWordTriple): boolean;
    function MemoryDecrypt(Src: Pointer; SrcSize: Cardinal; Target: Pointer; TargetSize: Cardinal; Key:TWordTriple): boolean;

var
  frmChangePass_profile: TfrmChangePass_profile;

implementation

uses DM, Profile, AccountInformation;

{$R *.dfm}

function MemoryEncrypt(Src: Pointer; SrcSize: Cardinal; Target: Pointer; TargetSize: Cardinal; Key: TWordTriple): boolean;
var
  pIn, pOut: ^byte;
  i : Cardinal;
begin
    if SrcSize = TargetSize then
    begin
        pIn := Src;
        pOut := Target;
        for i := 1 to SrcSize do
        begin
          pOut^ := pIn^ xor (Key[2] shr 8);
          Key[2] := Byte(pIn^ + Key[2]) * Key[0] + Key[1];
          inc(pIn);
          inc(pOut);
        end;
              Result := True;
    end else
              Result := False;
end;

function MemoryDecrypt(Src: Pointer; SrcSize: Cardinal; Target: Pointer; TargetSize: Cardinal; Key: TWordTriple): boolean;
var
  pIn, pOut: ^byte;
  i : Cardinal;
begin
  if SrcSize = TargetSize then
  begin
    pIn := Src;
    pOut := Target;
    for i := 1 to SrcSize do
    begin
      pOut^ := pIn^ xor (Key[2] shr 8);
      Key[2] := byte(pOut^ + Key[2]) * Key[0] + Key[1];
      inc(pIn);
      inc(pOut);
    end;
    Result := True;
  end else
    Result := False;
end;

function TextCrypt(const s: string; Key: TWordTriple; Encrypt: Boolean): string;
var
  bOK: Boolean;
begin
  SetLength(Result, Length(s));
  if Encrypt then
    bOK := MemoryEncrypt(PChar(s), Length(s), PChar(Result), Length(Result), Key)
  else
    bOK := MemoryDecrypt(PChar(s), Length(s), PChar(Result), Length(Result), Key);
  if not bOK then Result := '';
end;

function FileCrypt(InFile, OutFile: String; Key: TWordTriple; Encrypt: Boolean): boolean;
var
  MIn, MOut: TMemoryStream;
begin
  MIn := TMemoryStream.Create;
  MOut := TMemoryStream.Create;
  Try
    MIn.LoadFromFile(InFile);
    MOut.SetSize(MIn.Size);
    if Encrypt then
      Result := MemoryEncrypt(MIn.Memory, MIn.Size, MOut.Memory, MOut.Size, Key)
    else
      Result := MemoryDecrypt(MIn.Memory, MIn.Size, MOut.Memory, MOut.Size, Key);
    MOut.SaveToFile(OutFile);
  finally
    MOut.Free;
    MIn.Free;
  end;
end;
function TextEncrypt(const s: string; Key: TWordTriple): string;
begin
  Result := TextCrypt(s, Key, True);
end;

function TextDecrypt(const s: string; Key: TWordTriple): string;
begin
  Result := TextCrypt(s, Key, False);
end;

function FileEncrypt(InFile, OutFile: String; Key: TWordTriple): boolean;
begin
  Result := FileCrypt(InFile, OutFile, Key, True);
end;

function FileDecrypt(InFile, OutFile: String; Key: TWordTriple): boolean;
begin
  Result := FileCrypt(InFile, OutFile, Key, False);
end;

procedure TfrmChangePass_profile.btnCancelClick(Sender: TObject);
begin
      Clear();
      Close;
end;

procedure TfrmChangePass_profile.btnResetClick(Sender: TObject);
begin
      ResetPass();
end;

//....................dirty works here
procedure TfrmChangePass_profile.Clear();
begin
    txtNewPass.Clear;
    txtRType.Clear;
end;

procedure TfrmChangePass_profile.ResetPass();
    var
    endec:TWordTriple;
begin
    endec[0]:=1000;
    endec[1]:=9292;
    endec[2]:=3333;

    if(txtNewPass.Text='')then begin
        MessageDlg('Please enter new password',mtConfirmation,[mbOK],0);
        txtNewPass.SetFocus;
    end
    else if(txtRType.Text='')then begin
        MessageDlg('Retype password',mtConfirmation,[mbOK],0);
        txtRType.SetFocus;
    end
    else if(txtRType.Text <> txtNewPass.Text)then begin
        MessageDlg('Password did not match',mtError,[mbOK],0);
        txtRType.SetFocus;
    end
    else begin
     if MessageDlg('Change password?',mtConfirmation,[mbYes, mbNo], 0)= mrYes then begin

          id:= frmProfile.txtIDNum.Caption;
         
         if DModule.TR.InTransaction then
            DModule.TR.Commit;

            DModule.QR.SQL.Clear;
            DModule.QR.SQL.Add('UPDATE usersinfo SET UPassword=:A  WHERE UI_ID =:B');
                DModule.QR.Params.AsString[0] := Trim(TextEncrypt(txtNewPass.Text, endec));
                DModule.QR.Params.AsString[1] := id;

                  MessageDlg('Password successfully changed',mtInformation,[mbOK],0);
            DModule.QR.Prepare;
            DModule.QR.Open();

          if DModule.TR.InTransaction then
              DModule.TR.Commit;
              frmUserInformation.loadAccountInfo;
              Clear();
              Close;
    end
    else begin
      MessageDlg('Nothing change',mtInformation,[mbOK],0);
      Clear();
    end
    end
end;

end.
