I have a 1024-bit RSA
private key in text format in a file privateKey.txt
contains,
RSA Private-Key: (1024 bit, 2 primes)
modulus:
00:a3:38:61:b8:4a:29:60:2c:2f:6c:df:3b:e1:9b:
b9:13:96:8f:dc:7f:b4:5a:7c:6d:61:6e:f4:df:63:
36:1b:7c:97:b9:f5:03:8e:40:7e:ca:48:00:5f:b6:
a3:51:66:39:7b:d2:b6:f0:5e:5e:93:de:e0:ec:c6:
ae:bb:c5:3d:50:be:ca:e1:02:d0:0b:e2:36:75:43:
e0:64:81:25:92:a6:26:e3:55:d8:cf:fe:f5:44:4f:
c5:d5:1d:8a:6e:d2:3e:61:50:72:95:26:a2:7f:8a:
66:a6:57:da:58:13:c7:9a:d4:b7:cb:5d:c7:c7:5e:
d4:94:e5:b8:cc:e2:c4:2c:c3
publicExponent: 65537 (0x10001)
privateExponent:
21:19:4b:e7:bd:52:4a:a8:6b:2d:9e:cf:5b:de:e2:
57:30:8f:17:dd:e5:ca:fb:b4:8a:af:9f:84:b5:6f:
c1:7f:30:9d:8b:2c:1f:6a:c0:cb:70:72:72:f6:6c:
84:13:40:d0:f5:58:17:c2:6b:57:7d:39:0b:8a:f6:
e0:29:0d:57:c1:73:99:1a:e1:e7:bc:52:15:89:08:
7e:d5:cb:dc:be:0b:af:32:4c:b1:f3:cc:ea:bc:95:
1a:37:19:5c:de:94:be:ca:9b:57:fb:42:01:1c:42:
db:d5:b2:d4:16:f7:f2:dd:7f:28:c4:bb:67:9d:21:
ef:4b:d2:b8:78:cf:f6:61
prime1:
00:d7:84:e9:f3:4a:2b:f9:c2:de:a4:ce:6c:5d:57:
fa:bd:77:c3:83:55:cf:b0:42:c8:9d:e4:85:60:dd:
c8:9a:e4:f8:52:80:ac:fa:cb:33:36:2f:cf:ad:8d:
2a:7b:9e:6a:dc:6f:94:d9:91:0f:47:42:64:22:5d:
d4:84:7d:85:99
prime2:
00:c1:e0:b5:ca:28:7a:46:2f:d3:46:ec:86:7c:20:
c2:49:d6:20:5c:aa:4a:c4:5f:fd:88:d0:ba:f2:e3:
33:9a:d4:29:dc:94:5b:3c:05:99:2e:7f:a6:3a:73:
34:e1:2a:58:a9:c4:69:c1:cf:ed:5c:b1:a8:45:e0:
e0:cc:c2:06:bb
exponent1:
0f:ed:f7:0c:d0:72:a7:a7:4b:a5:8a:cc:5f:a9:dd:
12:9c:03:8b:75:90:40:4a:ef:61:1c:3e:ef:85:f5:
09:dd:da:ac:de:6d:ea:ab:92:41:73:0f:3a:98:b0:
87:2a:92:63:70:ab:e3:d8:e0:f4:37:68:07:52:40:
1e:fa:d8:21
exponent2:
63:f8:fd:84:aa:ee:b2:36:83:19:b5:69:7b:a4:60:
cb:36:31:25:d7:62:0a:74:fd:05:89:ac:7d:06:8f:
5b:ad:3f:90:d0:a7:63:01:f8:05:34:29:81:46:5a:
e4:28:b3:e3:86:b6:8a:0c:2f:f7:76:5c:b0:fb:ce:
af:86:f5:75
coefficient:
00:c0:30:ec:87:5f:7b:d2:c6:ab:b8:17:3a:aa:de:
7c:6b:3e:24:5d:12:ca:d6:87:7d:df:b8:28:df:b1:
b6:c6:46:0b:13:98:82:2b:82:8b:ea:1a:2f:cd:5a:
17:e5:54:a3:17:d6:e6:0f:68:c1:76:73:2e:08:8f:
18:66:ef:b6:cd
I'm trying to read the contents of this file and tokenize each components/blocks separately (like modulus, publicExponent, privateExponent, etc.) using the following code. However, When I'm trying to read from the file, seems like I'm not reading the whole content,
#include<stdio.h>
#include<string.h>
#define KEY_BUFFER_SIZE 2560
unsigned char private_size[2070];
int main() {
FILE * fp2 = fopen ("privateKey.txt", "r");
int size_key=KEY_BUFFER_SIZE;
if(fp2){
fread(private_size,1,sizeof (private_size),fp2);
//fgets(private_size,sizeof (private_size),fp2);
}
fclose(fp2);
while(size_key>0){
size_key=size_key-1;
}
printf("%s\n",private_size);
return 0;
}
output:
RSA Private-Key: (1024 bit, 2 primes)
modulus:
00:a3:38:61:b8:4a:29:60:2c:2f:6c:df:3b:e1:9b:
b9:13:96:8f:dc:7f:b4:5a:7c:6d:61:6e:f4:df:63:
36:1b:7c:97:b9:f5:03:8e:40:7e:ca:48:00:5f:b6:
a3:51:66:39:7b:d2:b6:f0:5e:5e:93:de:e0:ec:c6:
ae:bb:c5:3d:50:be:ca:e1:02:d0:0b:e2:36:75:43:
e0:64:81:25:92:a6:26:e3:55:d8:cf:fe:f5:44:4f:
c5:d5:1d:8a:6e:d2:3e:61:50:72:95:26:a2:7f:8a:
66:a6:57:da:58:13:c7:9a:d4:b7:cb:5d:c7:c7:5e:
d4:94:e5:b8:cc:e2:c4:2c:c3
publicExponent: 65537 (0x10001)
privateExponent:
21:19:4b:e7:bd:52:4a:a8:6b:2d:9e:cf:5b:de:e2:
57:30:8f:17:dd:e5:ca:fb:b4:8a:af:9f:84:b5:6f:
c1:7f:30:9d:8b:2c:1f:6a:c0:cb:70:72:72:f6:6c:
84:13:40:d0:f5:58:17:c2:6b:57:7d:39:0b:8a:f6:
e0:29:0d:57:c1:73:99:1a:e1:e7:bc:52:15:89:08:
7e:d5:cb:dc:be:0b:af:32:4c:b1:f3:cc:ea:bc:95:
1a:37:19:5c:de:94:be:ca:9b:57:fb:42:01:1c:42:
db:d5:b2:d4:16:f7:f2:dd:7f:28:c4:bb:67:9d:21:
ef:4b:d2:b8:78:cf:f6:61
prime1:
00:d7:84:e9:f3:4a:2b:f9:c2:de:a4:ce:6c:5d:57:
fa:bd:77:c3:83:55:cf:b0:42:c8:9d:e4:85:60:dd:
c8:9a:e4:f8:52:80:ac:fa:cb:33:36:2f:cf:ad:8d:
2a:7b:9e:6a:dc:6f:94:d9:91:0f:47:42:64:22:5d:
d4:84:7d:85:99
prime2:
00:c1:e0:b5:ca:28:7a:46:2f:d3:46:ec:86:7c:20:
c2:49:d6:20:5c:aa:4a:c4:5f:fd:88:d0:ba:f2:e3:
33:9a:d4:29:dc:94:5b:3c:05:99:2e:7f:a6:3a:73:
34:e1:2a:58:a9:c4:69:c1:cf:ed:5c:b1:a8:45:e0:
e0:cc:c2:06:bb
exponent1:
0f:ed:f7:0c:d0:72:a7:a7:4b:a5:8a:cc:5f:a9:dd:
12:9c:03:8b:75:90:40:4a:ef:61:1c:3e:ef:85:f5:
09:dd:da:ac:de:6d:ea:ab:92:41:73:0f:3a:98:b0:
87:2a:92:63:70:ab:e3:d8:e0:f4:37:68:07:52:40:
1e:fa:d8:21
exponent2:
63:f8:fd:84:aa:ee:b2:36:83:19:b5:69:7b:a4:60:
cb:36:31:25:d7:62:0a:74:fd:05:89:ac:7d:06:8f:
5b:ad:3f:90:d0:a7:63:01:f8:05:34:29:81:46:5a:
e4:28:b3:e3:86:b6:8a:0c:2f:f7:76:5c:b0:fb:ce:
af:86:f5:75
coefficient:
00:c0:30:ec:87:5f:7b:d2:c6:ab:b8:17:3a:aa:de:
7c:6b:3e:24:5d:12:ca:d6:87:7d:df:b8:28:df:b1:
b6:c6:46:0b:13:98:82:2b:82:8b:ea:1a:2f:cd:5a:
17:e5:54:a3:17:d6:e6:0f:68:c1:76:73:2e:0
I'm not sure from where the first two lines of the output are coming from.
Update: I have updated my code and output according to the suggestions and now I'm able to read the whole file correctly using the above code
if(fp2)
fread(private_size,1,sizeof (private_size),fp2);
fclose(fp2);
should be enough.
I think RSA Private-Key: (1024 bit, 2 primes)
should not be included in the input file, as it is hard to parse it.
Other problem comes from the fact that you do not instrument the compiler to consider the correct signature for strtok_r
by doing
#include <string.h>
In the case you do not declare the correct signature, it uses the K&R
calling convention, and this is wrong here, as automatic conversion from char*
toward int
and back is undefined behavior.