Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 04-15-2009, 08:05 PM
123452 123452 is offline
Junior Member
 
Join Date: Apr 2009
Posts: 2
Default Diffie-Hellman algorithm

Hi. Does anyone got source code for Diffie-Hellman algorithm? http://en.wikipedia.org/wiki/Diffie-Hellman
Reply With Quote
  #2  
Old 04-15-2009, 08:17 PM
develyoy develyoy is offline
Senior Member
 
Join Date: Nov 2007
Posts: 628
Default RE: Diffie-Hellman algorithm

If I understand correctly, the algorhythm describes a bit of math you need to perform, and the rest is up to the communication method or component you are using. (Or am I missing something?)
Reply With Quote
  #3  
Old 04-15-2009, 08:19 PM
Jasser Jasser is offline
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,059
Default RE: Diffie-Hellman algorithm

See [link]http://www.codeproject.com/KB/security/DiffieHellmanExample.aspx[/link]

Also try google [link]http://www.google.com/search?hl=ar&q=Diffie-Hellman+algorithm+source+code&meta=&aq=f&oq=[/link]

Regards,
Abdulaziz Jasser
Reply With Quote
  #4  
Old 04-15-2009, 08:43 PM
123452 123452 is offline
Junior Member
 
Join Date: Apr 2009
Posts: 2
Default RE: Diffie-Hellman algorithm

Yep, i think it's so. I need to make programm for my university project, to show this method on 1 computer, so i think i don't need any communication method.
In fact i got some source code with the DH algo, but it looks like it differs from algorithm mentioned in the wiki.

- Menu form:
Code:
unit Unit_menu;

interface

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

type
  TForm_menu = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form_menu: TForm_menu;

implementation

uses Unit_kluch, Unit_zashifr, Unit_rasshifr, Unit_metod;

{$R *.dfm}

procedure TForm_menu.Button1Click(Sender: TObject);
begin
 form_kluch.show;
end;

procedure TForm_menu.Button2Click(Sender: TObject);
begin
form_zashifr.show;
form_zashifr.stringgrid2.cells[0,0]:='a';
end;

procedure TForm_menu.Button3Click(Sender: TObject);
begin
form_rasshifr.show;
form_rasshifr.stringgrid3.cells[0,0]:='p';
form_rasshifr.stringgrid3.cells[0,1]:='v';
form_rasshifr.stringgrid3.cells[0,2]:='u';
form_rasshifr.stringgrid3.cells[0,3]:='b';
end;

procedure TForm_menu.Button4Click(Sender: TObject);
begin
Form_menu.close
end;

procedure TForm_menu.Button5Click(Sender: TObject);
begin
form_metod.show
end;
end.

- Forming a key:
Code:
unit Unit_kluch;

interface

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

type
  TForm_kluch = class(TForm)
    Edit1: TEdit;
    Label1: TLabel;
    Button1: TButton;
    Label3: TLabel;
    StringGrid2: TStringGrid;
    Label5: TLabel;
    StringGrid3: TStringGrid;
    Label4: TLabel;
    Button4: TButton;
    SaveDialog1: TSaveDialog;
    Button5: TButton;
    SaveDialog2: TSaveDialog;
    Button6: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form_kluch: TForm_kluch;

implementation

uses Unit_zashifr, Unit_rasshifr;


{$R *.dfm}
function NOD(A: integer;  B: integer): integer;
begin
    while (a <> 0) and (b <> 0) do
       if a >= b then
         a := a mod b
       else 
         b := b mod a;
    NOD := a + b; { >48= - =>;L }
end;

procedure TForm_kluch.Button1Click(Sender: TObject);
var
  c,i,j,u,v: integer;
  a: array[1..8] of integer;
  b: array[1..8] of integer;
  p: array[1..8] of integer;
begin
if Edit1.Text='' then
   showmessage('Enter number >255 for key generation!')
else if strtoint(Edit1.Text)<=255 then
   showmessage('Enter number >255 for key generation!')
else
begin
u:=strtoint(Edit1.Text);
randomize;
p[1]:=random(7)+1;
c:=1;
for i:=2 to 8 do
    while (cp[j]) then
            c:=c+1;
      end;
b[1]:=1;
for i:=2 to 8 do
  b[i]:=b[i-1]*2;
i:=0;
v:=0;
while (i<>1) and (v

- ciphering the text:
Code:
unit Unit_rashifr;

interface

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

type
  TForm_rasshifr = class(TForm)
    Label4: TLabel;
    Label5: TLabel;
    StringGrid3: TStringGrid;
    Label2: TLabel;
    Button1: TButton;
    Label1: TLabel;
    Memo1: TMemo;
    Memo2: TMemo;
    OpenDialog1: TOpenDialog;
    Button2: TButton;
    SaveDialog1: TSaveDialog;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form_rasshifr: TForm_rasshifr;

implementation

{$R *.dfm}

function BinToInt(bin: string): integer;
var
  i,j,h: integer;
begin
   Result := 0;
   for i := 1 to 8 do
     begin
       h:=1;
       for j := 1 to 8-i do
         h:=h*2;
       Result := Result+StrToInt( bin[i])*h;
     end;
end;



procedure TForm_rasshifr.Button1Click(Sender: TObject);
var k,l,a,i,j,n,s,u,v,d,v1: integer;
  b: array[1..8] of integer;
  p: array[1..8] of integer;
  r: array[1..8] of integer;
  m: array[1..8] of integer;
  c: string;
//  m: array[1..8] of string;
  t: string;
begin
n:=0;
for i:=1 to 8 do
  if (StringGrid3.Cells[i,0]='') or (StringGrid3.Cells[i,3]='') then n:=n+1;
if (StringGrid3.Cells[1,1]='') or (StringGrid3.Cells[1,2]='') then n:=n+1;
if n>0 then
   showmessage('Enter secret key!')
else
begin
Memo1.text:='';
c:=Memo2.Text;
v:=strtoint(stringgrid3.cells[1,1]);
u:=strtoint(stringgrid3.cells[1,2]);
v1:=2;
while ((v1*v) mod u <>1) do
  v1:=v1+1;
for i:=1 to 8 do
  begin
    p[i]:=strtoint(stringgrid3.cells[i,0]);
    b[i]:=strtoint(stringgrid3.cells[i,3]);
    a:=a+((v*b[i]) mod u);
  end;
l:=length(inttostr(a));
n:=length(c);
k:=Trunc(n/l);
for j:=1 to k do
  begin
    s:=strtoint(copy(c,(j-1)*l+1,l));
    d:=(s*v1) mod u;
    for i:=0 to 7 do
      begin
        r[8-i]:=Trunc(d/b[8-i]);
        d:=d mod b[8-i]
      end;
    for i:=1 to 8 do
      m[i]:=r[p[i]];
    t:='';
    for i:=1 to 8 do
       t:=t+inttostr(m[i]);
    Memo1.text:=Memo1.text+chr(bintoint(t));
  end;
label1.visible:=true;
Memo1.visible:=true;
end;
end;
procedure TForm_rasshifr.Button2Click(Sender: TObject);
begin
if opendialog1.Execute then memo2.Lines.LoadFromFile(opendialog1.FileName);
end;

procedure TForm_rasshifr.Button3Click(Sender: TObject);
begin
if savedialog1.Execute then memo1.Lines.SaveToFile(savedialog1.FileName);
end;
end.

- deciphering the text:
Code:
unit Unit_rasshifr;

interface

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

type
  TForm_rasshifr = class(TForm)
    Label4: TLabel;
    Label5: TLabel;
    StringGrid3: TStringGrid;
    Label2: TLabel;
    Button1: TButton;
    Label1: TLabel;
    Memo1: TMemo;
    Memo2: TMemo;
    OpenDialog1: TOpenDialog;
    Button2: TButton;
    SaveDialog1: TSaveDialog;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    OpenDialog2: TOpenDialog;
    Memo3: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form_rasshifr: TForm_rasshifr;

implementation

{$R *.dfm}

function BinToInt(bin: string): integer;
var
  i,j,h: integer;
begin
   Result := 0;
   for i := 1 to 8 do
     begin
       h:=1;
       for j := 1 to 8-i do
         h:=h*2;
       Result := Result+StrToInt( bin[i])*h;
     end;
end;

procedure TForm_rasshifr.Button1Click(Sender: TObject);
var k,l,a,i,j,n,s,u,v,d,v1: integer;
  b: array[1..8] of integer;
  p: array[1..8] of integer;
  r: array[1..8] of integer;
  m: array[1..8] of integer;
  c: string;
  t: string;
begin
n:=0;
for i:=1 to 8 do
  if (StringGrid3.Cells[i,0]='') or (StringGrid3.Cells[i,3]='') then n:=n+1;
if (StringGrid3.Cells[1,1]='') or (StringGrid3.Cells[1,2]='') then n:=n+1;
if n>0 then
   showmessage('Enter secret key!')
else
begin
Memo1.text:='';
c:=Memo2.Text;
v:=strtoint(stringgrid3.cells[1,1]);
u:=strtoint(stringgrid3.cells[1,2]);
v1:=2;
while ((v1*v) mod u <>1) do
  v1:=v1+1;
for i:=1 to 8 do
  begin
    p[i]:=strtoint(stringgrid3.cells[i,0]);
    b[i]:=strtoint(stringgrid3.cells[i,3]);
    a:=a+((v*b[i]) mod u);
  end;
l:=length(inttostr(a));
n:=length(c);
k:=Trunc(n/l);
for j:=1 to k do
  begin
    s:=strtoint(copy(c,(j-1)*l+1,l));
    d:=(s*v1) mod u;
    for i:=0 to 7 do
      begin
        r[8-i]:=Trunc(d/b[8-i]);
        d:=d mod b[8-i]
      end;
    for i:=1 to 8 do
      m[i]:=r[p[i]];
    t:='';
    for i:=1 to 8 do
       t:=t+inttostr(m[i]);
    Memo1.text:=Memo1.text+chr(bintoint(t));
  end;
label1.visible:=true;
Memo1.visible:=true;
Button3.visible:=true;
end;
end;
procedure TForm_rasshifr.Button2Click(Sender: TObject);
begin
if opendialog1.Execute then memo2.Lines.LoadFromFile(opendialog1.FileName);
end;
procedure TForm_rasshifr.Button3Click(Sender: TObject);
begin
if savedialog1.Execute then memo1.Lines.SaveToFile(savedialog1.FileName);
end;
procedure TForm_rasshifr.Button4Click(Sender: TObject);
begin
Form_rasshifr.close
end;
procedure TForm_rasshifr.Button5Click(Sender: TObject);
var i,j: integer;
    kl:string;
    kluch: array[1..18] of string;
begin
if opendialog2.Execute then
  begin
    memo3.Lines.LoadFromFile(opendialog2.FileName);
    kl:=memo3.Text;
    for i:=1 to 18 do
      begin
        j:=pos(';',kl);
        kluch[i]:=copy(kl,1,j-1);
        delete(kl,1,j);
      end;
    for i:=1 to 8 do
      begin
        stringgrid3.cells[i,0]:=kluch[i*2-1];
        stringgrid3.cells[i,3]:=kluch[i*2]
      end;
    stringgrid3.cells[1,1]:=kluch[17];
    stringgrid3.cells[1,2]:=kluch[18]
  end;
end;
end.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 03:04 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.