Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
achattaraj_comp30023_2019_project-2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Aneesh Chattaraj
achattaraj_comp30023_2019_project-2
Commits
157b9c24
Commit
157b9c24
authored
May 24, 2019
by
aneesh
Browse files
Options
Downloads
Patches
Plain Diff
dh
parent
65423358
No related branches found
No related tags found
No related merge requests found
Pipeline
#2853
canceled
May 24, 2019
Stage: build
Stage: test
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
dh
+0
-0
0 additions, 0 deletions
dh
dh.c
+157
-0
157 additions, 0 deletions
dh.c
with
157 additions
and
0 deletions
dh
0 → 100644
+
0
−
0
View file @
157b9c24
File added
This diff is collapsed.
Click to expand it.
dh.c
0 → 100644
+
157
−
0
View file @
157b9c24
/* A simple client program for server.c
To compile: gcc client.c -o client
To run: start the server, then the client */
#include
<stdbool.h>
#include
<sys/types.h>
#include
<sys/socket.h>
#include
<netinet/in.h>
#include
<unistd.h>
#include
<arpa/inet.h>
#include
<netdb.h>
#include
<unistd.h>
#include
<signal.h>
#include
<stdio.h>
#include
<string.h>
#include
<fcntl.h>
#include
<errno.h>
#include
<sys/time.h>
#include
<stdlib.h>
#include
<memory.h>
#include
<ifaddrs.h>
#include
<net/if.h>
#include
<stdarg.h>
// https://www.techiedelight.com/c-program-demonstrate-diffie-hellman-algorithm/
int
compute
(
int
g
,
int
b
,
int
p
)
{
int
r
;
int
y
=
1
;
while
(
b
>
0
)
{
r
=
b
%
2
;
// fgst exponention
if
(
r
==
1
)
y
=
(
y
*
g
)
%
p
;
g
=
g
*
g
%
p
;
b
=
b
/
2
;
}
return
y
;
}
int
main
(
int
argc
,
char
**
argv
)
{
int
sockfd
,
portno
,
n
,
b
,
f
;
struct
sockaddr_in
serv_addr
;
struct
hostent
*
server
;
portno
=
7800
;
char
buffer
[
256
];
b
=
atoi
(
argv
[
1
]);
bzero
((
char
*
)
&
serv_addr
,
sizeof
(
serv_addr
));
/* Translate host name into peer's IP address ;
* This is name translation service by the operating system
*/
server
=
gethostbyname
(
"172.26.37.44"
);
if
(
server
==
NULL
)
{
fprintf
(
stderr
,
"ERROR, no such host
\n
"
);
exit
(
0
);
}
/* Building data structures for socket */
bzero
((
char
*
)
&
serv_addr
,
sizeof
(
serv_addr
));
serv_addr
.
sin_family
=
AF_INET
;
bcopy
(
server
->
h_addr_list
[
0
],
(
char
*
)
&
serv_addr
.
sin_addr
.
s_addr
,
server
->
h_length
);
serv_addr
.
sin_port
=
htons
(
portno
);
/* Create TCP socket -- active open
* Preliminary steps: Setup: creation of active open socket
*/
sockfd
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
if
(
sockfd
<
0
)
{
perror
(
"ERROR opening socket"
);
exit
(
0
);
}
else
{
printf
(
"Socket connection successful
\n
"
);
}
if
(
connect
(
sockfd
,
(
struct
sockaddr
*
)
&
serv_addr
,
sizeof
(
serv_addr
))
<
0
)
{
perror
(
"ERROR"
);
exit
(
0
);
}
bool
protocol
=
true
;
char
*
message_phase
[]
=
{
"send_username"
,
"send_gb_mod_p"
,
"send_ga_mod_p"
,
"success"
,
"end"
};
char
*
current_phase
=
"send_username"
;
printf
(
"Send Usename"
);
char
*
username
=
"achattaraj
\n
"
;
bzero
(
buffer
,
256
);
sprintf
(
buffer
,
username
);
n
=
write
(
sockfd
,
buffer
,
strlen
(
buffer
));
current_phase
=
message_phase
[
1
];
printf
(
"send gb mod p
\n
"
);
bzero
(
buffer
,
256
);
b
=
compute
(
15
,
atoi
(
argv
[
1
]),
97
);
sprintf
(
buffer
,
"%d
\n
"
,
b
);
n
=
write
(
sockfd
,
buffer
,
strlen
(
buffer
));
current_phase
=
message_phase
[
2
];
printf
(
"send ga mod p
\n
"
);
bzero
(
buffer
,
256
);
n
=
read
(
sockfd
,
buffer
,
255
);
int
a
=
atoi
(
buffer
);
bzero
(
buffer
,
256
);
a
=
compute
(
a
,
atoi
(
argv
[
1
]),
97
);
sprintf
(
buffer
,
"%d
\n
"
,
a
);
n
=
write
(
sockfd
,
buffer
,
strlen
(
buffer
));
n
=
0
;
while
(
strstr
(
buffer
,
"Success"
)
==
NULL
)
{
n
=
read
(
sockfd
,
buffer
,
255
);
current_phase
=
"end"
;
buffer
[
n
]
=
0
;
printf
(
"%s
\n
"
,
buffer
);
close
(
sockfd
);
return
0
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment