From 07d1632976333c72c8d59742e32625fed5ce518d Mon Sep 17 00:00:00 2001 From: Xiaoxuan-Val <lixiaoxuan233@gmail.com> Date: Fri, 24 May 2019 16:01:46 +1000 Subject: [PATCH] finally get dh.c XD --- dh.c | 201 +++++++++++++++++++++++++++++++++++++++ hash.txt | 1 + project2.c | 15 ++- pwd6sha256 | Bin 0 -> 640 bytes xiaoxuanl4@172.26.37.159 | 174 +++++++++++++++++++++++++++++++++ 5 files changed, 390 insertions(+), 1 deletion(-) create mode 100644 dh.c create mode 100644 hash.txt create mode 100644 pwd6sha256 create mode 100644 xiaoxuanl4@172.26.37.159 diff --git a/dh.c b/dh.c new file mode 100644 index 0000000..d456d67 --- /dev/null +++ b/dh.c @@ -0,0 +1,201 @@ +/*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; +} diff --git a/hash.txt b/hash.txt new file mode 100644 index 0000000..de80120 --- /dev/null +++ b/hash.txt @@ -0,0 +1 @@ +SHA256(dh.c)= e2ee2bf56359a4028d098efd16d996c11fedd74587356f82573f1b2f2029d0ea diff --git a/project2.c b/project2.c index 3070209..5527c63 100644 --- a/project2.c +++ b/project2.c @@ -40,7 +40,6 @@ int main(int argc, char* argv[]){ } //one arguement scenario - if( argc == 2){ int count = atoi(argv[1]); char guess[10]; @@ -58,9 +57,23 @@ int main(int argc, char* argv[]){ } //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; } diff --git a/pwd6sha256 b/pwd6sha256 new file mode 100644 index 0000000000000000000000000000000000000000..e4b6da58e3fbe3eb7f1588d5352565c89d245481 GIT binary patch literal 640 zcmZ2vDnGa3bKc&Eca-05yR`q%E03@Xt;ZDJ>~xuDxThlhzo1p=&t>blCCt-jII`|% ztMrrlxxac*W@Oh{VXeA-H;O~kd6r)JVAR&yTjyM|SmVue&G1coJ0E<D@trQ)QL<g} z)rkvDfsB^wi_KS8SI*Ku8GFIA=lS=8b9U)UF><l%*YAFROUO$5gz-bxnyCE_JN$!w zT$^q(@&D3F^$8c9J*5l{1y^~sM3h=4%U3LV-PiT0M_h360efQuuSZ7u3;cU_`B}Vj z*)Vg7=fu0V{O?~L`Y~7C{5?y@xetAu_o`LbD|8?HJW2CQy2h@&tE(DVue4d5YYeKj z>pNw8Xn9`LnL80*H!bhkQhC%-IbLhROvUeQ`X8RWuz8?%ne|>>+TnJcp0?>5jiRM1 zi#V#97&`9uKjv)@ei*9uQ|X5Gx(T808Y9HHcV0L;TgjDOv*wy|UFw5v;_GY5wF0a& z<xcQ>w|qG=b8mUa#M>|O?)BVOJ8|<!i{$mED=KR~M$Y-#_n4zT(5LE;c8;1~>9fW? z{}_$+LjqPwZCT8~$|;k?wpps-*8fRyy!*B<%g<h{{B-Ftjev}uQU5~=876XFNtWFx zdBvpT#G4qA%l!GX)#RhXQeO+{?%99iYJQ~0@;l~bn^r5oopS5<s$}2uyXNmrs(p4( z&OrCKNy?2C@6+3vHgD3uA+}SlAVke7$bZpAsb&$6Kl|6rl@RFl@agAW)Om7dt<UAZ zCl+q-sjfRcujqY1YFy3Mg1hGu8U+g`UMZ^86EI0Dw=sCO;(37I=l?13H+jD84mUmY z;?DWwau*go`x6^;^kvJ5>+keVT}VjY`)k?rYeD=b1_!Tiv+s#8S#(~aFr;)R^J;}B FsQ_c$LlFP~ literal 0 HcmV?d00001 diff --git a/xiaoxuanl4@172.26.37.159 b/xiaoxuanl4@172.26.37.159 new file mode 100644 index 0000000..0ac2528 --- /dev/null +++ b/xiaoxuanl4@172.26.37.159 @@ -0,0 +1,174 @@ +/*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; +} -- GitLab