每次使用 c++ 生成相同的 RSA 密钥
Generate every time same RSA key with c++
我每次都尝试使用 RSA_generate_key_ex(...( 生成相同的 RSA 密钥,然后写入文件,但我总是得到不同的密钥。是否有任何交钥匙解决方案,或者我需要为此编写自己的函数?(我知道这是不安全的解决方案(
char *passW = "password";
RSA *rsa = RSA_new();
BIGNUM *bn = BN_new();
BN_bn2bin(bn, (unsigned char *)passW);
BN_set_word(bn, RSA_3);
OpenSSL_add_all_algorithms();
RSA_generate_key_ex(rsa, 1024, bn, nullptr);
/* Save private key */
FILE* fp = fopen("private.key", "w");
PEM_write_RSAPrivateKey(fp, rsa, EVP_aes_256_cbc(), nullptr, 0, nullptr, &passW);
fclose(fp);
如果密码匹配,我希望使用相同的 RSA 密钥。
阅读有关RAND_seed的文档 我发现,我必须编写我的一个兰德引擎(我从这个答案中获取了它(,我已经针对我的问题修改了这个解决方案
// These don't need to do anything if you don't have anything for them to do.
static void stdlib_rand_cleanup() {}
static void stdlib_rand_add(const void *buf, int num, double add_entropy) {}
static int stdlib_rand_status() { return 1; }
// Seed the RNG. srand() takes an unsigned int, so we just use the first
// sizeof(unsigned int) bytes in the buffer to seed the RNG.
static void stdlib_rand_seed(const void *buf, int num)
{
assert(num >= sizeof(unsigned int)); /// Use if statement, if u don't want to terminate ur app
srand(*((unsigned int *)buf));
}
// Fill the buffer with random bytes. For each byte in the buffer, we generate
// a random number and clamp it to the range of a byte, 0-255.
static int stdlib_rand_bytes(unsigned char *buf, int num)
{
for(int index = 0; index < num; ++index)
{
buf[index] = rand() % 256;
}
return 1;
}
// Create the table that will link OpenSSL's rand API to our functions.
static RAND_METHOD stdlib_rand_meth = {
stdlib_rand_seed,
stdlib_rand_bytes,
stdlib_rand_cleanup,
stdlib_rand_add,
stdlib_rand_bytes,
stdlib_rand_status
};
// This is a public-scope accessor method for our table.
RAND_METHOD *RAND_stdlib() { return &stdlib_rand_meth; }
void createRSAKey() {
// Intilize data
int bits = 2048; // 512, 1024, 2048, 4096
std::string passW = "password";
unsigned int randBuff = sizeof(passW);
unsigned int randNum[4];
// Create BIGNUM
BIGNUM *bn = BN_new();
BN_set_word(bn, RSA_3);
// Create RSA
RSA *rsa = RSA_new();
// Connect algorithms, another way it crashes
OpenSSL_add_all_algorithms();
// Randomize bytes
RAND_set_rand_method(RAND_stdlib());
RAND_seed(&randBuff, sizeof(randBuff));
RAND_bytes(reinterpret_cast<unsigned char *>(&randNum[0]), sizeof(randNum));
if(RSA_generate_key_ex(rsa, bits, bn, nullptr) != 1) { qDebug() << "error"; }
// Save private key
FILE *fp = fopen("private.key", "w");
PEM_write_RSAPrivateKey(fp, rsa, nullptr, nullptr, 0, nullptr, &passW);
fclose(fp);
// Clear memory
BN_free(bn);
RSA_free(rsa);
}
相关文章:
- SSH通过/sbin/SSH无法读取RSA密钥文件(从控制台运行)
- 在加密++中使用RSA加密对称AES密钥
- C++生成 RSA 密钥对并读取
- 一代 RSA-2048 密钥(公钥和私钥)
- 每次使用 c++ 生成相同的 RSA 密钥
- Windows TPM 虚拟智能卡 RSA 密钥存储
- 如何在C++中使用Cryptolib加载puttygen RSA密钥
- 如何生成不同寻常但数学上有效的RSA密钥
- 在 rjindael.cpp 中崩溃,使用 RSA 密钥时功能AESNI_Enc_Block
- OpenSSL:我使用存储外部文件的RSA密钥加密和解密文件,但有时我的程序会崩溃
- 通过 tcp 发送 rsa 密钥
- 如何在没有 p、q 等的情况下加载 RSA 密钥对
- 如何在没有Openssl或其他库的情况下加载RSA密钥对
- 加密++ rsa密钥进入缓冲区
- 介绍如何打印OpenSSL RSA密钥的内容
- 以"simple"形式获取 RSA 密钥
- OpenSSL,从CString读取公共RSA密钥
- 在其他计算机上导入RSA密钥的问题
- 从RSA密钥对变量中分离公钥和私钥
- 在.net中使用OpenSSL RSA密钥