Advertisement
Guest User

TI cool

a guest
Oct 14th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 14.90 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7.   Dialogs, StdCtrls, ExtCtrls;
  8.  
  9. const
  10.    Size = 250000;
  11.  
  12. type
  13.    MyBoolean = array [1 .. 8] of Boolean;
  14.    Arr = array [1 .. Size] of Byte;
  15.    ChArr = array [1 .. Size] of Char;
  16.  
  17.   TForm1 = class(TForm)
  18.     Label1: TLabel;
  19.     RadioGroup1: TRadioGroup;
  20.     OpenDialog1: TOpenDialog;
  21.     Memo1: TMemo;
  22.     Memo2: TMemo;
  23.     Memo3: TMemo;
  24.     btnCipher: TButton;
  25.     Button1Decipher: TButton;
  26.     Button1LoadFile: TButton;
  27.     edtKey: TEdit;
  28.     Button1: TButton;
  29.     Label2: TLabel;
  30.     Label3: TLabel;
  31.     Label4: TLabel;
  32.     Button2: TButton;
  33.     procedure Cipher(var f, fcipher: file);
  34.     procedure RC4(var f, fcipher: file);
  35.     procedure GenerateKey;
  36.     function BinToDec(var tempBool: MyBoolean): Byte;
  37.     procedure Button1LoadFileClick(Sender: TObject);
  38.     procedure btnCipherClick(Sender: TObject);
  39.     procedure Button1DecipherClick(Sender: TObject);
  40.     function DecToBin(Number: Byte): MyBoolean;
  41.     procedure edtKeyKeyPress(Sender: TObject; var Key: Char);
  42.     procedure FormCreate(Sender: TObject);
  43.     procedure Button1Click(Sender: TObject);
  44.     procedure RadioGroup1Enter(Sender: TObject);
  45.     procedure Button2Click(Sender: TObject);
  46.    
  47.   private
  48.     { Private declarations }
  49.   public
  50.     { Public declarations }
  51.   end;
  52.  
  53. var
  54.    Form1: TForm1;
  55.    i, count1: Integer;
  56.    f, fcipher, fDecipher: file;
  57.    bufArr: MyBoolean;
  58.    OrigFile: array [1 .. Size] of Byte;
  59.    key: array [1 .. Size * 8] of Boolean;
  60.    reg: array [1 .. 28] of Boolean;
  61.    FName: string;
  62.    count: LongInt;
  63.    arrkey: array [0 .. 700000] of Byte;
  64.    checkRC4key: Boolean;
  65.    buf: Arr;
  66.    fNameCipher, fNameDecipher: string;
  67.    keyLFSR: string;
  68.    keyLFSRCounter, edtCounter: Integer;
  69.  
  70. implementation
  71.  
  72. {$R *.dfm}
  73.  
  74.  
  75.  
  76. procedure TForm1.GenerateKey;
  77. var
  78.    i: Integer;
  79. begin
  80.    for i := 1 to 28 do
  81.    begin
  82.       //reg[i] := (edtKey.Text[i] = '1');
  83.       reg[i] := (keyLFSR[i] = '1');
  84.    end;
  85. end;
  86.  
  87. function TForm1.BinToDec(var tempBool: MyBoolean): Byte;
  88.  
  89.    function MyPower(Number, N: Byte): Byte;
  90.    var
  91.       i, temp: Byte;
  92.    begin
  93.       temp := 1;
  94.       for i := 1 to N do
  95.          temp := temp * Number;
  96.       Result := temp;
  97.    end;
  98.  
  99. var
  100.    i: Integer;
  101.    tempRez, pos: Byte;
  102. begin
  103.    pos := 0;
  104.    i := 8;
  105.    tempRez := 0;
  106.    while i <> 0 do
  107.    begin
  108.       if tempBool[i] = True then
  109.       begin
  110.          tempRez := tempRez + MyPower(2, pos);
  111.       end;
  112.       i := i - 1;
  113.       pos := pos + 1;
  114.    end;
  115.    Result := tempRez;
  116. end;
  117.  
  118. function TForm1.DecToBin(Number: Byte): MyBoolean;
  119. var
  120.    i: Integer;
  121.    modRez: Byte;
  122.    tempBool: MyBoolean;
  123. begin
  124.    for i := 1 to 8 do
  125.       tempBool[i] := False;
  126.    for i := 8 downto 1 do
  127.    begin
  128.       modRez := Number mod 2;
  129.       Number := Number div 2;
  130.       if modRez = 1 then
  131.          tempBool[i] := True
  132.       else
  133.          tempBool[i] := False;
  134.    end;
  135.    Result := tempBool;
  136. end;
  137.  
  138. procedure TForm1.RC4(var f, fcipher: file);
  139. var
  140.    j, i, outSize: Integer;
  141.    flag, finish: Boolean;
  142.    str: string;
  143.    fText: TextFile;
  144.    s: array [0 .. 256] of Byte;
  145.    t, k: LongInt;
  146.    key: Arr;
  147.    bufch: ChArr;
  148. begin
  149.    finish := True;
  150.    flag := False;
  151.    while not flag do
  152.    begin
  153.       BlockRead(f, buf, Size, count1);
  154.       if count1 < Size then
  155.          flag := True;
  156.       if finish then
  157.       begin
  158.          Memo1.Lines.Clear;
  159.          if count1 < 8 then
  160.             outSize := count1
  161.          else
  162.             outSize := 8;
  163.          for i := 1 to outSize do
  164.          begin
  165.             {bufArr := DecToBin(buf[i]);
  166.             str := '';
  167.             j := 1;
  168.             while j <= 8 do
  169.             begin
  170.                if bufArr[j] = True then
  171.                   str := str + '1'
  172.                else
  173.                   str := str + '0';
  174.                j := j + 1;
  175.             end;
  176.             Memo1.Lines.Add(str);  }
  177.             Memo1.Lines.Add(inttostr(buf[i]));
  178.          end;
  179.       end;
  180.       for i := 0 to 255 do
  181.          s[i] := i;
  182.       j := 0;
  183.       for i := 0 to 255 do
  184.       begin
  185.          j := (j + s[i] + arrkey[i mod count]) mod 256;
  186.          t := s[i];
  187.          s[i] := s[j];
  188.          s[j] := t;
  189.       end;
  190.       Assignfile(fText, FName + 'KEY_RC4');
  191.       Rewrite(fText);
  192.       i := 0;
  193.       j := 0;
  194.       for k := 0 to count1 - 1 do
  195.       begin
  196.          i := (i + 1) mod 256;
  197.          j := (j + s[i]) mod 256;
  198.          t := s[i];
  199.          s[i] := s[j];
  200.          s[j] := t;
  201.          key[k] := s[(s[i] + s[j]) mod 256];
  202.          write(fText, key[k], ' ');
  203.       end;
  204.       closefile(fText);
  205.  
  206.       if count1 < Size then
  207.          flag := True;
  208.       if finish then
  209.       begin
  210.          Form1.Memo2.Lines.Clear;
  211.          if count1 < 8 then
  212.             outSize := count1
  213.          else
  214.             outSize := 8;
  215.          for i := 0 to outSize - 1 do
  216.          begin
  217.             str := '';
  218.             str := str + inttostr(key[i]);
  219.             Form1.Memo2.Lines.Add(str);
  220.          end;
  221.       end;
  222.       if finish then
  223.       begin
  224.          Memo3.Lines.Clear;
  225.          if count1 < 8 then
  226.             outSize := count1
  227.          else
  228.             outSize := 8;
  229.          for i := 1 to outSize do
  230.             Form1.Memo3.Lines.Add(inttostr(key[i - 1] xor buf[i]));
  231.          for i := 1 to count1 do
  232.          begin
  233.             bufch[i] := chr(key[i - 1] xor buf[i]);
  234.          end;
  235.          for i := 1 to count1 do         ////
  236.             buf[i] := ord(bufch[i]);     ////
  237.          finish := False;                ////
  238.       end;
  239.       //BlockWrite(fcipher, bufch, count1);
  240.       BlockWrite(fcipher, buf, count1);    ///
  241.    end;
  242. end;
  243.  
  244. procedure TForm1.Cipher(var f, fcipher: file);
  245. var
  246.    tempReg: Boolean;
  247.    i, j, outSize: Integer;
  248.    flag, finish: Boolean;
  249.    s: string;
  250.    first, last: Integer;
  251. begin
  252.    GenerateKey;
  253.    finish := True;
  254.    flag := False;
  255.    while not flag do
  256.    begin
  257.       BlockRead(f, buf, Size, count1);
  258.       if count1 < Size then
  259.          flag := True;
  260.       i := 1;
  261.       while i <= (count1 * 8) do
  262.       begin
  263.          key[i] := reg[1];
  264.          tempReg := reg[1] xor reg[26];
  265.          j := 1;
  266.          while j <= 27 do
  267.          begin
  268.          //for j := 1 to 27 do
  269.             reg[j] := reg[j + 1];
  270.             inc(j);
  271.          end;
  272.          reg[28] := tempReg;
  273.          Inc(i);
  274.       end;
  275.  
  276.       if finish then
  277.       begin
  278.          Memo1.Lines.Clear;
  279.          if count1 < 8 then
  280.             outSize := count1
  281.          else
  282.             outSize := 8;
  283.          for i := 1 to outSize do
  284.          begin
  285.             bufArr := DecToBin(buf[i]);
  286.             s := '';
  287.             j := 1;
  288.             while j <= 8 do
  289.             begin
  290.                if bufArr[j] = True then
  291.                   s := s + '1'
  292.                else
  293.                   s := s + '0';
  294.                j := j + 1;
  295.             end;
  296.             Memo1.Lines.Add(s);
  297.          end;
  298.  
  299.          Memo2.Lines.Clear;
  300.          if count1 < 8 then
  301.             outSize := count1
  302.          else
  303.             outSize := 8;
  304.          for i := 1 to outSize do
  305.          begin
  306.             first := i * 8;
  307.             last := i * 8 - 7;
  308.             j := 8;
  309.             while first >= last do
  310.             begin
  311.                bufArr[j] := key[first]; // ??? ??????
  312.                Dec(first);
  313.                Dec(j);
  314.             end;
  315.             s := '';
  316.             j := 1;
  317.             while j <= 8 do
  318.             begin
  319.                if bufArr[j] = True then
  320.                   s := s + '1'
  321.                else
  322.                   s := s + '0';
  323.                j := j + 1;
  324.             end;
  325.             Memo2.Lines.Add(s);
  326.          end;
  327.       end;
  328.       for i := 1 to count1 do
  329.       begin
  330.          first := i * 8;
  331.          last := i * 8 - 7;
  332.          j := 8;
  333.          while first >= last do
  334.          begin
  335.             bufArr[j] := key[first]; // ?????? ??? ????????? 1 ?????
  336.             Dec(first);
  337.             Dec(j);
  338.          end;
  339.          buf[i] := buf[i] xor BinToDec(bufArr);
  340.       end;
  341.       if finish then
  342.       begin
  343.          Memo3.Lines.Clear;
  344.          if count1 < 8 then
  345.             outSize := count1
  346.          else
  347.             outSize := 8;
  348.          for i := 1 to outSize do
  349.          begin
  350.             bufArr := DecToBin(buf[i]);
  351.             s := '';
  352.             j := 1;
  353.             while j <= 8 do
  354.             begin
  355.                if bufArr[j] = True then
  356.                   s := s + '1'
  357.                else
  358.                   s := s + '0';
  359.                j := j + 1;
  360.             end;
  361.             Memo3.Lines.Add(s);
  362.          end;
  363.          finish := False;
  364.       end;
  365.       BlockWrite(fcipher, buf, count1);
  366.    end;
  367. end;
  368.  
  369. procedure TForm1.Button1LoadFileClick(Sender: TObject);
  370. var
  371.    temp, keyRC4: string;
  372.    c: string;
  373.    x, code: LongInt;
  374. begin
  375.    {if Length(edtKey.Text) <> 28 then
  376.    begin
  377.       ShowMessage('??????? ????');
  378.       Exit;
  379.    end;    }
  380.    if RadioGroup1.ItemIndex = 0 then
  381.    begin
  382.       label1.Visible := True;
  383.       temp := '';
  384.       if length(keyLFSR) < 28 then
  385.       begin
  386.       //temp := temp + copy(keyLFSR, 1, length(keyLFSR));
  387.          for i := 1 to 28 - length(keyLFSR) do
  388.             temp := temp + '0';
  389.       //insert(temp, keyLFSR, length(keyLFSR) + 1);
  390.          temp := temp + copy(keyLFSR, 1, length(keyLFSR));
  391.          keyLFSRCounter := 28;
  392.          edtCounter := 28;
  393.       end;
  394.       if length(keyLFSR) >= 28 then
  395.       begin
  396.          temp := temp + copy(keyLFSR, 1, 28);
  397.          keyLFSRCounter := 28;
  398.          edtCounter := 28;
  399.       end;
  400.       if Length(keyLFSR) = 0 then
  401.       begin
  402.          ShowMessage('Ââåäèòå êîððåêòíûé êëþ÷');
  403.          Exit;
  404.       end;
  405.       {label1.Caption := IntToStr(Length(temp)) + '/28';
  406.       edtKey.Text := temp;
  407.       keyLFSR :=  temp;}
  408.       edtKey.Text := temp;
  409.       keyLFSR :=  temp;
  410.       label1.Caption := IntToStr(keyLFSRCounter) + '/28';
  411.    end;
  412.    if RadioGroup1.ItemIndex = 1 then
  413.    begin
  414.       count := 0;
  415.       temp := edtKey.Text;
  416.       c := temp + ' ';
  417.       while c <> '' do
  418.       begin
  419.          Val(Copy(c, 1, pos(' ', c) - 1), x, code);
  420.          Delete(c, 1, pos(' ', c));
  421.          if (code = 0) and (x <= 256) and (x >= 0) then
  422.          begin
  423.             arrkey[count] := x;
  424.             Inc(count);
  425.          end;
  426.       end;
  427.       if count = 0 then
  428.       begin
  429.          ShowMessage('Ââåäèòå êîððåêòíûé êëþ÷');
  430.          Exit;
  431.       end;
  432.    end;
  433.    if not OpenDialog1.Execute then
  434.       Exit;
  435.    FName := OpenDialog1.FileName;
  436.    Assignfile(f, FName);
  437.    reset(f, 1);
  438.    i := Length(FName);
  439.    fNameCipher := FName;
  440.    while fNameCipher[i] <> '.' do
  441.       Dec(i);
  442.    Insert('  CIPHER', fNameCipher, i);
  443.    Assignfile(fcipher, fNameCipher);
  444.    fNameDecipher := fNameCipher;
  445.    Insert(' DE', fNameDecipher, pos('CIPHER', fNameDecipher));
  446.    Assignfile(fDecipher, fNameDecipher);
  447.    btnCipher.Enabled := True;
  448.    Button1Decipher.Enabled := True;
  449. end;
  450.  
  451.  
  452.  
  453. procedure TForm1.btnCipherClick(Sender: TObject);
  454. begin
  455.    {if Length(edtKey.Text) <> 28 then
  456.    begin
  457.       ShowMessage('??????? ????');
  458.       Exit;
  459.    end;  }
  460.    reset(f, 1);
  461.    Rewrite(fcipher, 1);
  462.    if RadioGroup1.ItemIndex = 0 then
  463.       Cipher(f, fcipher);
  464.    if RadioGroup1.ItemIndex = 1 then
  465.       RC4(f, fcipher);
  466.    closefile(f);
  467.    closefile(fcipher);
  468.    ShowMessage('Successfully ciphered');
  469. end;
  470.  
  471. procedure TForm1.Button1DecipherClick(Sender: TObject);
  472. begin
  473.    {if Length(edtKey.Text) <> 28 then
  474.    begin
  475.       ShowMessage('??????? ????');
  476.       Exit;
  477.    end;     }
  478.    reset(fcipher, 1);
  479.    Rewrite(fDecipher, 1);
  480.    if RadioGroup1.ItemIndex = 0 then
  481.       Cipher(fcipher, fDecipher);
  482.    if RadioGroup1.ItemIndex = 1 then
  483.       RC4(fcipher, fDecipher);
  484.    closefile(fcipher);
  485.    closefile(fDecipher);
  486.    ShowMessage('Successfully deciphered');
  487. end;
  488.  
  489. procedure TForm1.edtKeyKeyPress(Sender: TObject; var Key: Char);
  490. var
  491.   temp: string;
  492.   i, k: integer;
  493. const
  494.    Digit: set of Char = ['0', '1'];
  495. begin
  496.    if RadioGroup1.ItemIndex = 0 then
  497.       label1.Visible := True
  498.    else
  499.    //begin
  500.       label1.Visible := False;
  501.    //   Button1.Visible := True;
  502.    //end;
  503.    if (key <> #8) and (keyLFSRCounter < 45) then
  504.       inc(edtCounter);
  505.    if key in digit then
  506.    begin
  507.       //keyLFSR := keyLFSR + key;
  508.       //inc(keyLFSRCounter);
  509.       //label1.Caption := IntToStr(Length(keyLFSR)) + '/28';
  510.       if keyLFSRCounter <> 45 then
  511.       begin
  512.          keyLFSR := keyLFSR + key;
  513.          inc(keyLFSRCounter);
  514.          label1.Caption := IntToStr(Length(keyLFSR)) + '/28';
  515.       end;
  516.    end;
  517.    if key = #8 then
  518.    begin
  519.       if edtCounter <> 0 then
  520.       begin
  521.          if edtKey.Text[edtCounter] in Digit then
  522.          begin
  523.             keyLFSR := copy(keyLFSR, 1, keyLFSRCounter - 1);
  524.             dec(keyLFSRCounter);
  525.             //label1.Caption := IntToStr(Length(keyLFSR)) + '/28';
  526.             label1.Caption := IntToStr(keyLFSRCounter) + '/28';
  527.          end;
  528.          dec(edtCounter);
  529.       end;
  530.    end;
  531. end;
  532.  
  533. procedure TForm1.FormCreate(Sender: TObject);
  534. begin
  535.    keyLFSRCounter := 0;
  536.    edtCounter := 0;
  537. end;
  538.  
  539. procedure TForm1.Button1Click(Sender: TObject);
  540. var
  541.    c: string;
  542.    x, code: LongInt;
  543. begin
  544.    count := 0;
  545.    if OpenDialog1.Execute then
  546.    begin
  547.       Assignfile(input, OpenDialog1.FileName);
  548.       reset(input);
  549.       while not(Eof(input)) do
  550.       begin
  551.          readln(c);
  552.          c := c + ' ';
  553.          while c <> '' do
  554.          begin
  555.             Val(Copy(c, 1, pos(' ', c) - 1), x, code);
  556.             Delete(c, 1, pos(' ', c));
  557.             if (code = 0) and (x <= 256) and (x >= 0) then
  558.             begin
  559.                arrkey[count] := x;
  560.                Inc(count);
  561.             end;
  562.          end;
  563.       end;
  564.       closefile(input);
  565.    end;
  566.    checkRC4key := False;
  567.    if count <> 0 then
  568.       checkRC4key := True
  569.    else
  570.       Application.MessageBox
  571.         ('ôàéë ñ êëþ÷îì ïóñò èëè ñîäåðæèò íåêîððåêòíûå äàííûå', 'îøèáêà')
  572. end;
  573.  
  574. procedure TForm1.RadioGroup1Enter(Sender: TObject);
  575. begin
  576.    edtKey.Visible := not ((RadioGroup1.ItemIndex = 0) and (RadioGroup1.ItemIndex = 1));
  577.    edtKey.Clear;
  578.    Memo1.Clear;
  579.    Memo2.Clear;
  580.    Memo3.Clear;
  581.    Button2.Visible := True;
  582.    Button1LoadFile.Visible := True;
  583.    btnCipher.Visible := True;
  584.    Button1Decipher.Visible := True;
  585.    btnCipher.Enabled := False;
  586.    Button1Decipher.Enabled := False;
  587.    label1.Caption := '0' + '/28';
  588. end;
  589.  
  590. procedure TForm1.Button2Click(Sender: TObject);
  591. begin
  592.    keyLFSRCounter := 0;
  593.    edtCounter := 0;
  594.    keyLFSR := '';
  595.    edtKey.Clear;
  596.    Memo1.Clear;
  597.    Memo2.Clear;
  598.    Memo3.Clear;
  599.    label1.Visible := false;
  600.    label1.Caption := '0' + '/28';
  601.    RadioGroup1.ItemIndex := -1;
  602.    Button1LoadFile.Visible := False;
  603.    btnCipher.Visible := False;
  604.    Button1Decipher.Visible := False;
  605. end;
  606.  
  607. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement