program encryption;

uses crt,graph;

type
  textfile = text;

  procedure helpthem;
  begin
    writeln;
    writeln('Command line:');
    writeln('        code_it file.name [/d]');
    writeln('        /d = decode, default encode');
    writeln('Overwrites file! Don''t forget you''re passwords!!!');
    halt;
  end;

  procedure decode(var ch:char;var val,val2:integer);
  var
    a,i:integer;
  begin
    a := ord(ch);
    if a>30 then
    begin
      a := a - random(val2 mod 65535);
      while a<30 do a := a + 220;
      val := a;
      ch := chr(a);
    end;
  end;

  procedure encode(var ch:char;var val,val2:integer);
  var
    a,i:integer;
  begin
    a := ord(ch);
    if a>30 then
    begin
      a := a + random(val2 mod 65535);
      while a>220 do a := a - 220;
      val := a;
      ch := chr(a);
    end;
  end;

  procedure main;
  var
    f1,f2:textfile;
    str:string;
    ch:char;
    val,val2,i:integer;
  begin
    writeln('CODE_IT v-2.4 written by Retupmoc 7');
    if paramstr(1)='' then helpthem;
    write('Enter password #1:');
    readln(val);
    write('Enter password #2:');
    readln(val2);
    randseed := val;
    assign(f1,paramstr(1));
    reset(f1);
    assign(f2,'temp');
    rewrite(f2);
    str := paramstr(2);
    for i:=1 to length(str) do str[i] := upcase(str[i]);
    while not eof(f1) do
    begin
      read(f1,ch);
      if str='/D' then decode(ch,val,val2)
      else encode(ch,val,val2);
      write(f2,ch);
    end;
    close(f1);
    close(f2);
    erase(f1);
    rename(f2,paramstr(1));
    if str='/D' then writeln('File decoded.')
    else writeln('File encoded.');
  end;

begin
  main;
end.
