unit unitChangePass;

interface

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

type
  TfrmChangePass = class(TForm)
    Groupbox: TGroupBox;
    Label8: TLabel;
    Label9: TLabel;
    txtNew: TEdit;
    txtVerifyNew: TEdit;
    BitBtn1: TBitBtn;
    btnUpdate: TBitBtn;
    procedure btnUpdateClick(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private declarations }
  public
      user : Integer;
      fullname : String;
      USERID : Integer;
      password : String;
      procedure changePass;
  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: TfrmChangePass;

implementation

uses DM, unitUserList;

{$R *.dfm}

procedure TfrmChangePass.btnUpdateClick(Sender: TObject);
begin
      changePass;
end;

procedure TfrmChangePass.BitBtn1Click(Sender: TObject);
begin
      Close;
end;

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.changePass;
var   endec:TWordTriple;
begin
     endec[0]:=1000;
   endec[1]:=9292;
   endec[2]:=3333;

    if (txtNew.Text='') then begin

       ShowMessage('Please fill the field');

   end
         else if (txtVerifyNew.Text = '') then begin

       ShowMessage('Please fill the field');

   end

   else if (txtNew.Text=txtVerifyNew.Text) then
   begin
    if DataModule1.tr.InTransaction then
      DataModule1.tr.Commit;
      DataModule1.query.SQL.Clear;
      DataModule1.query.SQL.Add('UPDATE USER_ACCOUNTS SET USERPASS=:A WHERE USERID=:B');
      DataModule1.query.Params.AsString[0]:=TextEncrypt(txtVerifyNew.Text,endec);
      DataModule1.query.Params.AsInteger[1]:=strtoint(frmUser.lvAccnts.ItemFocused.caption);

      DataModule1.query.Prepare;
     DataModule1.query.Open();
      ShowMessage('Password changes successfully saved!');

      txtNew.Clear;
      txtVerifyNew.Clear;

      if DataModule1.tr.InTransaction then
        DataModule1.tr.Commit;
    end;
end;

end.
