linkedin facebook linkedin facebook nod32

Delphi dasturlash tilida RSA-algoritm

Muallif: Mengliyev Sh.

Qo`shilgan sana: 2017-08-22

Delphi dasturlash tilida RSA-algoritm

Delphi dasturlash tilida RSA-algoritmning dasturi quyidagidan iborat.

Dastur kodi

unit Unit1;

interface

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

type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Edit6: TEdit;
Edit7: TEdit;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
Label10: TLabel;
Label11: TLabel;
Edit8: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);


private
{ Private declarations }
fn:Int64;
d:Int64;
e:Int64;
n:Int64;
kod,dekod:Integer;
shifr_kod, de_shifr_kod:array[0..1000] of Int64;
e_str,d_str:string;
p,p1:array [0..1000]of Int64;
count:Integer;
public
procedure Taqqoslama(Sender: TObject);
procedure ShifrBinar(Sender: TObject);
procedure DeShifrBinar(Sender: TObject);
procedure Shifr(Sender: TObject);
procedure DeShifr(Sender: TObject);

{ Public declarations }
end;

var
Form1: TForm1;

implementation

uses Unit2;
function EKUB(a:Int64; b:Int64):Integer;
var
i:Integer;
a1:Int64;
b1:Int64;
z:Int64;
begin
a1:=a;
b1:=b;
z:=a1 mod b1;
if z=0 then ekub:=b1 else
while z<>0 do
begin
a1:=b1;
b1:=z;
z:=a1 mod b1;
end;
ekub:=b1;
end;
procedure DIAFANT(var x0,y0:Int64;u:Int64;v:Int64;ekub:Int64;c:Int64);
begin
if c mod ekub=0 then
begin
x0:=Round(u*c/ekub);
y0:=Round(v*c/ekub);
end
else
begin
MessageBeep(MB_ICONHAND);
ShowMessage('Tenglama butun yechimga ega emas!!!');
end;
end;
procedure EVKLID(var alfa,beta:Int64; a:Int64; b:Int64);
var
a1,b1:Integer;
y,x:array[-1 ..1000] of Integer;
q:array[1 ..1000] of Integer;
r:array[1 ..1000] of Integer;
i,z:Integer;
begin
i:=1;
x[0]:=0; y[0]:=1; x[-1]:=1; y[-1]:=0;
r[i]:=a mod b;
q[i]:=a div b;
x[i]:=x[i-2]-q[i]*x[i-1];
y[i]:=y[i-2]-q[i]*y[i-1];
a1:=a;
b1:=b;
if r[i]=0 then
begin
alfa:=x[i];
beta:=y[i];
end
else
while a1 mod b1 <>0 do
begin
r[i]:=a1 mod b1;
q[i]:=a1 div b1;
x[i]:=x[i-2]-q[i]*x[i-1];
y[i]:=y[i-2]-q[i]*y[i-1];
a1:=b1;
b1:=r[i];
Inc(i);
alfa:=x[i-1];
beta:=y[i-1];
end;
end;
procedure TForm1.Taqqoslama(Sender: TObject);
var
a,b,c:Int64;
ek:Integer;
alfa,beta,x0,y0,x:Int64;
t:Integer;
begin
a:=d;
b:=fn;
c:=1;
//t:=1;
ek:=EKUB(a,b);
if ek=1 then
begin
ShowMessage('Tenglama yagona echimga ega!');
end;
Evklid(alfa,beta,a,b);
diafant(x0,y0,alfa,beta,ek,c);
if c mod ek=0 then
for t:=1 to ek do
begin
x:=(x0+Round(t*b/ek)) mod b;
end;
e:=x;
end;
procedure TForm1.ShifrBinar(Sender: TObject);
var
i:Integer;
e_bin:string;
begin
e_bin:=IntToBin(e);
for i:=0 to 31 do
if e_bin[i]='1' then
begin
e_str:=Copy(e_bin,i,31);
Break;
end;
end;
procedure TForm1.Shifr(Sender: TObject);
var
i1:Integer;
e_bin_new:PCHAR;
len:Integer;
begin
e_bin_new:=Pchar(e_str);
len:=Length(e_bin_new)-1;
p[len]:=kod mod n;
for i1:=len-1 downto 0 do
begin
if e_bin_new[len-i1]='1' then
p[i1]:=(p[i1+1]*p[i1+1]*kod) mod n
else
p[i1]:=(p[i1+1]*p[i1+1]) mod n;
end;
shifr_kod[count]:=p[0];
Inc(count);
end;
procedure TForm1.DeShifrBinar(Sender: TObject);
var
i:Integer;
d_bin:string;
begin
d_bin:=IntToBin(d);
for i:=0 to 31 do
if d_bin[i]='1' then
begin
d_str:=Copy(d_bin,i,31);
Break;
end;
end;
procedure TForm1.DeShifr(Sender: TObject);
var
i1:Integer;
d_bin_new:PCHAR;
len:Integer;
begin
d_bin_new:=Pchar(d_str);
len:=Length(d_bin_new)-1;
p1[len]:=dekod mod n;
for i1:=len-1 downto 0 do
begin
if d_bin_new[len-i1]='1' then
p1[i1]:=(p1[i1+1]*p1[i1+1]*dekod) mod n
else
p1[i1]:=(p1[i1+1]*p1[i1+1]) mod n;
end;
de_shifr_kod[count]:=p1[0];
Inc(count);
end;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
p:Integer;
q:Integer;
begin
p:=StrToInt(Edit1.Text);
q:=StrToInt(Edit2.Text);
n:=p*q;
fn:=(p-1)*(q-1);
randomize;
repeat
d:=random(9999999999);
Ekub(d,fn);
until ekub(d,fn)=1;
Edit3.Text:=IntToStr(d);
Taqqoslama(Sender);
Edit4.Text:=IntToStr(e);
ShifrBinar(Sender);
DeShifrBinar(Sender);
count:=0;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
str_kod:string;
i:Integer;
begin
Edit6.Text:='';
str_kod:=Edit5.Text;
for i:=1 to Length(str_kod) do
begin
kod:=ord(str_kod[i]);
Shifr(Sender);
Edit6.Text:=Edit6.Text+IntToStr(shifr_kod[i-1]);
Edit6.Text:=Edit6.Text+' ';
end;
Edit7.Text:=Edit6.Text;

end;

procedure TForm1.Button3Click(Sender: TObject);
var
str_kod,str1:string;
probel:byte;
i,i1:Integer;
begin
Edit8.Text:='';
count:=0;
i:=0;
probel:=32;
str1:='';
str_kod:=Edit7.Text;
for i1:=1 to Length(str_kod) do
begin
str1:=str1+str_kod[i1];

if probel=ord(str_kod[i1+1]) then
begin
dekod:=StrToInt(str1);
DeShifr(Sender);
Edit8.Text:=Edit8.Text+chr(de_shifr_kod[i]);
str1:='';
inc(i);
end;
end;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
Close;
end;
end.

941 marta o`qildi.

Parol:
Eslab qolish.


Ro`yhatdan o`tish

testing

+998915878681

Siz o`z maxsulotingizni 3D reklama ko`rinishda bo`lishini xohlaysizmi? Unda xamkorlik qilamiz.

3D Reklama


Рейтинг@Mail.ru
Рейтинг@Mail.ru

Besucherzahler
счетчик посещений