无法在 OpenSSL 中设置 public/private 键
Can't set public/private key in OpenSSL
我正在尝试通过字符串将 public 和私钥设置为 C++ 中的 EVP_PKEY。然而,即使在遵循几个在线示例之后,PKEY 始终显示为 NULL。这是重现问题的代码(这是针对私钥的,但对 public 密钥的效果相同):
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <string>
#include <iostream>
int main()
{
EVP_PKEY *keypair;
keypair = NULL;
std::string privateKey = "-----BEGIN PRIVATE KEY-----MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCuKz6ENUUniXU9CelOgD6oTlmkxPWZoaeBUVXLwlh2aE4As/8RMaIpTsxq9M8UmY7L69GVHo51+wmACrW1ftEa3S5rVLRuOrwFNYGLUlrbji4huD5ipxS0MbxKE+lc/VYLTN26zPI8pl8Qb38eESiLXZ7uNO1XRisKxjixy/J4CMMDdn0UFTjKxXmdQluN67T1JlzGzALRKYFMLF8H+VLqhW6ygFE1OjjJ8zwQuP8t2mRBFLXwH1bwffXX4rUwAb+C9FXHWEn5SJv8aoj/9awEw7cYWa0eeAbl2TfPW+QcbzcITp9L9g/9/8M9rH0wqURCzv+z7P0Wkc2JrdcA6vIDAgMBAAECggEAHjFO8BpP96+jjEZIlJw0wHZ8ocynCs/0Or2/0sfJEBW61/JcGD3zb5fCZhQ0Rczs7TrfJSub5KX1GqZAo8rtMuv+3QpdEPLLn1eMMnu/VmpKYvwOjImMVER+8JSNEtqz7NeRZI2cvJjWyAGcFrlAvxAmugKg1k1Krt53+5JwYRBSWQb+05egOXVoQahBJbLWbWJrUFBjmsCAkF/1MvnJ9h2CAmrJIOWMjzWgs2MyUSzAE0I0vLG/mtlUXUGWA7EFAY6BNGWUaHdd6Cy2GhlCoEEL4gYATdtQK1k5r/5O54gTJq1rISEjicXp/qLiFIEa8feuCFGWfFQnIXlmvRZ/gQKBgQDkA8gdQ5XTBQHdcasIeNjJTgmB1W0cugYmUB01O0Kk0O5sVAh0LtWkIF6Tw5Od3olYRt/UNpegOvYoc+lm3kLS14NPcd5P3Zdl/i9NcWq8QF3uNzT5NfLi2nh8lyH2q41Koc2skRQsIQkZEOgLjP2+n3SsZf0PBOE9rYeO9rkSwwKBgQDDi6CWVOSH4suSGLJeauc/j7/MwMAtM7r5cOegh1tJZrAhiL7DXeWM9iT2VWU+9fiH16Je6+0PuXPmkcj3T/E/fSWQ+6n0YjnvIuyQGxMFXW2Zwssxgv51iD+EO88lFD+2wR6YUHl0wPeSQTUvHM3xUp/uOG0yUvoCijqxFCYvwQKBgCvy7qd1V3ONC4b7WdQQNvjWKc70rEtVL8pCtt6DxZyvCMWojNiYDmXMN26Ty+gp9J8nSg1FWIRx2PCDJwHE5WxuTckKUadjNsNZ8xQiQEh4v6Ii0fsXAvHm2kJpfwpklp9EALi6XgEDfRVvxMo1KtOLOJniLpyufBu/TtmnhJSxAoGAG+CI+UWK+vBTMzXAIAO/iek1X7xO6WLJkgwNiW4ijnlMPMug3ntBYXIp3NBZybsJPlx4cm3R8v7uTf2Ul+VWsEVoGJ44IXXranMhJH4nnQkvuc7yZtGU8y6EV5arNk8Hskcf1HkMh5+G69vN+DR0C6ZK1IxIzPbVW41XRq1nsEECgYBmtbY5dIvRemh4FdsMjdUuQcurvyQpEIwdczWurRXfKCuWtbVvn1arTQ/nL+kUixMjPueBInX90vxP+ptoPAHXOMKuZx+bFNwIQZ+OIBQ1WPUG8nV4o7tRZOkIPSy8YVz8UFQuuA6WF5W2kh3gc3iPrQt5JjspkWPDuC1taQo/pw==-----END PRIVATE KEY-----";
BIO *bio = BIO_new(BIO_s_mem());
if(BIO_write(bio, (unsigned char*)privateKey.c_str(), (size_t)privateKey.size()) != (int)privateKey.size())
{
BIO_free_all(bio);
return 0;
}
PEM_read_bio_PrivateKey(bio, &keypair, NULL, NULL);
BIO_free_all(bio);
if(keypair == NULL)
{
std::cout << "error";
}
else
{
return 0;
}
}
我做错了什么?
您传递的私钥无效;它缺少换行符。 -----BEGIN PRIVATE KEY-----
页眉后需要一个,相应页脚之前需要一个,正文中每64个字符后需要一个,例如
std::string privateKey = "-----BEGIN RSA PRIVATE KEY-----\n"
"MIIEogIBAAKCAQEAris+hDVFJ4l1PQnpToA+qE5ZpMT1maGngVFVy8JYdmhOALP/\n"
"ETGiKU7MavTPFJmOy+vRlR6OdfsJgAq1tX7RGt0ua1S0bjq8BTWBi1Ja244uIbg+\n"
/* ... */
"5y/pFIsTIz7ngSJ1/dL8T/qbaDwB1zjCrmcfmxTcCEGfjiAUNVj1BvJ1eKO7UWTp\n"
"CD0svGFc/FBULrgOlheVtpId4HN4j60LeSY7KZFjw7gtbWkKP6c=\n"
"-----END RSA PRIVATE KEY-----";
我正在尝试通过字符串将 public 和私钥设置为 C++ 中的 EVP_PKEY。然而,即使在遵循几个在线示例之后,PKEY 始终显示为 NULL。这是重现问题的代码(这是针对私钥的,但对 public 密钥的效果相同):
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <string>
#include <iostream>
int main()
{
EVP_PKEY *keypair;
keypair = NULL;
std::string privateKey = "-----BEGIN PRIVATE KEY-----MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCuKz6ENUUniXU9CelOgD6oTlmkxPWZoaeBUVXLwlh2aE4As/8RMaIpTsxq9M8UmY7L69GVHo51+wmACrW1ftEa3S5rVLRuOrwFNYGLUlrbji4huD5ipxS0MbxKE+lc/VYLTN26zPI8pl8Qb38eESiLXZ7uNO1XRisKxjixy/J4CMMDdn0UFTjKxXmdQluN67T1JlzGzALRKYFMLF8H+VLqhW6ygFE1OjjJ8zwQuP8t2mRBFLXwH1bwffXX4rUwAb+C9FXHWEn5SJv8aoj/9awEw7cYWa0eeAbl2TfPW+QcbzcITp9L9g/9/8M9rH0wqURCzv+z7P0Wkc2JrdcA6vIDAgMBAAECggEAHjFO8BpP96+jjEZIlJw0wHZ8ocynCs/0Or2/0sfJEBW61/JcGD3zb5fCZhQ0Rczs7TrfJSub5KX1GqZAo8rtMuv+3QpdEPLLn1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o7tRZOkIPSy8YVz8UFQuuA6WF5W2kh3gc3iPrQt5JjspkWPDuC1taQo/pw==-----END PRIVATE KEY-----";
BIO *bio = BIO_new(BIO_s_mem());
if(BIO_write(bio, (unsigned char*)privateKey.c_str(), (size_t)privateKey.size()) != (int)privateKey.size())
{
BIO_free_all(bio);
return 0;
}
PEM_read_bio_PrivateKey(bio, &keypair, NULL, NULL);
BIO_free_all(bio);
if(keypair == NULL)
{
std::cout << "error";
}
else
{
return 0;
}
}
我做错了什么?
您传递的私钥无效;它缺少换行符。 -----BEGIN PRIVATE KEY-----
页眉后需要一个,相应页脚之前需要一个,正文中每64个字符后需要一个,例如
std::string privateKey = "-----BEGIN RSA PRIVATE KEY-----\n"
"MIIEogIBAAKCAQEAris+hDVFJ4l1PQnpToA+qE5ZpMT1maGngVFVy8JYdmhOALP/\n"
"ETGiKU7MavTPFJmOy+vRlR6OdfsJgAq1tX7RGt0ua1S0bjq8BTWBi1Ja244uIbg+\n"
/* ... */
"5y/pFIsTIz7ngSJ1/dL8T/qbaDwB1zjCrmcfmxTcCEGfjiAUNVj1BvJ1eKO7UWTp\n"
"CD0svGFc/FBULrgOlheVtpId4HN4j60LeSY7KZFjw7gtbWkKP6c=\n"
"-----END RSA PRIVATE KEY-----";