Skip to content
Snippets Groups Projects
Commit f7bed6d2 authored by Abhisha Nirmalathas's avatar Abhisha Nirmalathas
Browse files

completed diffie hellman exchange

parent 798da93e
No related branches found
No related tags found
No related merge requests found
...@@ -22,7 +22,6 @@ int modulo(int a, int b, int n){ ...@@ -22,7 +22,6 @@ int modulo(int a, int b, int n){
} }
int main(int argc, char *argv[]){ int main(int argc, char *argv[]){
printf("rnning main\n");
int g,p; int g,p;
g = 15; g = 15;
...@@ -41,15 +40,9 @@ int main(int argc, char *argv[]){ ...@@ -41,15 +40,9 @@ int main(int argc, char *argv[]){
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
char privKey[5]; char privKey[5];
// memset(privKey,0,0);
memcpy(privKey, argv[2], 2); memcpy(privKey, argv[2], 2);
privKey[2] = '\0'; 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); int b = (int)strtol(privKey, NULL, 16);
printf("b is %d\n\n", b);
portno = 7800; portno = 7800;
/* Translate host name into peer's IP address ; /* Translate host name into peer's IP address ;
...@@ -92,25 +85,18 @@ int main(int argc, char *argv[]){ ...@@ -92,25 +85,18 @@ int main(int argc, char *argv[]){
printf("send username\n"); printf("send username\n");
char *username = "nirmalathasa"; char *username = "nirmalathasa";
n = write(sockfd, username, strlen(username)); n = write(sockfd, username, strlen(username));
printf("n is %d\n", n);
n = write(sockfd, "\n", 1); n = write(sockfd, "\n", 1);
printf("n is %d\n", n);
if (n < 0) if (n < 0)
{ {
perror("ERROR writing to socket"); perror("ERROR writing to socket");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
printf("g: %d, b: %d, p: %d\n", g,b,p); int client_secret = modulo(g,b,p);
// long int numerator = (int)pow(g,b); printf("my key is is %d\n", client_secret);
// printf("numerator: %ld\n", numerator); char secret[100];
// int val = numerator%p; sprintf(secret,"%d", client_secret);
int val = modulo(g,b,p); for(int i=0; i < strlen(secret); i++){
printf("my key is is %d\n", val); n = write(sockfd, &secret[i], 1);
char value[100];
sprintf(value,"%d", val);
for(int i=0; i < strlen(value); i++){
printf("write value is %s\n", value);
n = write(sockfd, value, 1);
if (n < 0) if (n < 0)
{ {
perror("ERROR writing to socket"); perror("ERROR writing to socket");
...@@ -118,26 +104,16 @@ int main(int argc, char *argv[]){ ...@@ -118,26 +104,16 @@ int main(int argc, char *argv[]){
} }
} }
n = write(sockfd, "\n", 1); n = write(sockfd, "\n", 1);
printf("write newline n is %d\n", n);
while(1){
n = read(sockfd, buffer, 255); n = read(sockfd, buffer, 255);
printf("read n is %d\n", n);
printf("bufferrr %s and size if %d\n\n",buffer, strlen(buffer));
if(!strncmp(buffer,"\n",1)){ if(!strncmp(buffer,"\n",1)){
printf("is newline");
buffer[n] = 0; buffer[n] = 0;
break;
}
// buffer[n] = 0;
} }
buffer[n] = 0; buffer[n] = 0;
int responseDiffie = atoi(buffer); int responseDiffie = atoi(buffer);
printf("response is %d\n", responseDiffie); char shared_value[100];
char sec[100]; sprintf(shared_value,"%d",modulo(responseDiffie, b, p));
sprintf(sec,"%d",modulo(val, responseDiffie, p)); for(int i=0; i < strlen(shared_value); i++){
printf("final is %s\n", sec); n = write(sockfd, &shared_value[i], 1);
for(int i=0; i < strlen(value); i++){
n = write(sockfd, value, 1);
if (n < 0) if (n < 0)
{ {
perror("ERROR writing to socket"); perror("ERROR writing to socket");
......
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment