如何从文件中读取数据并标记化?
How can I read data from file and tokenize?
我有一个 1024 位 RSA
文本格式的私钥,文件 privateKey.txt
包含,
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
我正在尝试读取此文件的内容并使用以下代码分别标记每个 components/blocks(如模数、publicExponent、privateExponent 等)。但是,当我试图从文件中读取时,似乎我没有读取全部内容,
#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;
}
输出:
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
我不确定前两行输出的来源。
更新:
我已经根据建议更新了我的代码和输出,现在我可以使用上面的代码正确读取整个文件
if(fp2)
fread(private_size,1,sizeof (private_size),fp2);
fclose(fp2);
应该够了。
我认为 RSA Private-Key: (1024 bit, 2 primes)
不应该包含在输入文件中,因为它很难解析。
其他问题是因为您没有通过
来检测编译器来考虑 strtok_r
的正确签名
#include <string.h>
如果你没有声明正确的签名,它使用 K&R
调用约定,这里是错误的,因为从 char*
到 int
的自动转换和返回是 未定义的行为。
我有一个 1024 位 RSA
文本格式的私钥,文件 privateKey.txt
包含,
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
我正在尝试读取此文件的内容并使用以下代码分别标记每个 components/blocks(如模数、publicExponent、privateExponent 等)。但是,当我试图从文件中读取时,似乎我没有读取全部内容,
#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;
}
输出:
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
我不确定前两行输出的来源。
更新: 我已经根据建议更新了我的代码和输出,现在我可以使用上面的代码正确读取整个文件
if(fp2)
fread(private_size,1,sizeof (private_size),fp2);
fclose(fp2);
应该够了。
我认为 RSA Private-Key: (1024 bit, 2 primes)
不应该包含在输入文件中,因为它很难解析。
其他问题是因为您没有通过
来检测编译器来考虑strtok_r
的正确签名
#include <string.h>
如果你没有声明正确的签名,它使用 K&R
调用约定,这里是错误的,因为从 char*
到 int
的自动转换和返回是 未定义的行为。