RSA implementation using GMP library in c

I'm implementing rsa encryption using GMP library in c. I get a seg fault on line 44 while trying to read from a file. Thanks for any help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sstream>
#include<gmp.h>

int main()
{
//Compute d and N given p, q, and e
  char inputPQE;
  char outputDN;
  char inputX;
  char outputEX;
  char inputC;
  char outputDC;
  char a, pStr, qStr, eStr;
  int b;
  int k;
  mpz_t p, q, e, d, n, fin, x, c, z;
  mpz_init(p);
  mpz_init(q);
  mpz_init(e);
  mpz_init(d);
  mpz_init(n);
  mpz_init(fin);
  mpz_init(x);
  mpz_init(c);
  mpz_init(z);


//Get file names and number of bits for N
  printf("Enter the name of the file that contains p, q, and e: ");
  scanf("%s", &inputPQE);
  printf("Enter the output file name to store d and N: ");
  scanf("%s", &outputDN);
  printf("Enter the number of bits of N: ");
  scanf ("%d", &k);
  mpz_init2(n, k);
  
  
//Read p, q, e from specified file
  FILE *input1;
  input1 = fopen(&inputPQE, "r");
  fscanf(input1, "%s", &pStr);
  fscanf(input1, "%s", &qStr);
  fscanf(input1, "%s", &eStr);
  mpz_set_str(p, &pStr, 10);
  mpz_set_str(q, &qStr, 10);
  mpz_set_str(e, &eStr, 10);
  int fclose(FILE *input1);


//Compute d and N using the given p, q, and e
  mpz_mul(n, p, q);
  mpz_sub_ui(p, p, 1UL);
  mpz_sub_ui(q, q, 1UL);
  mpz_mul(fin, p, q);
  mpz_invert(d, e, fin);


//Output d and N to specified file  
  FILE *output1;
  output1 = fopen(&outputDN, "w");
  fprintf(output1, "%ld\n", mpz_get_ui(d));
  fprintf(output1, "%ld\n", mpz_get_ui(n));
  int fclose(FILE *output1);


//Encrypt x and output the value

//Get file names
  printf("Enter the name of the file that contains x to be encrypted: ");
  scanf("%s", &inputX);
  printf("Enter the output file name to store E(x): ");
  scanf("%s", &outputEX);
 
  
//Read in x
  FILE *input2;
  input2 = fopen(&inputX, "r");
  mpz_inp_str(x, input2, 10);
  int fclose(FILE *input2);
 
  
//Encrypt x
  mpz_powm_ui(c, x, mpz_get_ui(e), n);
 
  
//Output cipher to file
  FILE *output2;
  output2 = fopen(&outputEX, "w");
  mpz_inp_str(c, output2, 10);
  int fclose(FILE *output2);


//Decrypt c

//Get file names
  printf("Enter the name of the file that contains c to be decrypted: ");
  scanf("%s", &inputC);
  printf("Enter the output file name to store D(c): ");
  scanf("%s", &outputDC);
 
  
//Read in c
  FILE *input3;
  input3 = fopen(&inputC, "r");
  mpz_inp_str(c, input3, 10);
  int fclose(FILE *input3);
 
  
//Decrypt x
  mpz_powm_ui(x, c, mpz_get_ui(d), n);
 
  
//Output cipher to file
  FILE *output3;
  output3 = fopen(&outputDC, "w");
  mpz_inp_str(x, output3, 10);
  int fclose(FILE *output3);
  
  return 0;
}
Topic archived. No new replies allowed.