标准的openssl生成的证书的顺序是 :
[OpenSSL 测试程序: https://github.com/qianguozheng/datastructure]
如下是生成密钥对, 根据密钥并且分别保存密钥对的私钥,与公钥.
经过测试, 密钥对中的私钥可以提取出公钥, 我对其密钥不是很了解,通过测试发现是可以的. 不知道谁可以讲述下.
/* 产生RSA密钥 */
RSA *rsa = RSA_new();
BIGNUM* e = BN_new();
/* 设置随机数长度 */
BN_set_word(e, 65537);
/* 生成RSA密钥对 */
RSA_generate_key_ex(rsa, 2048, e, NULL);
printf("BIGNUM: %s\n", BN_bn2hex(rsa->n));
/* 提取私钥 */
printf("PRIKEY:\n");
FILE *filename = NULL;
filename = fopen("privateKey.pem", "wb");
PEM_write_RSAPrivateKey(filename, rsa, NULL, NULL, 0, NULL, NULL);
fclose(filename);
//writeRSA(rsa, NULL);
/* 提取公钥 */
unsigned char *n_b = (unsigned char *)calloc(RSA_size(rsa), sizeof(unsigned char));
unsigned char *e_b = (unsigned char *)calloc(RSA_size(rsa), sizeof(unsigned char));
int n_size = BN_bn2bin(rsa->n, n_b);
int b_size = BN_bn2bin(rsa->e, e_b);
RSA *pubrsa = RSA_new();
pubrsa->n = BN_bin2bn(n_b, n_size, NULL);
pubrsa->e = BN_bin2bn(e_b, b_size, NULL);
printf("PUBKEY: \n");
FILE *publicKey = NULL;
publicKey = fopen("publicKey.pem", "wb");
PEM_write_RSAPublicKey(publicKey, pubrsa);
fclose(publicKey);
RSA_free(rsa);
RSA_free(pubrsa);
时间: 2024-10-29 19:06:25