133 lines
3.5 KiB
C++
133 lines
3.5 KiB
C++
#include <openssl/evp.h>
|
||
#include <openssl/pem.h>
|
||
#include <openssl/err.h>
|
||
#include <openssl/rsa.h>
|
||
#include <iostream>
|
||
#include <string>
|
||
|
||
/**
|
||
* @brief 生成RSA密钥对
|
||
* @param privateKeyPath 私钥保存路径
|
||
* @param publicKeyPath 公钥保存路径
|
||
* @param keySize 密钥长度(默认2048位)
|
||
* @return 0表示成功,-1表示失败
|
||
*/
|
||
int generateRSAKeyPair(const char *privateKeyPath, const char *publicKeyPath, int keySize = 2048)
|
||
{
|
||
if (!privateKeyPath || !publicKeyPath) {
|
||
std::cerr << "错误:密钥路径不能为空" << std::endl;
|
||
return -1;
|
||
}
|
||
|
||
// 生成RSA密钥对
|
||
EVP_PKEY *pkey = EVP_PKEY_new();
|
||
if (!pkey) {
|
||
std::cerr << "错误:无法创建密钥对象" << std::endl;
|
||
return -1;
|
||
}
|
||
|
||
// 创建RSA密钥生成上下文
|
||
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, nullptr);
|
||
if (!ctx) {
|
||
std::cerr << "错误:无法创建密钥生成上下文" << std::endl;
|
||
EVP_PKEY_free(pkey);
|
||
return -1;
|
||
}
|
||
|
||
// 初始化密钥生成
|
||
if (EVP_PKEY_keygen_init(ctx) <= 0) {
|
||
std::cerr << "错误:无法初始化密钥生成" << std::endl;
|
||
EVP_PKEY_CTX_free(ctx);
|
||
EVP_PKEY_free(pkey);
|
||
return -1;
|
||
}
|
||
|
||
// 设置密钥长度
|
||
if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, keySize) <= 0) {
|
||
std::cerr << "错误:无法设置密钥长度" << std::endl;
|
||
EVP_PKEY_CTX_free(ctx);
|
||
EVP_PKEY_free(pkey);
|
||
return -1;
|
||
}
|
||
|
||
// 生成密钥对
|
||
if (EVP_PKEY_keygen(ctx, &pkey) <= 0) {
|
||
std::cerr << "错误:密钥生成失败" << std::endl;
|
||
EVP_PKEY_CTX_free(ctx);
|
||
EVP_PKEY_free(pkey);
|
||
return -1;
|
||
}
|
||
|
||
// 保存私钥到文件
|
||
FILE *privateKeyFile = fopen(privateKeyPath, "w");
|
||
if (!privateKeyFile) {
|
||
std::cerr << "错误:无法创建私钥文件" << std::endl;
|
||
EVP_PKEY_CTX_free(ctx);
|
||
EVP_PKEY_free(pkey);
|
||
return -1;
|
||
}
|
||
|
||
if (PEM_write_PrivateKey(privateKeyFile, pkey, nullptr, nullptr, 0, nullptr, nullptr) != 1) {
|
||
std::cerr << "错误:无法写入私钥" << std::endl;
|
||
fclose(privateKeyFile);
|
||
EVP_PKEY_CTX_free(ctx);
|
||
EVP_PKEY_free(pkey);
|
||
return -1;
|
||
}
|
||
fclose(privateKeyFile);
|
||
|
||
// 保存公钥到文件
|
||
FILE *publicKeyFile = fopen(publicKeyPath, "w");
|
||
if (!publicKeyFile) {
|
||
std::cerr << "错误:无法创建公钥文件" << std::endl;
|
||
EVP_PKEY_CTX_free(ctx);
|
||
EVP_PKEY_free(pkey);
|
||
return -1;
|
||
}
|
||
|
||
if (PEM_write_PUBKEY(publicKeyFile, pkey) != 1) {
|
||
std::cerr << "错误:无法写入公钥" << std::endl;
|
||
fclose(publicKeyFile);
|
||
EVP_PKEY_CTX_free(ctx);
|
||
EVP_PKEY_free(pkey);
|
||
return -1;
|
||
}
|
||
fclose(publicKeyFile);
|
||
|
||
// 清理资源
|
||
EVP_PKEY_CTX_free(ctx);
|
||
EVP_PKEY_free(pkey);
|
||
|
||
std::cout << "RSA密钥对生成成功!" << std::endl;
|
||
std::cout << "私钥保存到: " << privateKeyPath << std::endl;
|
||
std::cout << "公钥保存到: " << publicKeyPath << std::endl;
|
||
|
||
return 0;
|
||
}
|
||
|
||
/**
|
||
* @brief 主函数
|
||
*/
|
||
int main(int argc, char *argv[])
|
||
{
|
||
// 固定文件名,生成在程序所在目录
|
||
const char *privateKeyPath = "private_key.pem";
|
||
const char *publicKeyPath = "public_key.pem";
|
||
int keySize = 2048; // 默认密钥长度
|
||
|
||
std::cout << "=== XNSignature 密钥生成工具 ===" << std::endl;
|
||
std::cout << "私钥文件: " << privateKeyPath << std::endl;
|
||
std::cout << "公钥文件: " << publicKeyPath << std::endl;
|
||
std::cout << "密钥长度: " << keySize << " 位" << std::endl;
|
||
std::cout << "正在生成密钥对..." << std::endl;
|
||
|
||
int result = generateRSAKeyPair(privateKeyPath, publicKeyPath, keySize);
|
||
|
||
if (result == 0) {
|
||
std::cout << "密钥生成完成!" << std::endl;
|
||
return 0;
|
||
} else {
|
||
std::cerr << "密钥生成失败!" << std::endl;
|
||
return -1;
|
||
}
|
||
} |