diff --git a/DiffieHellman.c b/DiffieHellman.c index e0991ea9553aea8e5c7c35b25f7bc4c216c8b455..ca888897696617ac0491299a9bcdd1afdc162081 100644 --- a/DiffieHellman.c +++ b/DiffieHellman.c @@ -7,6 +7,7 @@ #include <strings.h> #include <sys/socket.h> #include <sys/types.h> +#include <math.h> #include <unistd.h> int modulo(int a, int b, int n){ long long x=1, y=a; @@ -21,8 +22,8 @@ int modulo(int a, int b, int n){ } int main(int argc, char *argv[]){ - - long int g,p; + printf("rnning main\n"); + int g,p; g = 15; @@ -39,8 +40,17 @@ int main(int argc, char *argv[]){ fprintf(stderr, "usage %s hostname port\n", argv[0]); exit(EXIT_FAILURE); } - - portno = atoi(7800); + char privKey[5]; + // memset(privKey,0,0); + memcpy(privKey, argv[2], 2); + privKey[2] = '\0'; + printf("privKey is %s\n\n", privKey); + // char hex[10]; + // snprintf(hex, 2, "0x%s",privKey); + // printf("hex is %s\n\n", hex); + int b = (int)strtol(privKey, NULL, 16); + printf("b is %d\n\n", b); + portno = 7800; /* Translate host name into peer's IP address ; * This is name translation service by the operating system */ @@ -65,6 +75,7 @@ int main(int argc, char *argv[]){ /* Create TCP socket -- active open * Preliminary steps: Setup: creation of active open socket */ sockfd = socket(AF_INET, SOCK_STREAM, 0); + printf("created scoket\n"); if (sockfd < 0) { perror("ERROR opening socket"); @@ -78,19 +89,28 @@ int main(int argc, char *argv[]){ } /* Do processing */ + printf("send username\n"); char *username = "nirmalathasa"; n = write(sockfd, username, strlen(username)); + printf("n is %d\n", n); n = write(sockfd, "\n", 1); + printf("n is %d\n", n); if (n < 0) { perror("ERROR writing to socket"); exit(EXIT_FAILURE); } - long int b = argv[2]; - int val = (g^b)%p; - char *value = itoa(val); + printf("g: %d, b: %d, p: %d\n", g,b,p); + // long int numerator = (int)pow(g,b); + // printf("numerator: %ld\n", numerator); + // int val = numerator%p; + int val = modulo(g,b,p); + printf("my key is is %d\n", val); + char value[100]; + sprintf(value,"%d", val); for(int i=0; i < strlen(value); i++){ - n = write(sockfd, value[i], 1); + printf("value is %s\n", value); + n = write(sockfd, value, 1); if (n < 0) { perror("ERROR writing to socket"); @@ -98,9 +118,11 @@ int main(int argc, char *argv[]){ } } n = write(sockfd, "\n", 1); + printf("n is %d\n", n); while(1){ n = read(sockfd, buffer, 255); - if(buffer =="\n"){ + printf("n is %d\n", n); + if(!strcmp(buffer,"\n")){ buffer[n] = 0; break; } @@ -108,10 +130,12 @@ int main(int argc, char *argv[]){ } buffer[n] = 0; int responseDiffie = atoi(buffer); - printf("response is %s\n", responseDiffie); - int sec = itoa(modulo(value, responseDiffie, p)); + printf("response is %d\n", responseDiffie); + char sec[100]; + sprintf(sec,"%d",modulo(val, responseDiffie, p)); + printf("final is %s\n", sec); for(int i=0; i < strlen(value); i++){ - n = write(sockfd, value[i], 1); + n = write(sockfd, value, 1); if (n < 0) { perror("ERROR writing to socket"); @@ -120,7 +144,7 @@ int main(int argc, char *argv[]){ } n = write(sockfd, "\n", 1); n = read(sockfd, buffer, 255); - printf("response message is %d\n", buffer); + printf("response message is %s\n", buffer); close(sockfd); return 0;