Program BlowFish_CBC;

 {**************************************************************************}
 {**  **  **       BLW-CBC.pas (c) 2001 by Dutra de Lacerda       **  **  **}
 {**  **  **   Cipher Utilility for protecting sensitive files!   **  **  **}
 {**  **  **          Hereby distributed as: OPEN SOURCE          **  **  **}
 {**************************************************************************}
 (*                                                                        *)
 (* This code is Open Source in respect to the similar status of Blowfish  *)
 (* It is Copyrighted by "(2001) Dutra de Lacerda"                         *)
 (* You are allowed to use it with the usual conditions:                   *)
 (*   - Maintain this Copyright notice in the code.                        *)
 (*   - Make reference to this code, and Author, in your application.      *)
 (*   - Graciously communicate the Author your use of this code.           *)
 (*                                                                        *)
 (* ---------------------------------------------------------------------- *)
 (*                                                                        *)
 (*                           Finding the Author                           *)
 (*                           ~~~~~~~~~~~~~~~~~~                           *)
 (*  This units author can be reached via the following E-Mail addresses:  *)
 (*                                                                        *)
 (*      Via InterNet    : dulac@ip.pt                                     *)
 (*            ...and    : dutra.lacerda@mail.telepac.pt                   *)
 (*                                                                        *)
 (* ---------------------------------------------------------------------- *)
 (*                                                                        *)
 (*                          Finding New Versions                          *)
 (*                          ~~~~~~~~~~~~~~~~~~~~                          *)
 (*      My Home Site    : http://planeta.ip.pt/~ip200075                  *)
 (*            ...and    : http://www.factor-h.com                         *)
 (*                                                                        *)
 (*      Net Archives    : ftp://garbo.uwasa.fi/pc/crypt                   *)
 (*                      : ftp://garbo.uwasa.fi/pc/security                *)
 (*                      : ftp://ftp.elf.stuba.sk/pub/pc/security          *)
 (*                                                                        *)
 (* ---------------------------------------------------------------------- *)

uses _BLWFILE, _RunError, Dos;

{$M $F000,$F000,$10000}

var
  Key : String;
  InputFile,
  OutputFile : String;
  Choice : String[3];
  Input,
  Output : file;
  Hour,
  Minute,
  Second,
  Second100 : Word;


  function File_Exists(const FileName : String) : Boolean;
  var
    F : file;

  begin
    Assign(F, FileName);

    {$i-}   (* Disable Automatic I/O Error Control *)
    Reset(F, 1);
    {$i+}   (* Enable Automatic I/O Error Control *)

    If IoResult = 0 then  (* File is there, Check! *)
      close(F);           (* Go back to last state *)

    File_Exists := IoResult = 0;    (* Give Result *)
  end;


begin
  CustomExitInit;
  WriteLn;
  WriteLn(#9, ':::::::::::::: Blowfish-CBC CRYPT-Utility v1.6g  ::::::::::::::');
  WriteLn(#9, '::  Free Program by : Dutra de Lacerda,  Email: dulac@ip.pt  ::');
  WriteLn(#9, ':::::::::::: PROTECTS your FILES, and your PRIVACY ::::::::::::');
  Choice := Copy(ParamStr(1), 1, 3);
  Choice[1] := Upcase(Choice[1]);
  InputFile := ParamStr(3);
  OutputFile := ParamStr(4);
  if ((Choice = 'E') or (Choice = 'D')) and
  (ParamStr(2) <> '') and
  (ParamStr(3) <> '') then
    begin
      if File_Exists(ParamStr(3)) then
        begin
          Key := Copy(ParamStr(2), 1, 56);
          WriteLn(#13, #10, 'Initializing key');
          BLW_INIT(Key);
          GetTime(Hour, Minute, Second, Second100);
          WriteLn(Hour, ':', Minute, ':', Second, ':', Second100);

          if Choice = 'E' then
            BLW_CRYPT(InputFile, OutputFile);
          if Choice = 'D' then
            BLW_DECRYPT(InputFile, OutputFile);
        {
          if Choice = 'A' then
            BLW_MAIL(InputFile, OutputFile);
        }
          GetTime(Hour, Minute, Second, Second100);
          WriteLn(Hour, ':', Minute, ':', Second, ':', Second100);
          WriteLn(OutputFile, ' written!');

        end {If 3th Argument (a File) exists}
      else {If 3th Argument (a File) does not exists}
        Writeln(#13, #10, 'File ',ParamStr(3),' does not exist! Aborting...');
    end {IF-else there Are 3 Arguments}
  else
    begin
      WriteLn;
      WriteLn(#9, '嬪様様様様様様様様様様様様様様様様様様様様様様様様様様様様様様�');
      WriteLn(#9, '� 臼臼臼臼臼臼臼臼�  BlowFishCBC v1.6 Help  臼臼臼臼臼臼臼臼� �');
      WriteLn(#9, '団陳陳陳陳陳陳陳陳陳陳陳陳陳陳陳陳陳陳陳陳陳陳陳陳陳陳陳陳陳陳�');
      WriteLn(#9, '�        BLW-x86 (E|?|D) <Key> <InputFile> [<OutputFile>]     �');
      WriteLn(#9, '�                 � � �                          �            �');
      WriteLn(#9, '�      Encrypt 陳潰 � 青陳 Decrypt           Optional!        �');
      WriteLn(#9, '�                Options                                      �');
      WriteLn(#9, '団陳陳陳陳陳陳陳陳陳陳陳陳陳 Notes 陳陳陳陳陳陳陳陳陳陳陳陳陳調');
      WriteLn(#9, '� For Best Protection Use LONG Keys, using ''_'' as spaces!     �');
      WriteLn(#9, '�  Cipher files are NOT compressable :: Do it BEFORE EnCrypt! �');
      WriteLn(#9, '�   Default extension is ''BFC'' :: Original extension is saved �');
      WriteLn(#9, '塒様様様様様様様様様様様様様様様様様様様様様様様様様様様様様様�');
      WriteLn(#9, '                                                               ');
      WriteLn(#9, '    Example          Mode        Key         File              ');
      WriteLn(#9, '    ~~~~~~~           |           |            |               ');
      WriteLn(#9, ' C:\SECRET>  BLW-CBC  E  MyOwn_Secret_Key  myfile.txt  <Enter> ');
    end;
  CustomExitDone;
end.
