RSA c implementation












0














This is my first attempt to implement an crypto algorithm.



I am using C and gmp.h to interact with big number.



Here is my code, you can run in with -lgmp parameter:



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <sys/types.h>
#include "gmp.h"



void generatePrimes(mpz_t* p, mpz_t* q);
void computeNandF(mpz_t* q, mpz_t* p, mpz_t *phi, mpz_t* n);
void generateE(mpz_t* phi, mpz_t* e);
void enc(mpz_t* e, mpz_t* n, mpz_t* d, mpz_t* c, char);
void dec(mpz_t* m, mpz_t* c, mpz_t* d, mpz_t* n);
void makeKeys(mpz_t n, mpz_t e, mpz_t d, mpz_t p, mpz_t q);
void encrFile(mpz_t e, mpz_t n, mpz_t d, mpz_t c);

gmp_randstate_t stat;

int main() {


mpz_t p, q, phi, e, n, d, c, dc;
char msg[40] = "welcome to cryptoworld";
int *mes;
int len = strlen(msg);
mpz_init(p);
mpz_init(q);
mpz_init(phi);
mpz_init(e);
mpz_init(n);
mpz_init(d);
mpz_init(c);
mpz_init(dc);

// RSA algorithm
generatePrimes(&p, &q);
computeNandF(&q, &p, &phi, &n);
generateE(&phi, &e);
// extended Euclidean
mpz_invert(d, e, phi);
printf("d = ");
mpz_out_str(stdout, 10, d);
printf("n------------------------------------------------------------------------------------------n");


enc(&e, &n, &d, &c, msg);

dec(&dc, &c, &d, &n);

printf("n------------------------------------------------------------------------------------------n");
printf("encrypt message = ");
mpz_out_str(stdout, 10, c);
printf("n");
printf("n------------------------------------------------------------------------------------------n");
printf("message as int after decr = ");
mpz_out_str(stdout, 10, dc);
printf("n");


mpz_export(mes, (size_t*) malloc(sizeof (size_t)), 1, sizeof (mes[0]), 0, 0, dc);
char r[40];
printf("message as string after decr = ");
for (int i = 0; i < len; i++) {
r[i] = (char) mes[i];
printf("%c", r[i]);
}
printf("n");

encrFile(e, n, d, c);

mpz_clear(p);
mpz_clear(q);
mpz_clear(phi);
mpz_clear(n);
mpz_clear(e);
mpz_clear(c);
mpz_clear(d);
mpz_clear(dc);
return 0;
}

void generatePrimes(mpz_t* p, mpz_t* q) {

int primetest;
long sd = 0;
mpz_t seed;
gmp_randinit(stat, GMP_RAND_ALG_LC, 120);
mpz_init(seed);
srand((unsigned) getpid());
sd = rand();
mpz_set_ui(seed, sd);
gmp_randseed(stat, seed);


mpz_urandomb(*p, stat, 512);
primetest = mpz_probab_prime_p(*p, 10);
if (primetest != 0) {
printf("p is primen");
} else {
//printf("p wasnt prime,choose next primen");
mpz_nextprime(*p, *p);
}

mpz_urandomb(*q, stat, 512);
primetest = mpz_probab_prime_p(*q, 10);
if (primetest != 0) {
// printf("q is primen");
} else {
// printf("p wasnt prime,choose next primen");
mpz_nextprime(*q, *q);
}


printf("p and q generated!!n");
printf("p = ");
mpz_out_str(stdout, 10, *p);
printf("q = ");
mpz_out_str(stdout, 10, *q);
printf("n------------------------------------------------------------------------------------------n");
mpz_clear(seed);
return;
}

void computeNandF(mpz_t* q, mpz_t* p, mpz_t *phi, mpz_t* n) {

mpz_t temp1, temp2;
mpz_init(temp1);
mpz_init(temp2);
//n=p*q
mpz_mul(*n, *q, *p);
mpz_sub_ui(temp1, *q, 1); //temp1=q-1
mpz_sub_ui(temp2, *p, 1); //temp2=p-1
//φ=(p-1)(q-1)
mpz_mul(*phi, temp1, temp2);
printf("phi and n generated!!n");
printf(" n= ");
mpz_out_str(stdout, 10, *n);
printf("phi = ");
mpz_out_str(stdout, 10, *phi);
printf("n------------------------------------------------------------------------------------------n");
}

void generateE(mpz_t* phi, mpz_t* e) {

mpz_t temp, seed;
mpz_init(seed);
mpz_init(temp);
long sd = 0;
gmp_randinit(stat, GMP_RAND_ALG_LC, 120);
srand((unsigned) getpid());
sd = rand();
mpz_set_ui(seed, sd);
gmp_randseed(stat, seed);

do {
mpz_urandomm(*e, stat, *phi + 1);
mpz_gcd(temp, *phi, *e); //temp=gcd(e,φ)
} while (mpz_cmp_ui(temp, 1) != 0); //while gcd!=1
printf("e generated n e = ");
mpz_out_str(stdout, 10, *e);
printf("n------------------------------------------------------------------------------------------n");

}

void enc(mpz_t* e, mpz_t* n, mpz_t* d, mpz_t* c, char msg) {

int r[40];
for (int i = 0; i < strlen(msg); i++) {
r[i] = (int) msg[i];
}

int *m = r;
mpz_t M;
mpz_init(M);
mpz_import(M, strlen(msg), 1, sizeof (m[0]), 0, 0, m);
printf("message as int before encryption = ");
mpz_out_str(stdout, 10, M);
printf("n");

mpz_powm(*c, M, *e, *n);

}

void dec(mpz_t* m, mpz_t* c, mpz_t* d, mpz_t* n) {

mpz_powm(*m, *c, *d, *n);

}

void encrFile(mpz_t e, mpz_t n, mpz_t d, mpz_t c) {

char text[80];
FILE *file;
file = fopen("text.txt", "r");
int i = 0;
if (file) {
while ((x = getc(file)) != EOF) {
i++;
putchar(x);
text[i] = (char) x;
}

int r[40];
for (int i = 0; i < strlen(text); i++) {
r[i] = (int) text[i];
}

int *m = r;
mpz_t M;
mpz_init(M);
mpz_import(M, strlen(text), 1, sizeof (m[0]), 0, 0, m);
printf("message as int before encryption = ");
mpz_out_str(stdout, 10, M);
printf("n");

mpz_powm(c, M, e, n);
printf("encrypt txt = ");
mpz_out_str(stdout, 10, c);
printf("n");

fclose(file);
file = fopen("text.txt", "w");

mpz_out_raw(file, c);
fclose(file);

}

}


Any comments and improvements in my implementation will be appreciate.










share|improve this question









New contributor




Thelouras is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    0














    This is my first attempt to implement an crypto algorithm.



    I am using C and gmp.h to interact with big number.



    Here is my code, you can run in with -lgmp parameter:



    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <math.h>
    #include <sys/types.h>
    #include "gmp.h"



    void generatePrimes(mpz_t* p, mpz_t* q);
    void computeNandF(mpz_t* q, mpz_t* p, mpz_t *phi, mpz_t* n);
    void generateE(mpz_t* phi, mpz_t* e);
    void enc(mpz_t* e, mpz_t* n, mpz_t* d, mpz_t* c, char);
    void dec(mpz_t* m, mpz_t* c, mpz_t* d, mpz_t* n);
    void makeKeys(mpz_t n, mpz_t e, mpz_t d, mpz_t p, mpz_t q);
    void encrFile(mpz_t e, mpz_t n, mpz_t d, mpz_t c);

    gmp_randstate_t stat;

    int main() {


    mpz_t p, q, phi, e, n, d, c, dc;
    char msg[40] = "welcome to cryptoworld";
    int *mes;
    int len = strlen(msg);
    mpz_init(p);
    mpz_init(q);
    mpz_init(phi);
    mpz_init(e);
    mpz_init(n);
    mpz_init(d);
    mpz_init(c);
    mpz_init(dc);

    // RSA algorithm
    generatePrimes(&p, &q);
    computeNandF(&q, &p, &phi, &n);
    generateE(&phi, &e);
    // extended Euclidean
    mpz_invert(d, e, phi);
    printf("d = ");
    mpz_out_str(stdout, 10, d);
    printf("n------------------------------------------------------------------------------------------n");


    enc(&e, &n, &d, &c, msg);

    dec(&dc, &c, &d, &n);

    printf("n------------------------------------------------------------------------------------------n");
    printf("encrypt message = ");
    mpz_out_str(stdout, 10, c);
    printf("n");
    printf("n------------------------------------------------------------------------------------------n");
    printf("message as int after decr = ");
    mpz_out_str(stdout, 10, dc);
    printf("n");


    mpz_export(mes, (size_t*) malloc(sizeof (size_t)), 1, sizeof (mes[0]), 0, 0, dc);
    char r[40];
    printf("message as string after decr = ");
    for (int i = 0; i < len; i++) {
    r[i] = (char) mes[i];
    printf("%c", r[i]);
    }
    printf("n");

    encrFile(e, n, d, c);

    mpz_clear(p);
    mpz_clear(q);
    mpz_clear(phi);
    mpz_clear(n);
    mpz_clear(e);
    mpz_clear(c);
    mpz_clear(d);
    mpz_clear(dc);
    return 0;
    }

    void generatePrimes(mpz_t* p, mpz_t* q) {

    int primetest;
    long sd = 0;
    mpz_t seed;
    gmp_randinit(stat, GMP_RAND_ALG_LC, 120);
    mpz_init(seed);
    srand((unsigned) getpid());
    sd = rand();
    mpz_set_ui(seed, sd);
    gmp_randseed(stat, seed);


    mpz_urandomb(*p, stat, 512);
    primetest = mpz_probab_prime_p(*p, 10);
    if (primetest != 0) {
    printf("p is primen");
    } else {
    //printf("p wasnt prime,choose next primen");
    mpz_nextprime(*p, *p);
    }

    mpz_urandomb(*q, stat, 512);
    primetest = mpz_probab_prime_p(*q, 10);
    if (primetest != 0) {
    // printf("q is primen");
    } else {
    // printf("p wasnt prime,choose next primen");
    mpz_nextprime(*q, *q);
    }


    printf("p and q generated!!n");
    printf("p = ");
    mpz_out_str(stdout, 10, *p);
    printf("q = ");
    mpz_out_str(stdout, 10, *q);
    printf("n------------------------------------------------------------------------------------------n");
    mpz_clear(seed);
    return;
    }

    void computeNandF(mpz_t* q, mpz_t* p, mpz_t *phi, mpz_t* n) {

    mpz_t temp1, temp2;
    mpz_init(temp1);
    mpz_init(temp2);
    //n=p*q
    mpz_mul(*n, *q, *p);
    mpz_sub_ui(temp1, *q, 1); //temp1=q-1
    mpz_sub_ui(temp2, *p, 1); //temp2=p-1
    //φ=(p-1)(q-1)
    mpz_mul(*phi, temp1, temp2);
    printf("phi and n generated!!n");
    printf(" n= ");
    mpz_out_str(stdout, 10, *n);
    printf("phi = ");
    mpz_out_str(stdout, 10, *phi);
    printf("n------------------------------------------------------------------------------------------n");
    }

    void generateE(mpz_t* phi, mpz_t* e) {

    mpz_t temp, seed;
    mpz_init(seed);
    mpz_init(temp);
    long sd = 0;
    gmp_randinit(stat, GMP_RAND_ALG_LC, 120);
    srand((unsigned) getpid());
    sd = rand();
    mpz_set_ui(seed, sd);
    gmp_randseed(stat, seed);

    do {
    mpz_urandomm(*e, stat, *phi + 1);
    mpz_gcd(temp, *phi, *e); //temp=gcd(e,φ)
    } while (mpz_cmp_ui(temp, 1) != 0); //while gcd!=1
    printf("e generated n e = ");
    mpz_out_str(stdout, 10, *e);
    printf("n------------------------------------------------------------------------------------------n");

    }

    void enc(mpz_t* e, mpz_t* n, mpz_t* d, mpz_t* c, char msg) {

    int r[40];
    for (int i = 0; i < strlen(msg); i++) {
    r[i] = (int) msg[i];
    }

    int *m = r;
    mpz_t M;
    mpz_init(M);
    mpz_import(M, strlen(msg), 1, sizeof (m[0]), 0, 0, m);
    printf("message as int before encryption = ");
    mpz_out_str(stdout, 10, M);
    printf("n");

    mpz_powm(*c, M, *e, *n);

    }

    void dec(mpz_t* m, mpz_t* c, mpz_t* d, mpz_t* n) {

    mpz_powm(*m, *c, *d, *n);

    }

    void encrFile(mpz_t e, mpz_t n, mpz_t d, mpz_t c) {

    char text[80];
    FILE *file;
    file = fopen("text.txt", "r");
    int i = 0;
    if (file) {
    while ((x = getc(file)) != EOF) {
    i++;
    putchar(x);
    text[i] = (char) x;
    }

    int r[40];
    for (int i = 0; i < strlen(text); i++) {
    r[i] = (int) text[i];
    }

    int *m = r;
    mpz_t M;
    mpz_init(M);
    mpz_import(M, strlen(text), 1, sizeof (m[0]), 0, 0, m);
    printf("message as int before encryption = ");
    mpz_out_str(stdout, 10, M);
    printf("n");

    mpz_powm(c, M, e, n);
    printf("encrypt txt = ");
    mpz_out_str(stdout, 10, c);
    printf("n");

    fclose(file);
    file = fopen("text.txt", "w");

    mpz_out_raw(file, c);
    fclose(file);

    }

    }


    Any comments and improvements in my implementation will be appreciate.










    share|improve this question









    New contributor




    Thelouras is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      0












      0








      0







      This is my first attempt to implement an crypto algorithm.



      I am using C and gmp.h to interact with big number.



      Here is my code, you can run in with -lgmp parameter:



      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      #include <unistd.h>
      #include <math.h>
      #include <sys/types.h>
      #include "gmp.h"



      void generatePrimes(mpz_t* p, mpz_t* q);
      void computeNandF(mpz_t* q, mpz_t* p, mpz_t *phi, mpz_t* n);
      void generateE(mpz_t* phi, mpz_t* e);
      void enc(mpz_t* e, mpz_t* n, mpz_t* d, mpz_t* c, char);
      void dec(mpz_t* m, mpz_t* c, mpz_t* d, mpz_t* n);
      void makeKeys(mpz_t n, mpz_t e, mpz_t d, mpz_t p, mpz_t q);
      void encrFile(mpz_t e, mpz_t n, mpz_t d, mpz_t c);

      gmp_randstate_t stat;

      int main() {


      mpz_t p, q, phi, e, n, d, c, dc;
      char msg[40] = "welcome to cryptoworld";
      int *mes;
      int len = strlen(msg);
      mpz_init(p);
      mpz_init(q);
      mpz_init(phi);
      mpz_init(e);
      mpz_init(n);
      mpz_init(d);
      mpz_init(c);
      mpz_init(dc);

      // RSA algorithm
      generatePrimes(&p, &q);
      computeNandF(&q, &p, &phi, &n);
      generateE(&phi, &e);
      // extended Euclidean
      mpz_invert(d, e, phi);
      printf("d = ");
      mpz_out_str(stdout, 10, d);
      printf("n------------------------------------------------------------------------------------------n");


      enc(&e, &n, &d, &c, msg);

      dec(&dc, &c, &d, &n);

      printf("n------------------------------------------------------------------------------------------n");
      printf("encrypt message = ");
      mpz_out_str(stdout, 10, c);
      printf("n");
      printf("n------------------------------------------------------------------------------------------n");
      printf("message as int after decr = ");
      mpz_out_str(stdout, 10, dc);
      printf("n");


      mpz_export(mes, (size_t*) malloc(sizeof (size_t)), 1, sizeof (mes[0]), 0, 0, dc);
      char r[40];
      printf("message as string after decr = ");
      for (int i = 0; i < len; i++) {
      r[i] = (char) mes[i];
      printf("%c", r[i]);
      }
      printf("n");

      encrFile(e, n, d, c);

      mpz_clear(p);
      mpz_clear(q);
      mpz_clear(phi);
      mpz_clear(n);
      mpz_clear(e);
      mpz_clear(c);
      mpz_clear(d);
      mpz_clear(dc);
      return 0;
      }

      void generatePrimes(mpz_t* p, mpz_t* q) {

      int primetest;
      long sd = 0;
      mpz_t seed;
      gmp_randinit(stat, GMP_RAND_ALG_LC, 120);
      mpz_init(seed);
      srand((unsigned) getpid());
      sd = rand();
      mpz_set_ui(seed, sd);
      gmp_randseed(stat, seed);


      mpz_urandomb(*p, stat, 512);
      primetest = mpz_probab_prime_p(*p, 10);
      if (primetest != 0) {
      printf("p is primen");
      } else {
      //printf("p wasnt prime,choose next primen");
      mpz_nextprime(*p, *p);
      }

      mpz_urandomb(*q, stat, 512);
      primetest = mpz_probab_prime_p(*q, 10);
      if (primetest != 0) {
      // printf("q is primen");
      } else {
      // printf("p wasnt prime,choose next primen");
      mpz_nextprime(*q, *q);
      }


      printf("p and q generated!!n");
      printf("p = ");
      mpz_out_str(stdout, 10, *p);
      printf("q = ");
      mpz_out_str(stdout, 10, *q);
      printf("n------------------------------------------------------------------------------------------n");
      mpz_clear(seed);
      return;
      }

      void computeNandF(mpz_t* q, mpz_t* p, mpz_t *phi, mpz_t* n) {

      mpz_t temp1, temp2;
      mpz_init(temp1);
      mpz_init(temp2);
      //n=p*q
      mpz_mul(*n, *q, *p);
      mpz_sub_ui(temp1, *q, 1); //temp1=q-1
      mpz_sub_ui(temp2, *p, 1); //temp2=p-1
      //φ=(p-1)(q-1)
      mpz_mul(*phi, temp1, temp2);
      printf("phi and n generated!!n");
      printf(" n= ");
      mpz_out_str(stdout, 10, *n);
      printf("phi = ");
      mpz_out_str(stdout, 10, *phi);
      printf("n------------------------------------------------------------------------------------------n");
      }

      void generateE(mpz_t* phi, mpz_t* e) {

      mpz_t temp, seed;
      mpz_init(seed);
      mpz_init(temp);
      long sd = 0;
      gmp_randinit(stat, GMP_RAND_ALG_LC, 120);
      srand((unsigned) getpid());
      sd = rand();
      mpz_set_ui(seed, sd);
      gmp_randseed(stat, seed);

      do {
      mpz_urandomm(*e, stat, *phi + 1);
      mpz_gcd(temp, *phi, *e); //temp=gcd(e,φ)
      } while (mpz_cmp_ui(temp, 1) != 0); //while gcd!=1
      printf("e generated n e = ");
      mpz_out_str(stdout, 10, *e);
      printf("n------------------------------------------------------------------------------------------n");

      }

      void enc(mpz_t* e, mpz_t* n, mpz_t* d, mpz_t* c, char msg) {

      int r[40];
      for (int i = 0; i < strlen(msg); i++) {
      r[i] = (int) msg[i];
      }

      int *m = r;
      mpz_t M;
      mpz_init(M);
      mpz_import(M, strlen(msg), 1, sizeof (m[0]), 0, 0, m);
      printf("message as int before encryption = ");
      mpz_out_str(stdout, 10, M);
      printf("n");

      mpz_powm(*c, M, *e, *n);

      }

      void dec(mpz_t* m, mpz_t* c, mpz_t* d, mpz_t* n) {

      mpz_powm(*m, *c, *d, *n);

      }

      void encrFile(mpz_t e, mpz_t n, mpz_t d, mpz_t c) {

      char text[80];
      FILE *file;
      file = fopen("text.txt", "r");
      int i = 0;
      if (file) {
      while ((x = getc(file)) != EOF) {
      i++;
      putchar(x);
      text[i] = (char) x;
      }

      int r[40];
      for (int i = 0; i < strlen(text); i++) {
      r[i] = (int) text[i];
      }

      int *m = r;
      mpz_t M;
      mpz_init(M);
      mpz_import(M, strlen(text), 1, sizeof (m[0]), 0, 0, m);
      printf("message as int before encryption = ");
      mpz_out_str(stdout, 10, M);
      printf("n");

      mpz_powm(c, M, e, n);
      printf("encrypt txt = ");
      mpz_out_str(stdout, 10, c);
      printf("n");

      fclose(file);
      file = fopen("text.txt", "w");

      mpz_out_raw(file, c);
      fclose(file);

      }

      }


      Any comments and improvements in my implementation will be appreciate.










      share|improve this question









      New contributor




      Thelouras is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      This is my first attempt to implement an crypto algorithm.



      I am using C and gmp.h to interact with big number.



      Here is my code, you can run in with -lgmp parameter:



      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      #include <unistd.h>
      #include <math.h>
      #include <sys/types.h>
      #include "gmp.h"



      void generatePrimes(mpz_t* p, mpz_t* q);
      void computeNandF(mpz_t* q, mpz_t* p, mpz_t *phi, mpz_t* n);
      void generateE(mpz_t* phi, mpz_t* e);
      void enc(mpz_t* e, mpz_t* n, mpz_t* d, mpz_t* c, char);
      void dec(mpz_t* m, mpz_t* c, mpz_t* d, mpz_t* n);
      void makeKeys(mpz_t n, mpz_t e, mpz_t d, mpz_t p, mpz_t q);
      void encrFile(mpz_t e, mpz_t n, mpz_t d, mpz_t c);

      gmp_randstate_t stat;

      int main() {


      mpz_t p, q, phi, e, n, d, c, dc;
      char msg[40] = "welcome to cryptoworld";
      int *mes;
      int len = strlen(msg);
      mpz_init(p);
      mpz_init(q);
      mpz_init(phi);
      mpz_init(e);
      mpz_init(n);
      mpz_init(d);
      mpz_init(c);
      mpz_init(dc);

      // RSA algorithm
      generatePrimes(&p, &q);
      computeNandF(&q, &p, &phi, &n);
      generateE(&phi, &e);
      // extended Euclidean
      mpz_invert(d, e, phi);
      printf("d = ");
      mpz_out_str(stdout, 10, d);
      printf("n------------------------------------------------------------------------------------------n");


      enc(&e, &n, &d, &c, msg);

      dec(&dc, &c, &d, &n);

      printf("n------------------------------------------------------------------------------------------n");
      printf("encrypt message = ");
      mpz_out_str(stdout, 10, c);
      printf("n");
      printf("n------------------------------------------------------------------------------------------n");
      printf("message as int after decr = ");
      mpz_out_str(stdout, 10, dc);
      printf("n");


      mpz_export(mes, (size_t*) malloc(sizeof (size_t)), 1, sizeof (mes[0]), 0, 0, dc);
      char r[40];
      printf("message as string after decr = ");
      for (int i = 0; i < len; i++) {
      r[i] = (char) mes[i];
      printf("%c", r[i]);
      }
      printf("n");

      encrFile(e, n, d, c);

      mpz_clear(p);
      mpz_clear(q);
      mpz_clear(phi);
      mpz_clear(n);
      mpz_clear(e);
      mpz_clear(c);
      mpz_clear(d);
      mpz_clear(dc);
      return 0;
      }

      void generatePrimes(mpz_t* p, mpz_t* q) {

      int primetest;
      long sd = 0;
      mpz_t seed;
      gmp_randinit(stat, GMP_RAND_ALG_LC, 120);
      mpz_init(seed);
      srand((unsigned) getpid());
      sd = rand();
      mpz_set_ui(seed, sd);
      gmp_randseed(stat, seed);


      mpz_urandomb(*p, stat, 512);
      primetest = mpz_probab_prime_p(*p, 10);
      if (primetest != 0) {
      printf("p is primen");
      } else {
      //printf("p wasnt prime,choose next primen");
      mpz_nextprime(*p, *p);
      }

      mpz_urandomb(*q, stat, 512);
      primetest = mpz_probab_prime_p(*q, 10);
      if (primetest != 0) {
      // printf("q is primen");
      } else {
      // printf("p wasnt prime,choose next primen");
      mpz_nextprime(*q, *q);
      }


      printf("p and q generated!!n");
      printf("p = ");
      mpz_out_str(stdout, 10, *p);
      printf("q = ");
      mpz_out_str(stdout, 10, *q);
      printf("n------------------------------------------------------------------------------------------n");
      mpz_clear(seed);
      return;
      }

      void computeNandF(mpz_t* q, mpz_t* p, mpz_t *phi, mpz_t* n) {

      mpz_t temp1, temp2;
      mpz_init(temp1);
      mpz_init(temp2);
      //n=p*q
      mpz_mul(*n, *q, *p);
      mpz_sub_ui(temp1, *q, 1); //temp1=q-1
      mpz_sub_ui(temp2, *p, 1); //temp2=p-1
      //φ=(p-1)(q-1)
      mpz_mul(*phi, temp1, temp2);
      printf("phi and n generated!!n");
      printf(" n= ");
      mpz_out_str(stdout, 10, *n);
      printf("phi = ");
      mpz_out_str(stdout, 10, *phi);
      printf("n------------------------------------------------------------------------------------------n");
      }

      void generateE(mpz_t* phi, mpz_t* e) {

      mpz_t temp, seed;
      mpz_init(seed);
      mpz_init(temp);
      long sd = 0;
      gmp_randinit(stat, GMP_RAND_ALG_LC, 120);
      srand((unsigned) getpid());
      sd = rand();
      mpz_set_ui(seed, sd);
      gmp_randseed(stat, seed);

      do {
      mpz_urandomm(*e, stat, *phi + 1);
      mpz_gcd(temp, *phi, *e); //temp=gcd(e,φ)
      } while (mpz_cmp_ui(temp, 1) != 0); //while gcd!=1
      printf("e generated n e = ");
      mpz_out_str(stdout, 10, *e);
      printf("n------------------------------------------------------------------------------------------n");

      }

      void enc(mpz_t* e, mpz_t* n, mpz_t* d, mpz_t* c, char msg) {

      int r[40];
      for (int i = 0; i < strlen(msg); i++) {
      r[i] = (int) msg[i];
      }

      int *m = r;
      mpz_t M;
      mpz_init(M);
      mpz_import(M, strlen(msg), 1, sizeof (m[0]), 0, 0, m);
      printf("message as int before encryption = ");
      mpz_out_str(stdout, 10, M);
      printf("n");

      mpz_powm(*c, M, *e, *n);

      }

      void dec(mpz_t* m, mpz_t* c, mpz_t* d, mpz_t* n) {

      mpz_powm(*m, *c, *d, *n);

      }

      void encrFile(mpz_t e, mpz_t n, mpz_t d, mpz_t c) {

      char text[80];
      FILE *file;
      file = fopen("text.txt", "r");
      int i = 0;
      if (file) {
      while ((x = getc(file)) != EOF) {
      i++;
      putchar(x);
      text[i] = (char) x;
      }

      int r[40];
      for (int i = 0; i < strlen(text); i++) {
      r[i] = (int) text[i];
      }

      int *m = r;
      mpz_t M;
      mpz_init(M);
      mpz_import(M, strlen(text), 1, sizeof (m[0]), 0, 0, m);
      printf("message as int before encryption = ");
      mpz_out_str(stdout, 10, M);
      printf("n");

      mpz_powm(c, M, e, n);
      printf("encrypt txt = ");
      mpz_out_str(stdout, 10, c);
      printf("n");

      fclose(file);
      file = fopen("text.txt", "w");

      mpz_out_raw(file, c);
      fclose(file);

      }

      }


      Any comments and improvements in my implementation will be appreciate.







      c cryptography






      share|improve this question









      New contributor




      Thelouras is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Thelouras is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 14 mins ago





















      New contributor




      Thelouras is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 19 mins ago









      Thelouras

      1016




      1016




      New contributor




      Thelouras is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Thelouras is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Thelouras is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.



























          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Thelouras is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210685%2frsa-c-implementation%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Thelouras is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          Thelouras is a new contributor. Be nice, and check out our Code of Conduct.













          Thelouras is a new contributor. Be nice, and check out our Code of Conduct.












          Thelouras is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210685%2frsa-c-implementation%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Morgemoulin

          Scott Moir

          Souastre