Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
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
Xiaoxuan Li
comp30023-2019-project-2
Commits
07d16329
Commit
07d16329
authored
May 24, 2019
by
Xiaoxuan-Val
Browse files
Options
Downloads
Patches
Plain Diff
finally get dh.c XD
parent
f99decd5
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
dh.c
+201
-0
201 additions, 0 deletions
dh.c
hash.txt
+1
-0
1 addition, 0 deletions
hash.txt
project2.c
+14
-1
14 additions, 1 deletion
project2.c
pwd6sha256
+0
-0
0 additions, 0 deletions
pwd6sha256
xiaoxuanl4@172.26.37.159
+174
-0
174 additions, 0 deletions
xiaoxuanl4@172.26.37.159
with
390 additions
and
1 deletion
dh.c
0 → 100644
+
201
−
0
View file @
07d16329
/*socket programming part comes from comp30023 lab5 sample code.
credit to the person who write that sample code*/
/* A simple client program for server.c
To compile: gcc client.c -o client
To run: start the server, then the client */
#include
<stdio.h>
#include
<stdlib.h>
#include
<math.h>
#include
<string.h>
#include
<strings.h>
#include
<sys/types.h>
#include
<sys/socket.h>
#include
<netinet/in.h>
#include
<netdb.h>
#include
<unistd.h>
#include
<ctype.h>
// Function to compute a^m mod n, comes from www.techiedelight.com
int
compute
(
int
a
,
int
m
,
int
n
)
{
int
r
;
int
y
=
1
;
while
(
m
>
0
)
{
r
=
m
%
2
;
// fast exponention
if
(
r
==
1
)
y
=
(
y
*
a
)
%
n
;
a
=
a
*
a
%
n
;
m
=
m
/
2
;
}
return
y
;
}
int
main
(
int
argc
,
char
**
argv
)
{
int
sockfd
,
portno
,
n
;
struct
sockaddr_in
serv_addr
;
struct
hostent
*
server
;
char
buffer
[
256
];
portno
=
7800
;
/* 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
);
}
if
(
connect
(
sockfd
,
(
struct
sockaddr
*
)
&
serv_addr
,
sizeof
(
serv_addr
))
<
0
)
{
perror
(
"ERROR connecting"
);
exit
(
0
);
}
/* Do processing
*/
//send user name
bzero
(
buffer
,
256
);
sprintf
(
buffer
,
"xiaoxuanl4
\n
"
);
n
=
write
(
sockfd
,
buffer
,
strlen
(
buffer
));
printf
(
"%s
\n
"
,
buffer
);
//send g^b(mod p)
int
g
=
15
;
int
p
=
97
;
bzero
(
buffer
,
256
);
char
hash
[
256
];
FILE
*
file
=
popen
(
"openssl sha256 dh.c"
,
"r"
);
if
(
!
file
){
printf
(
"something wrong with your command or file
\n
"
);
exit
(
0
);
}
while
(
fgets
(
hash
,
256
,
file
)){
strcat
(
buffer
,
hash
);
bzero
(
hash
,
256
);
}
pclose
(
file
);
printf
(
"%s
\n
"
,
buffer
);
char
res
[
256
];
char
*
temp
=
malloc
(
3
*
sizeof
(
char
*
));
temp
=
strstr
(
buffer
,
"SHA256(dh.c)= "
)
+
14
;
temp
[
2
]
=
'\0'
;
printf
(
"%s
\n
"
,
temp
);
strcpy
(
res
,
temp
);
temp
=
NULL
;
free
(
temp
);
int
b1
=
0
;
int
b2
=
0
;
int
b
=
0
;
if
(
isalpha
(
res
[
0
]))
{
//fix with alpha case
b1
=
res
[
0
]
-
87
;
}
else
{
//fix with integer case
b1
=
res
[
0
]
-
48
;
}
if
(
isalpha
(
res
[
1
]))
{
//fix with alpha case
b2
=
res
[
1
]
-
87
;
}
else
{
//fix with integer case
b2
=
res
[
1
]
-
48
;
}
b
=
(
b1
*
16
)
+
b2
;
printf
(
"%d
\n
"
,
b
);
bzero
(
buffer
,
256
);
bzero
(
res
,
256
);
//Assume I am Bob
int
Bob_req
=
compute
(
g
,
b
,
p
);
sprintf
(
buffer
,
"%d
\n
"
,
Bob_req
);
n
=
write
(
sockfd
,
buffer
,
strlen
(
buffer
));
printf
(
"%s
\n
"
,
buffer
);
if
(
n
<
0
)
{
perror
(
"ERROR writing to socket"
);
exit
(
0
);
}
bzero
(
buffer
,
256
);
n
=
read
(
sockfd
,
buffer
,
255
);
printf
(
"%s
\n
"
,
buffer
);
if
(
n
<
0
)
{
perror
(
"ERROR reading from socket"
);
exit
(
0
);
}
//server is Alice
int
Alice_res
=
atoi
(
buffer
);
int
Bob_res_again
=
compute
(
Alice_res
,
b
,
p
);
bzero
(
buffer
,
256
);
sprintf
(
buffer
,
"%d
\n
"
,
Bob_res_again
);
n
=
write
(
sockfd
,
buffer
,
strlen
(
buffer
));
printf
(
"%s
\n
"
,
buffer
);
bzero
(
buffer
,
256
);
n
=
read
(
sockfd
,
buffer
,
255
);
printf
(
"status report: %s
\n
"
,
buffer
);
return
0
;
}
This diff is collapsed.
Click to expand it.
hash.txt
0 → 100644
+
1
−
0
View file @
07d16329
SHA256(dh.c)= e2ee2bf56359a4028d098efd16d996c11fedd74587356f82573f1b2f2029d0ea
This diff is collapsed.
Click to expand it.
project2.c
+
14
−
1
View file @
07d16329
...
@@ -40,7 +40,6 @@ int main(int argc, char* argv[]){
...
@@ -40,7 +40,6 @@ int main(int argc, char* argv[]){
}
}
//one arguement scenario
//one arguement scenario
if
(
argc
==
2
){
if
(
argc
==
2
){
int
count
=
atoi
(
argv
[
1
]);
int
count
=
atoi
(
argv
[
1
]);
char
guess
[
10
];
char
guess
[
10
];
...
@@ -58,9 +57,23 @@ int main(int argc, char* argv[]){
...
@@ -58,9 +57,23 @@ int main(int argc, char* argv[]){
}
}
//two arguements scenarios
//two arguements scenarios
if
(
argc
==
3
){
FILE
*
test_file
;
FILE
*
target_file
;
test_file
=
fopen
(
argv
[
2
],
"r"
);
target_file
=
fopen
(
argv
[
3
],
"rb"
);
unsigned
char
target_harshes
[
10000
][
SHA256_BLOCK_SIZE
];
}
return
0
;
return
0
;
}
}
...
...
This diff is collapsed.
Click to expand it.
pwd6sha256
0 → 100644
+
0
−
0
View file @
07d16329
File added
This diff is collapsed.
Click to expand it.
xiaoxuanl4@172.26.37.159
0 → 100644
+
174
−
0
View file @
07d16329
/*socket programming part comes from comp30023 lab5 sample code.
credit to the person who write that sample code*/
/* A simple client program for server.c
To compile: gcc client.c -o client
To run: start the server, then the client */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
// Function to compute a^m mod n, comes from www.techiedelight.com
int compute(int a, int m, int n)
{
int r;
int y = 1;
while (m > 0)
{
r = m % 2;
// fast exponention
if (r == 1)
y = (y*a) % n;
a = a*a % n;
m = m / 2;
}
return y;
}
int main(int argc, char ** argv)
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent * server;
char buffer[256];
portno = 7800;
/* 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);
}
if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
perror("ERROR connecting");
exit(0);
}
/* Do processing
*/
//send user name
bzero(buffer, 256);
sprintf(buffer, "xiaoxuanl4\n");
n = write(sockfd, buffer, strlen(buffer));
//send g^b(mod p)
int g=15;
int p=97;
bzero(buffer, 256);
char command[50];
strcpy(command, "openssl sha256 dh.c > hash.txt" );
FILE *fp;
fp = fopen("hash.txt", "r");
while((fgetc(fp) != EOF) && (fgetc(fp) != ' ')) {
char first = fgetc(fp);
char second = fgetc(fp);
}
int first_num;
int second_num;
//converting
if(isalpha(first)){
first_num = first - 87;
}
else{
first_num = first - 48;
}
if(isalpha(second)){
second_num = second - 87;
}
else{
second_num = second - 48;
}
//finally my b value
int b = (first_num * 16) + second_num;
//Assume I am Bob
int Bob_req = compute(g, b, p);
sprintf(buffer, "%d\n", Bob_req);
n = write(sockfd, buffer, strlen(buffer));
if (n < 0)
{
perror("ERROR writing to socket");
exit(0);
}
bzero(buffer, 256);
n = read(sockfd, buffer, 255);
if (n < 0)
{
perror("ERROR reading from socket");
exit(0);
}
//server is Alice
int Alice_res = atoi(buffer);
int Bob_res_again= compute(Alice_res, b, p);
bzero(buffer, 256);
sprintf(buffer, "%d\n", Bob_res_again);
n = write(sockfd, buffer, strlen(buffer));
bzero(buffer, 256);
n = read(sockfd, buffer, 255);
printf("status report: %s\n", buffer);
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